OOPs Concept in C++

OOPs Concept in C++: The major purpose of C++ programming is to introduce the OOPs (Object Oriented Programming language) concept in C++ language. Object-oriented programming is a paradigm in which it provides new concepts like inheritance, data binding, polymorphism, etc; And a programming paradigm where everything is represented as an object is known as “object-oriented programming language“. Smalltalk is considered as the first truly object-oriented programming language.

OOPs Concept in C++

The main aim of OOP is to bind together the data and the functions that operate on them so that no other part of the code can access this data except that function. The object-oriented paradigm (or) a methodology to design a program using classes and objects. So it simplifies the software development and maintenance by using some concepts as follow:

  • Object
  • Class
  • Inheritance
  • Polymorphism
  • Abstraction
  • Encapsulation

oops concept in c++

C++ Objects

Objects are nothing but the instances of the class, which holds the data variable declared in class and the member functions work on these class objects. And each object has a different variable. While the objects are initialized using special functions called “constructors”. And if your object is out of scope then the other special class member function called “Destructor” is called to release the memory reserved by the object. And in C++ we are not having automatic garbage collector like JAVA. So, destructor performs the task.

Syntax: class_name object_name;


class person 
   char name[20]; 
   int id; 
   void getdetails(){} 
  int main() 
   person p1;//p1 is a object 

C++ Classes

A class is nothing but a collection of objects is called “class. It is a user-defined data type which holds the data members and member functions of its own. While it can be accessed and used by creating the instance of the class. The variables inside class definition are called as data members and the functions are called member functions.

Facts about Class:

  • The class name must start with an uppercase letter(It is not mandatory)
  • If a class name is made of more than one word, the first letter of each word must be in uppercase. Ex: class Study, class StudyTonight, etc
  • The classes contain, data members and member functions, and the access of these data members and variable depends on the access specifiers.
  • Class 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, whereas structure defaults to public.
  • Objects of the class hold separate copies of data members. We can create as many objects of a class as we need.


Class class_name
Data Members;


class student
     int roll_number;
     char name[10];
     int func1()
         roll_number = 2;
         return roll_number;

Accessing the Data Members in C++

The data members can be defined as public, private and protected are accessed by using the direct member accessed operator (.).

Private: The class members that have been declared as private can be accessed only within the class. It is used for data hiding which is also one of the key features of object-oriented programming.

Protected: The class members that have been declared as protected can be accessed within the class and also in the derived class.

Public: The class members can be accessed from outside.


using namespace std;
class Box {
      double length;//Length of a box
      double breadth;//Breadth of a box
      double height; //Height of a box

int main() {
   Box Box1;//Declare Box1 of type Box
   Box Box2; //Declare Box2 of type Box
   double volume = 0.0; //Store the volume of a box here 
//box 1 specification
   Box1.height = 5.0; 
   Box1.length = 6.0; 
   Box1.breadth = 7.0;
//box 2 specification
   Box2.height = 10.0;
   Box2.length = 12.0;
   Box2.breadth = 13.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;
   return 0;


Volume of Box1: 210
Volume of Box2: 1560


class employee
int salary;
class developer : public employee
employee e;
void salary()
cout<<"Enter employee salary: ";
cin>>e.salary;   // access base class data member
cout<<"Employee salary: "<<e.salary;

void main()
developer obj;


Enter employee salary: 50000
Employee salary: 50000