C++ OOP

🧠 C++ OOP (Object-Oriented Programming)

Object-Oriented Programming (OOP) in C++ is a programming paradigm based on the concept of objects that contain data (attributes) and functions (methods).
It helps in writing clean, modular, reusable, and maintainable code.


πŸ”Ή 1. Core Concepts of OOP in C++

There are four main pillars of OOP:

  1. Class

  2. Object

  3. Encapsulation

  4. Inheritance

  5. Polymorphism

  6. Abstraction

(Usually taught as 4 pillars, but abstraction & encapsulation are often separated.)


πŸ”Ή 2. Class

A class is a blueprint/template for creating objects.

class Student {
public:
int id;
string name;

void display() {
cout << id << " " << name;
}
};


πŸ”Ή 3. Object

An object is an instance of a class.

int main() {
Student s1;
s1.id = 101;
s1.name = "Sanjit";
s1.display();
}

πŸ”Ή 4. Encapsulation

Encapsulation means binding data and methods together and restricting direct access.

class Account {
private:
int balance;

public:
void setBalance(int b) {
balance = b;
}

int getBalance() {
return balance;
}
};

βœ” Data is protected
βœ” Access through public methods


πŸ”Ή 5. Abstraction

Abstraction means showing only essential details and hiding implementation.

class Car {
public:
void start() {
cout << "Car started";
}
};

User does not know how the engine startsβ€”only that it starts.


πŸ”Ή 6. Inheritance

Inheritance allows a class to acquire properties of another class.

class Animal {
public:
void eat() {
cout << "Eating";
}
};

class Dog : public Animal {
public:
void bark() {
cout << "Barking";
}
};

Dog d;
d.eat();
d.bark();

βœ” Code reusability
βœ” Parent β†’ Child relationship


πŸ”Ή 7. Polymorphism

Polymorphism means many forms.

Example: Function Overriding

class Animal {
public:
virtual void sound() {
cout << "Animal sound";
}
};

class Dog : public Animal {
public:
void sound() {
cout << "Dog barks";
}
};

Animal* a;
Dog d;
a = &d;
a->sound(); // Dog barks

βœ” Achieved using virtual functions


πŸ”Ή 8. Constructors and Destructors

Constructor

Called automatically when object is created.

class Demo {
public:
Demo() {
cout << "Constructor called";
}
};

Destructor

Called automatically when object is destroyed.

~Demo() {
cout << "Destructor called";
}

πŸ”Ή 9. Access Specifiers

Specifier Access
public Accessible everywhere
private Only inside class
protected Class + derived classes

πŸ”Ή 10. OOP vs Procedural Programming

OOP Procedural
Object-based Function-based
Better security Less secure
Reusable Less reusable
Complex projects Small programs

πŸ“Œ Real-Life Example (OOP Thinking)

  • Class β†’ Car

  • Object β†’ BMW, Audi

  • Encapsulation β†’ Engine hidden

  • Inheritance β†’ ElectricCar inherits Car

  • Polymorphism β†’ start() behaves differently


πŸ“Œ Summary

  • OOP is based on classes and objects

  • Improves reusability, security, maintainability

  • Key pillars: Encapsulation, Abstraction, Inheritance, Polymorphism

  • Core foundation of modern C++ programming

You may also like...