C++ Classes and Objects
C++ Classes and Objects
int main(){
// create objects
Room room3, room4;
}
• Here, two objects room1 and room2 of the Room class are created
in sampleFunction(). Similarly, the objects room3 and room4 are
created in main().
• As we can see, we can create objects of a class in any function of
the program. We can also create objects of a class within the class
itself, or in other classes.
• Also, we can create as many objects as we want from a single
class.
C++ Access Data Members and Member Functions
• We can access the data members and member functions of
a class by using a . (dot) operator.
• For example, room2.calculateArea(); This will call the
calculateArea() function inside the Room class for object
room2.
• Similarly, the data members can be accessed as:
• room1.length = 5.5; In this case, it initializes the length
variable of room1 to 5.5.
// objects and class in C++ Programming int main() {
#include <iostream>
using namespace std; // create object of Room class
// create a class Room room1;
class Room { // assign values to data members
public: room1.length = 42.5;
double length; room1.breadth = 30.8;
double breadth; room1.height = 19.2;
double height;
double calculateArea() { // calculate and display the area and volume of the room
return length * breadth; cout << "Area of Room = " <<
} room1.calculateArea() << endl;
cout << "Volume of Room = " <<
double calculateVolume() { room1.calculateVolume() << endl;
return length * breadth * height;
} return 0;
}; }
• Note the use of the keyword public in the program. This
means the members are public and can be accessed
anywhere from the program.
• As per our needs, we can also create private members
using the private keyword.
• The private members of a class can only be accessed from
within the class.
• For example,
class Test {
private:
int a;
void function1() { }
public:
int b;
void function2() { }
}
Here, a and function1() are private. Thus they cannot be accessed from outside the
class.
On the other hand, b and function2() are accessible from everywhere in the program.
// public and private in C++ Class }
#include <iostream> double calculateVolume() {
using namespace std; return length * breadth * height;
class Room { }
private: };
double length; int main() {
double breadth; // create object of Room class
double height; Room room1;
public: // pass the values of private variables as
// function to initialize private variables arguments
void initData(double len, double brth, double hgt) room1.initData(42.5, 30.8, 19.2);
{ cout << "Area of Room = " <<
length = len; room1.calculateArea() << endl;
breadth = brth; cout << "Volume of Room = " <<
height = hgt; room1.calculateVolume() << endl;
} return 0;
double calculateArea() { }
return length * breadth;
C++ Constructors
A constructor is a special type of member function that is called automatically when
an object is created.
In C++, a constructor has the same name as that of the class and it does not have a
return type. For example,
class Wall {
public:
// create a constructor
Wall() {
// code
}
};
Here, the function Wall() is a constructor of the class Wall. Notice that the
constructor has the same name as the class, does not have a return type, and is
public
C++ Default Constructor