Related Topics
DBMS
DBMS Page 1
DBMS Page 2
DBMS Page 3
DBMS Page 4
DBMS Page 5
DBMS Page 6
DBMS Page 7
DBMS Page 8
DBMS Page 9
DBMS Page 10
DBMS Page 11
DBMS Page 12
DBMS Page 13
DBMS Page 14
DBMS Page 15
DBMS Page 16
DBMS Page 17
DBMS Page 18
DBMS Page 19
DBMS Page 20
DBMS Page 21
DBMS Page 22
DBMS Page 23
DBMS Page 24
DBMS Page 25
DBMS Page 26
DBMS Page 27
DBMS Page 28

C++ Programing
class MyClass {
public:
int myData;
void myFunction() {
// Code for the function
}
};
int main() {
MyClass obj;
obj.myData = 42; // Accessing data member using dot operator
obj.myFunction(); // Calling member function using dot operator
return 0;
}
Arrow Operator (->) with Pointers:
When you have a pointer to an object, you use the arrow operator to access data members and call member functions.
The syntax is
pointer->memberName
to access data members or call member functions.
Example with pointers:
class MyClass {
public:
int myData;
void myFunction() {
// Code for the function
}
};
int main() {
MyClass obj;
MyClass* ptr = &obj; // Creating a pointer to MyClass object
ptr->myData = 42; // Accessing data member using arrow operator
ptr->myFunction(); // Calling member function using arrow operator
return 0;
}
In this example, ptr
is a pointer to a MyClass
object. The arrow operator allows us to access the object’s data members and call its member functions using the pointer.
Using the correct operator (dot or arrow) is essential depending on whether you are working with an object or a pointer to an object. The dot operator is used with objects, and the arrow operator is used with pointers to objects.
class Person {
public:
// Data members
std::string name;
int age;
// Constructor with parameters
Person(const std::string& newName, int newAge) {
name = newName;
age = newAge;
}
};
int main() {
// Create an object of the Person class using the constructor
Person person("Alice", 30);
return 0;
}
In this example, the Person
class has a constructor that takes two parameters (newName
and newAge
) to initialize the name
and age
data members of the object. When you create an object using the constructor, it automatically sets the provided values for the data members.
Constructors play a crucial role in ensuring that objects are properly initialized and ready for use, making them an essential aspect of object-oriented programming in C++.
class Rectangle {
public:
int width;
int height;
// Parameterized constructor
Rectangle(int initialWidth, int initialHeight) {
width = initialWidth;
height = initialHeight;
}
};
int main() {
// Create an object of the Rectangle class using the constructor
Rectangle myRectangle(5, 10);
return 0;
}
In this example, the Rectangle
class has a parameterized constructor that takes two arguments (initialWidth
and initialHeight
) to initialize the width
and height
data members of the object myRectangle
.
Constructors play a crucial role in setting up the initial state of objects, making them an essential aspect of object-oriented programming in C++.
#include
#include
class Student {
private:
std::string name;
int age;
int rollNumber;
public:
// Constructor (parameterized)
Student(const std::string& newName, int newAge, int newRollNumber) {
name = newName;
age = newAge;
rollNumber = newRollNumber;
}
// Member function to set the name
void setName(const std::string& newName) {
name = newName;
}
// Member function to set the age
void setAge(int newAge) {
age = newAge;
}
// Member function to set the roll number
void setRollNumber(int newRollNumber) {
rollNumber = newRollNumber;
}
// Member function to get the name
std::string getName() const {
return name;
}
// Member function to get the age
int getAge() const {
return age;
}
// Member function to get the roll number
int getRollNumber() const {
return rollNumber;
}
};
int main() {
// Create an object of the Student class using the constructor
Student student1("John Doe", 20, 12345);
// Use member functions to access and modify the data members
std::cout << "Name: " << student1.getName() << std::endl;
std::cout << "Age: " << student1.getAge() << std::endl;
std::cout << "Roll Number: " << student1.getRollNumber() << std::endl;
student1.setAge(21);
std::cout << "Updated Age: " << student1.getAge() << std::endl;
return 0;
}
In this example, we define a Student
class with private data members (name
, age
, and rollNumber
) and public member functions (setName
, setAge
, setRollNumber
, getName
, getAge
, and getRollNumber
). We also have a constructor to initialize the data members when an object is created.
The main
function demonstrates how to create an object of the Student
class, set and retrieve its data members using the member functions, and modify the age using the setAge
member function.
When you run this program, it will output:
Name: John Doe
Age: 20
Roll Number: 12345
Updated Age: 21
This shows the basic structure of a class in C++, its data members, member functions, and how to work with objects of the class to manipulate and access its properties.




Popular Category
Topics for You
Go through our study material. Your Job is awaiting.