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.

ConceptMeaning
EncapsulationWrapping data and functions together
AbstractionShowing only essential features
InheritanceOne class deriving from another
PolymorphismSame 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:

  • balance is 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

TypeDescription
SingleOne parent โ†’ one child
MultipleMultiple parents โ†’ one child
MultilevelGrandparent โ†’ parent โ†’ child
HierarchicalOne parent โ†’ many children
HybridCombination

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

AdvantageExplanation
Code reuseUsing inheritance
ModularityPrograms divided into classes
SecurityData hiding
Easy maintenanceSmall reusable modules
ScalabilityEasy 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