Object-Oriented Programming (OOP) is one of the most important programming paradigms used in C++, Java, Python, and many modern languages. It organizes software design around objects rather than functions and logic.
In simple terms:
๐ OOP models real-world entities (objects) in programs.
For example:
- Car
- Bank Account
- User
- File
- Network Connection
Each object has:
- Data (attributes / properties)
- Behavior (methods / functions)
1๏ธโฃ What is Object-Oriented Programming?
Object-Oriented Programming (OOP) is a programming approach where software is structured using classes and objects.
Example idea:
Real world:
Car
โโ color
โโ speed
โโ start()
In C++:
class Car
{
public:
string color;
int speed; void start()
{
cout << "Car started\n";
}
};
Object creation:
Car c1;
c1.color = "Red";
c1.speed = 100;
c1.start();
2๏ธโฃ Key Concepts of OOP
There are 4 main pillars of OOP.
| Concept | Meaning |
|---|---|
| Encapsulation | Wrapping data and functions together |
| Abstraction | Showing only essential features |
| Inheritance | One class deriving from another |
| Polymorphism | Same function behaving differently |
3๏ธโฃ Class and Object
Class
A class is a blueprint or template.
Example:
class Person
{
public:
string name;
int age; void speak()
{
cout << "Hello\n";
}
};
Object
An object is an instance of a class.
Person p1;
p1.name = "Venkat";
p1.age = 30;
p1.speak();
Memory is allocated only when objects are created.
4๏ธโฃ Encapsulation
Encapsulation means:
๐ Binding data and functions together in one unit.
Also used for data protection.
Example:
class BankAccount
{
private:
int balance;public:
void deposit(int amount)
{
balance += amount;
} int getBalance()
{
return balance;
}
};
Here:
balanceis private- Access only through functions
Benefits:
- Data safety
- Controlled access
5๏ธโฃ Abstraction
Abstraction means:
๐ Hiding complex implementation details.
Example:
When you drive a car:
You use:
start()
brake()
accelerate()
You do NOT see:
- engine process
- fuel injection
- internal mechanics
C++ Example:
class Printer
{
public:
void print()
{
cout << "Printing document\n";
}
};
User only calls:
printer.print();
6๏ธโฃ Inheritance
Inheritance means:
๐ One class acquires properties of another class.
Example:
Parent class:
class Animal
{
public:
void eat()
{
cout << "Animal eating\n";
}
};
Child class:
class Dog : public Animal
{
public:
void bark()
{
cout << "Dog barking\n";
}
};
Usage:
Dog d;
d.eat();
d.bark();
Types of Inheritance
| Type | Description |
|---|---|
| Single | One parent โ one child |
| Multiple | Multiple parents โ one child |
| Multilevel | Grandparent โ parent โ child |
| Hierarchical | One parent โ many children |
| Hybrid | Combination |
7๏ธโฃ Polymorphism
Polymorphism means:
๐ Same function name, different behavior.
Two types:
Compile-time polymorphism
Function overloading
class Math
{
public:
int add(int a, int b)
{
return a + b;
} double add(double a, double b)
{
return a + b;
}
};
Runtime polymorphism
Using virtual functions.
class Shape
{
public:
virtual void draw()
{
cout << "Drawing shape\n";
}
};class Circle : public Shape
{
public:
void draw()
{
cout << "Drawing circle\n";
}
};
Usage:
Shape* s;
Circle c;s = &c;
s->draw();
Output:
Drawing circle
8๏ธโฃ Constructors
Constructor initializes objects.
class Car
{
public:
Car()
{
cout << "Car created\n";
}
};
Runs automatically when object is created.
9๏ธโฃ Destructor
Destructor runs when object is destroyed.
class Car
{
public:
~Car()
{
cout << "Car destroyed\n";
}
};
๐ Advantages of OOP
| Advantage | Explanation |
|---|---|
| Code reuse | Using inheritance |
| Modularity | Programs divided into classes |
| Security | Data hiding |
| Easy maintenance | Small reusable modules |
| Scalability | Easy to extend systems |
1๏ธโฃ1๏ธโฃ OOP in Large Systems
Used in:
- Operating systems
- Game engines
- Banking software
- Web servers
- Network systems
- GUI applications
Example C++ systems:
- Web servers
- Database engines
- Network frameworks
1๏ธโฃ2๏ธโฃ Simple Real World Example
Bank system design:
Classes:
Customer
Account
Transaction
ATM
Bank
Each object interacts with others.
Example:
Customer -> Account -> Transaction
1๏ธโฃ3๏ธโฃ Example Full Program
#include <iostream>
using namespace std;class Car
{
private:
int speed;public:
void setSpeed(int s)
{
speed = s;
} void showSpeed()
{
cout << "Speed: " << speed << endl;
}
};int main()
{
Car c1; c1.setSpeed(120);
c1.showSpeed(); return 0;
}
Output
Speed: 120
โญ Summary
Object-Oriented Programming is based on:
Class
Object
Encapsulation
Abstraction
Inheritance
Polymorphism
It helps build:
- Large systems
- Maintainable software
- Reusable code
- Secure programs