C++ Classes and Objects


  • The classes are the most important feature of C++ that leads to Object Oriented programming.
  • Class is a user defined data type, which holds its own data members and member functions, which can be accessed and used by creating instance of that class.
  • The variables inside class definition are called as data members and the functions are called member functions.
  • Class name must start with an uppercase letter. If class name is made of more than one word, then first letter of each word must be in uppercase. Example,
class Box {
double length; // Length of a box
double breadth; // Breadth of a box
double height; // Height of a box
  • Classes contain, data members and member functions, and the access of these data members and variable depends on the access specifiers.
  • Class's member functions can be defined inside the class definition or outside the class definition.
  • Class in C++ are similar to structures in C, the only difference being, class defaults to private access control, where as structure defaults to public.
  • All the features of OOPS, revolve around classes in C++. Inheritance, Encapsulation, Abstraction etc.
  • Objects of class holds separate copies of data members. We can create as many objects of a class as we need.


  • Objects are instances of class, which holds the data variables declared in class and the member functions work on these class objects.
  • Each object has different data variables.
  • Objects are initialised using special class functions calledConstructors. We will study about constructors later.
  • And whenever the object is out of its scope, another special class member function called Destructor is called, to release the memory reserved by the object. C++ doesn't have Automatic Garbage Collector like in JAVA, in C++ Destructor performs this task.

Box Box1; // Declare Box1 the first object of class Box;
Box Box2; // Declare Box2 the second object of class Box;


#include <iostream>

class Box
double length;   // Length of a box
double breadth;  // Breadth of a box
double height;   // Height of a box

void main( )
Box Box1;        // Declare Box1 object of class Box
Box Box2;        // Declare Box2 object of class Box

double volume = 0.0;     // Store the volume of a box here
// box 1 specification
Box1.height = 5.0; 
Box1.length = 5.0; 
Box1.breadth = 3.0;

// box 2 specification
Box2.height = 10.0;
Box2.length = 5.0;
Box2.breadth = 3.0;

// volume of box 1
volume = Box1.height * Box1.length * Box1.breadth;
cout << "Volume of Box1 : " << volume <<endl;

// volume of box 2
volume = Box2.height * Box2.length * Box2.breadth;
cout << "Volume of Box2 : " << volume <<endl;


Volume of Box1 : 75
Volume of Box2 : 150
  • It is important to note that private and protected members can not be accessed directly using direct member access operator (.).
  • We will learn how private and protected members can be accessed.

Subscribe us on Youtube

Share This Page on

Ask Question