Object Oriented Programming (OOP) in C++
Last Updated on: 11th Dec 2025 11:08:33 AM
Introduction to Object Oriented Programming
Object Oriented Programming (OOP) is a programming paradigm that organizes software design around objects and classes rather than functions and logic.
C++ is a powerful object-oriented programming language that allows developers to create real-world models using code.
In OOP, programs are built by modeling real-world entities, such as students, cars, bank accounts, or employees, and defining how they interact with each other.
Why OOP?
Traditional programming (procedural programming) focuses on functions and logic, while OOP focuses on data and behavior together, making programs:
-
Easier to understand
-
Easier to maintain
-
More scalable
-
More secure
-
Reusable
Real-Life Example of OOP
Think about a Car.
-
Data (Attributes):
-
Color
-
Brand
-
Speed
-
Fuel type
-
-
Behavior (Functions):
-
Start()
-
Stop()
-
Accelerate()
-
Brake()
-
In OOP, we represent this car as a class, and every real car becomes an object of that class.
Key Features of OOP in C++
OOP in C++ is based on six major concepts:
-
Class
-
Object
-
Encapsulation
-
Abstraction
-
Inheritance
-
Polymorphism
1. Class
A class is a user-defined blueprint or template used to create objects.
It defines properties (data members) and methods (member functions).
Syntax
class ClassName {
// data members
// member functions
};
Real-Life Example
A Student class:
-
Data: name, roll number, marks
-
Behavior: study(), appearExam()
Example Program
#include <iostream>
using namespace std;
class Student {
public:
string name;
int roll;
void display() {
cout << "Name: " << name << endl;
cout << "Roll: " << roll << endl;
}
};
int main() {
Student s1;
s1.name = "Rahul";
s1.roll = 101;
s1.display();
return 0;
}
2. Object
An object is a real-world instance of a class.
It represents actual values stored in memory.
Real-Life Example
-
Class: Car
-
Objects: BMW, Audi, Tesla
Each object has its own data values but shares the same structure.
3. Encapsulation
Encapsulation means binding data and functions together into a single unit (class) and protecting data from direct access.
Encapsulation helps in:
-
Data security
-
Controlled access
-
Better code organization
Access Specifiers in C++
| Specifier | Meaning |
|---|---|
private |
Data accessible only within the class |
protected |
Accessible in derived classes |
public |
Accessible from anywhere |
Real-Life Example
Bank Account:
-
Balance should not be directly modified
-
Only through deposit() or withdraw()
Example
class Bank {
private:
int balance = 1000;
public:
void deposit(int amt) {
balance += amt;
}
void showBalance() {
cout << "Balance: " << balance << endl;
}
};
4. Abstraction
Abstraction means hiding internal implementation details and showing only essential features to the user.
Real-Life Example
-
Driving a car:
-
You use steering, accelerator, brake
-
You don’t know engine internals
-
Achieved in C++ Using:
-
Abstract classes
-
Interfaces (using pure virtual functions)
Example
class Vehicle {
public:
virtual void start() = 0; // pure virtual function
};
class Bike : public Vehicle {
public:
void start() {
cout << "Bike started" << endl;
}
};
5. Inheritance
Inheritance allows one class (child class) to inherit properties and behavior from another class (parent class).
Benefits
-
Code reusability
-
Faster development
-
Hierarchical classification
Types of Inheritance in C++
-
Single
-
Multiple
-
Multilevel
-
Hierarchical
-
Hybrid
Real-Life Example
-
Parent: Employee
-
Child: Manager
Example
class Employee {
public:
int salary = 30000;
};
class Manager : public Employee {
public:
int bonus = 10000;
};
6. Polymorphism
Polymorphism means one function, many forms.
It allows the same function name to behave differently based on context.
Types of Polymorphism
1. Compile-time Polymorphism
-
Function Overloading
-
Operator Overloading
Example
int add(int a, int b) {
return a + b;
}
double add(double a, double b) {
return a + b;
}
2. Run-time Polymorphism
-
Achieved using function overriding
-
Requires inheritance and virtual functions
class Animal {
public:
virtual void sound() {
cout << "Animal sound" << endl;
}
};
class Dog : public Animal {
public:
void sound() {
cout << "Dog barks" << endl;
}
};
Constructor and Destructor
Constructor
-
Special function
-
Automatically called when object is created
-
Same name as class
class Demo {
public:
Demo() {
cout << "Constructor called" << endl;
}
};
Destructor
-
Automatically called when object is destroyed
-
Frees resources
~Demo() {
cout << "Destructor called" << endl;
}
this Pointer
this pointer holds the address of the calling object.
class Sample {
int x;
public:
void set(int x) {
this->x = x;
}
};
Static Members
Static members are shared among all objects of the class.
class Counter {
public:
static int count;
};
Advantages of OOP in C++
-
Better code reuse
-
Improved security
-
Easy to manage large programs
-
Real-world modeling
-
Reduced complexity
Disadvantages of OOP
-
Higher memory usage
-
Slower execution in some cases
-
Complex for small programs
Where OOP is Used?
-
Banking systems
-
E-commerce applications
-
Game development
-
Operating systems
-
Real-world simulations
Summary
Object Oriented Programming in C++ provides a structured, secure, and scalable way of writing programs.
By using classes, objects, encapsulation, abstraction, inheritance, and polymorphism, developers can create powerful applications that closely resemble real-world systems.
Keep practicing — you're doing amazing!
Happy Coding! ![]()