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:
-
Class
-
Object
-
Encapsulation
-
Inheritance
-
Polymorphism
-
Abstraction
(Usually taught as 4 pillars, but abstraction & encapsulation are often separated.)
πΉ 2. Class
A class is a blueprint/template for creating objects.
πΉ 3. Object
An object is an instance of a class.
πΉ 4. Encapsulation
Encapsulation means binding data and methods together and restricting direct access.
β Data is protected
β Access through public methods
πΉ 5. Abstraction
Abstraction means showing only essential details and hiding implementation.
User does not know how the engine startsβonly that it starts.
πΉ 6. Inheritance
Inheritance allows a class to acquire properties of another class.
β Code reusability
β Parent β Child relationship
πΉ 7. Polymorphism
Polymorphism means many forms.
Example: Function Overriding
β Achieved using virtual functions
πΉ 8. Constructors and Destructors
Constructor
Called automatically when object is created.
Destructor
Called automatically when object is destroyed.
πΉ 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
