Complete implementation of Object-Oriented Programming concepts in C++ with practical examples. Master the four pillars of OOP through hands-on code.
┌─────────────────────────────────────────────────────────────┐
│ │
│ 🔒 ENCAPSULATION 🎭 ABSTRACTION 🧬 INHERITANCE │
│ │
│ Hide Show Only Reuse Code │
│ Details Essentials & Extend │
│ │
│ 🔄 POLYMORPHISM │
│ Many Forms, One Interface │
│ │
└─────────────────────────────────────────────────────────────┘
| 🎨 Concept | 📝 Topics | 📄 File |
|---|---|---|
| 🏗️ Basics | Classes, Objects, Getters/Setters | clasobj.cpp |
| 🔒 Encapsulation | Private data, Access control | clasobj.cpp |
| ⚙️ Constructors | Default, Parameterized, Copy | constructor.cpp |
| 💾 Memory | Shallow vs Deep Copy, Destructors | shallowdeepcopy.cpp |
| 🧬 Inheritance | Base & Derived classes | inheritence.cpp |
| 🔄 Polymorphism | Overloading, Overriding, Virtual | polymorphism.cpp |
| 🎭 Abstraction | Pure Virtual, Abstract Classes | abstraction.cpp |
| 📌 Static | Static variables & objects | abstraction.cpp, staticobj.cpp |
g++ -std=c++11 filename.cpp -o output && ./output┌─────────────────────────┐
│ CLASS = Blueprint │
│ │
│ ┌───────────────┐ │
│ │ name │ │ ┌──────────────┐
│ │ cgpa │ │ →→ │ obj1: Prince │
│ │ phone │ │ │ obj2: Alice │
│ │ getInfo() │ │ │ obj3: Bob │
│ └───────────────┘ │ └──────────────┘
│ │ OBJECTS = Instances
└─────────────────────────┘
🔒 Encapsulation Principle:
┌───────────────────────────────────┐
│ Student Class │
│ │
│ 🔓 PUBLIC │
│ ├─ name │
│ └─ getPhone() │
│ │
│ 🔒 PRIVATE │
│ ├─ phone ────────┐ │
│ └─ userid │ │
│ │ │
│ Protected from │ │
│ outside access! ◄──┘ │
└───────────────────────────────────┘
┌─────────────────────────────────────────────────────────────┐
│ CONSTRUCTOR TYPES │
├─────────────────────────────────────────────────────────────┤
│ │
│ 📦 DEFAULT Car(); │
│ └─ No parameters, basic initialization │
│ │
│ 📦 PARAMETERIZED Car(name, color); │
│ └─ Initialize with specific values │
│ │
│ 📦 COPY Car(original); │
│ └─ Clone existing object │
│ │
│ 🗑️ DESTRUCTOR ~Car(); │
│ └─ Cleanup when object dies │
└─────────────────────────────────────────────────────────────┘
c1 c2
┌──────┐ ┌──────┐
│ name │ │ name │
│color │ │color │
│ ptr │──┐ ┌──│ ptr │
└──────┘ │ │ └──────┘
│ │
↓ ↓
┌─────────────┐
│ [memory] │ ← SHARED! Changes affect both ❌
└─────────────┘
c1 c2
┌──────┐ ┌──────┐
│ name │ │ name │
│color │ │color │
│ ptr │──┐ │ ptr │──┐
└──────┘ │ └──────┘ │
↓ ↓
┌─────────┐ ┌─────────┐
│memory 1 │ │memory 2 │ ← INDEPENDENT ✓
└─────────┘ └─────────┘
💡 Rule: Always use deep copy when your class has pointers!
┌──────────┐
│ Animal │
│ 🐾 │
└────┬─────┘
│
┌────────────┼────────────┐
↓ ↓ ↓
┌────────┐ ┌────────┐ ┌────────┐
│ Fish │ │ Bird │ │ Mammal │
│ 🐟 │ │ 🦅 │ │ 🦁 │
└────┬───┘ └────────┘ └────────┘
│
┌────┴────┐
↓ ↓
┌────────┐ ┌────────┐
│ Shark │ │ Tuna │
│ 🦈 │ │ 🐠 │
└────────┘ └────────┘
🎯 Benefit: Shark inherits eat() and breathe() from Animal!
┌─────────────────────────────────────────────────────────────┐
│ POLYMORPHISM TYPES │
├─────────────────────────────────────────────────────────────┤
│ │
│ ⚡ COMPILE-TIME │
│ │ │
│ ├─ Function Overloading │
│ │ show(int) vs show(string) │
│ │ │
│ └─ Operator Overloading │
│ n1 + n2 (custom + for Complex numbers) │
│ │
│ 🎭 RUNTIME │
│ │ │
│ ├─ Function Overriding │
│ │ Parent::show() → Child::show() │
│ │ │
│ └─ Virtual Functions │
│ Base *ptr = new Child(); │
│ ptr->hello(); ← Calls Child version! ✓ │
└─────────────────────────────────────────────────────────────┘
🔑 Virtual Keyword Magic:
WITHOUT virtual WITH virtual
Base *ptr Base *ptr
↓ ↓
┌────────┐ ┌────────┐
│ Base │ │ Child │
│hello() │ ← Always Base ❌ │hello() │ ← Correct! ✓
└────────┘ └────────┘
┌─────────────────────────────────────────────┐
│ ABSTRACT CLASS (Shape) │
│ │
│ virtual void draw() = 0; ← Pure Virtual │
│ │
│ ⚠️ Cannot create Shape objects! │
└──────────────┬──────────────────────────────┘
│
┌─────────┴─────────┐
↓ ↓
┌─────────┐ ┌─────────┐
│ Circle │ │ Square │
│ draw() │ │ draw() │ ← Must implement!
└─────────┘ └─────────┘
✓ ✓
🎯 Purpose: Force all shapes to have a draw() method
Example Objects Static Variable
┌──────┐
│ obj1 │───┐ ┌──────────┐
├──────┤ │ │ │
│ obj2 │───┼─────► ALL SHARE ────────►│ x = 5 │
├──────┤ │ │ │
│ obj3 │───┘ └──────────┘
└──────┘
ONE COPY FOR ALL!
obj1.x = 5;
cout << obj2.x; // Prints 5 ✓
cout << obj3.x; // Prints 5 ✓
┌──────────┬─────────────┬───────────────┬──────────────┐
│ Modifier │ Same Class │ Derived Class │ Outside │
├──────────┼─────────────┼───────────────┼──────────────┤
│ public │ ✓ │ ✓ │ ✓ │
│protected │ ✓ │ ✓ │ ✗ │
│ private │ ✓ │ ✗ │ ✗ │
└──────────┴─────────────┴───────────────┴──────────────┘
┌────────────────┬──────────────┬──────────────┐
│ Feature │ Constructor │ Destructor │
├────────────────┼──────────────┼──────────────┤
│ Name │ ClassName │ ~ClassName │
│ Parameters │ Yes │ No │
│ Overloading │ Yes │ No │
│ Called │ At birth 🐣 │ At death ⚰️ │
└────────────────┴──────────────┴──────────────┘
┌──────────────┬─────────────────┬─────────────────┐
│ Feature │ Overloading │ Overriding │
├──────────────┼─────────────────┼─────────────────┤
│ Where │ Same class │ Parent-Child │
│ Parameters │ Different │ Same │
│ When │ Compile-time ⚡ │ Runtime 🎭 │
│ Virtual │ Not needed │ Recommended │
└──────────────┴─────────────────┴─────────────────┘
✅ Make data private, use getters/setters
✅ Initialize all members in constructors
✅ Deep copy for classes with pointers
✅ Virtual destructors for base classes
✅ Mark const methods as const
✅ Follow RAII: Constructor allocates, Destructor deallocates
📂 OOP-CPP/
│
├─ 🏗️ clasobj.cpp ................. Classes & Encapsulation
├─ ⚙️ constructor.cpp ............. All Constructor Types
├─ 💾 shallowdeepcopy.cpp ......... Memory Management
├─ 🧬 inheritence.cpp .............. Code Reusability
├─ 🔄 polymorphism.cpp ............. Multiple Forms
├─ 🎭 abstraction.cpp .............. Interfaces & Static
└─ 📌 staticobj.cpp ................ Static Lifetime Demo
1. START HERE → clasobj.cpp
↓
2. INITIALIZATION → constructor.cpp
↓
3. MEMORY MGMT → shallowdeepcopy.cpp
↓
4. CODE REUSE → inheritence.cpp
↓
5. FLEXIBILITY → polymorphism.cpp
↓
6. DESIGN → abstraction.cpp
↓
7. ADVANCED → staticobj.cpp
- 📖 Learn C++ - Comprehensive guide
- 🎨 Design Patterns - Real-world OOP
- 💻 GeeksforGeeks - Practice problems
If this helped you understand OOP, give it a star! 🌟
Built with 💙 for students mastering C++ fundamentals