Object-Oriented Programming (OOP) is a programming paradigm that organizes programs using classes and objects. It helps developers create modular, reusable, and maintainable applications by modeling real-world entities in code.
- Organizes large programs into logical classes and objects
- Improves code reusability, scalability, and maintainability
- Supports important concepts like inheritance, encapsulation, abstraction, and polymorphism
Need for Object-Oriented Programming
Before Object-Oriented Programming (OOP), most programs followed a procedural approach where the focus was mainly on functions and step-by-step instructions. As programs became larger, managing and reusing code became difficult.
OOP in C++ was introduced to solve this problem by organizing programs into classes and objects, making applications easier to understand, reuse, and maintain.
- Structures programs into logical units using classes and objects
- Keeps related data and functions together through encapsulation
- Makes code modular, reusable, and scalable
- Prevents unauthorized access to data using access specifiers
- Supports the DRY (Don't Repeat Yourself) principle
Basic Concepts of Object-Oriented Programming
Object-Oriented Programming in C++ is built on concepts like classes, objects, inheritance, encapsulation, abstraction, and polymorphism that help in designing modular and reusable programs.

Class
A class is a user-defined blueprint used to create objects. It defines the data members and member functions that objects of the class will have. Using classes, multiple objects with similar properties and behavior can be created without rewriting code repeatedly.
Components of a Class
- Access Specifiers: Control accessibility using public, private, and protected
- Class Name: The name used to identify the class
- Data Members: Variables declared inside the class
- Member Functions: Functions that define object behavior

#include <iostream>
using namespace std;
class Student {
public:
string name;
int age;
void display() {
cout << name << endl;
}
};
int main() {
// Create object
Student s1;
// Assign values
s1.name = "Geeksforgeeks";
// Call function
s1.display();
return 0;
}
Output
Geeksforgeeks
Explanation: In this example, Student is a class and s1 is an object of that class. The object accesses the data member name and calls the member function display().
Object
An Object is a basic unit of Object-Oriented Programming that represents real-life entities. A typical C++ program creates many objects, which interact with each other by invoking methods. The objects are what perform your code, they are the part of your code visible to the user. An object mainly consists of:
- State: It is represented by the data members (attributes) of an object. It also reflects the properties of an object.
- Member Function: A member function is a collection of statements that perform some specific task and may return the result to the caller.
- Behavior: It is represented by the member functions of an object. It also reflects the response of an object to other objects.
- Identity: It is a unique name or reference given to an object that enables it to interact with other objects.
#include <iostream>
#include <string>
using namespace std;
class Employee {
// Instance variables
private:
string name;
float salary;
public:
// Constructor
Employee(string name, float salary) {
this->name = name;
this->salary = salary;
}
// getters method
string getName() { return name; }
float getSalary() { return salary; }
// setters method
void setName(string name) { this->name = name; }
void setSalary(float salary) { this->salary = salary; }
// Instance method
void displayDetails() {
cout << "Employee: " << name << endl;
cout << "Salary: " << salary << endl;
}
};
int main() {
Employee emp("Geek", 10000.0f);
emp.displayDetails();
return 0;
}
Output
Employee: Geek Salary: 10000
Explanation: Here, emp is an object of the Employee class. The constructor initializes object data, and the member function displays employee details.
Four Pillars of OOP in C++:
Abstraction
Abstraction in C++ is the process of hiding the implementation details and only showing the essential details or features to the user. It allows to focus on what an object does rather than how it does it. In C++ abstraction is achieved using abstract classes (classes that have at least one pure virtual function).
#include <iostream>
using namespace std;
class Shape {
public:
// Pure virtual function
virtual void draw() = 0;
};
class Circle : public Shape {
public:
void draw() {
cout << "Drawing Circle";
}
};
int main() {
Circle c;
c.draw();
return 0;
}
Output
Drawing Circle
Explanation
- Shape is an abstract class because it contains a pure virtual function draw().
- The pure virtual function hides implementation details from the user.
- Circle inherits the Shape class and provides implementation for draw().
- The user only interacts with the essential functionality without knowing internal details.
Encapsulation
Encapsulation is the process of bundling data and methods into a single unit (class) and restricting direct access to some of its components. It acts as a protective shield for the data.
- Data is hidden from other classes and accessed only through public methods.
- Achieved by declaring class variables as private and providing public getter/setter methods.
- Ensures data integrity and controlled access.

#include <iostream>
using namespace std;
class Student {
private:
int marks;
public:
void setMarks(int m) {
marks = m;
}
int getMarks() {
return marks;
}
};
int main() {
Student s;
s.setMarks(90);
cout << s.getMarks();
return 0;
}
Output
90
Explanation: The data member marks is private and can only be accessed using public member functions.
Inheritance
Inheritance is a mechanism in C++ where a class (derived) acquires the properties and behaviors of another class (base), forming an "is-a" relationship.
- Enables code reusability by inheriting data members and methods from the base class.
- Achieved using : followed by an access specifier (public, private, protected).
- Example: Dog, Cat, Cow can be derived classes of the Animal base class.

#include <iostream>
using namespace std;
class Animal {
public:
void sound() {
cout << "Animal makes sound";
}
};
class Dog : public Animal {
};
int main() {
Dog d;
d.sound();
return 0;
}
Output
Animal makes sound
Explanation: The Dog class inherits the sound() function from the Animal class.
Polymorphism
The word polymorphism means having many forms, and it comes from the Greek words poly (many) and morph (forms), this means one entity can take many forms. In C++, polymorphism allows the same method or object to behave differently based on the context, specially on the project's actual runtime class.

#include <iostream>
using namespace std;
class Shape {
public:
virtual void draw() {
cout << "Drawing Shape";
}
};
class Circle : public Shape {
public:
void draw() override {
cout << "Drawing Circle";
}
};
int main() {
Shape* s;
Circle c;
s = &c;
s->draw();
return 0;
}
Output
Drawing Circle
Explanation: The same function draw() behaves differently depending on the object type.
Advantages of OOP over Procedural Programming
Object-Oriented Programming provides several advantages compared to procedural programming.
- By using objects and classes, you can create reusable components, leading to less duplication and more efficient development.
- It provides a clear and logical structure, making the code easier to understand, maintain, and debug.
- OOP support the DRY (Don't Repeat Yourself) principle. This principle encourages minimizing code repetition, leading to cleaner, more maintainable code.
- By reusing existing code and creating modular components, OOP allows for quicker and more efficient application development.
Situations Where OOP May Not Be Suitable
Object-Oriented Programming may not be ideal in the following situations:
- The program is very small and simple.
- Minimizing code size is more important than modularity.
- Extremely low memory usage is required.
- High-performance low-level systems are being developed.