Skip to content

Princekr267/Object-Oriented-Programming

Repository files navigation

Object-Oriented Programming in C++

C++ OOP Learning

Complete implementation of Object-Oriented Programming concepts in C++ with practical examples. Master the four pillars of OOP through hands-on code.


🎯 The Four Pillars

┌─────────────────────────────────────────────────────────────┐
│                                                             │
│  🔒 ENCAPSULATION    🎭 ABSTRACTION    🧬 INHERITANCE     │
│                                                             │
│         Hide          Show Only        Reuse Code           │
│       Details         Essentials       & Extend             │
│                                                             │
│                   🔄 POLYMORPHISM                          │
│                   Many Forms, One Interface                 │
│                                                             │
└─────────────────────────────────────────────────────────────┘

📚 What's Inside

🎨 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

🚀 Quick Start

g++ -std=c++11 filename.cpp -o output && ./output

🏗️ Core Concepts Visualized

1️⃣ Classes & Objects (clasobj.cpp)

┌─────────────────────────┐
│   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!  ◄──┘           │
└───────────────────────────────────┘

2️⃣ Constructors (constructor.cpp)

┌─────────────────────────────────────────────────────────────┐
│                    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                            │
└─────────────────────────────────────────────────────────────┘

3️⃣ Shallow vs Deep Copy (shallowdeepcopy.cpp)

⚠️ SHALLOW COPY (Default - Dangerous!)

   c1                    c2
┌──────┐              ┌──────┐
│ name │              │ name │
│color │              │color │
│ ptr  │──┐        ┌──│ ptr  │
└──────┘  │        │  └──────┘
          │        │
          ↓        ↓
      ┌─────────────┐
      │  [memory]   │  ← SHARED! Changes affect both ❌
      └─────────────┘

✅ DEEP COPY (Safe!)

   c1                    c2
┌──────┐              ┌──────┐
│ name │              │ name │
│color │              │color │
│ ptr  │──┐           │ ptr  │──┐
└──────┘  │           └──────┘  │
          ↓                     ↓
      ┌─────────┐          ┌─────────┐
      │memory 1 │          │memory 2 │  ← INDEPENDENT ✓
      └─────────┘          └─────────┘

💡 Rule: Always use deep copy when your class has pointers!


4️⃣ Inheritance (inheritence.cpp)

                    ┌──────────┐
                    │  Animal  │
                    │  🐾      │
                    └────┬─────┘
                         │
            ┌────────────┼────────────┐
            ↓            ↓            ↓
       ┌────────┐   ┌────────┐   ┌────────┐
       │  Fish  │   │  Bird  │   │ Mammal │
       │  🐟   │   │  🦅    │   │  🦁    │
       └────┬───┘   └────────┘   └────────┘
            │
       ┌────┴────┐
       ↓         ↓
   ┌────────┐ ┌────────┐
   │ Shark  │ │  Tuna  │
   │  🦈   │ │  🐠    │
   └────────┘ └────────┘

🎯 Benefit: Shark inherits eat() and breathe() from Animal!


5️⃣ Polymorphism (polymorphism.cpp)

🔄 The Power of "Many Forms"

┌─────────────────────────────────────────────────────────────┐
│                    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! ✓
└────────┘                  └────────┘

6️⃣ Abstraction (abstraction.cpp)

┌─────────────────────────────────────────────┐
│         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


7️⃣ Static Members (abstraction.cpp, staticobj.cpp)

     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 ✓

📊 Quick Reference

🔐 Access Modifiers

┌──────────┬─────────────┬───────────────┬──────────────┐
│ Modifier │ Same Class  │ Derived Class │ Outside      │
├──────────┼─────────────┼───────────────┼──────────────┤
│ public   │     ✓       │      ✓        │      ✓       │
│protected │     ✓       │      ✓        │      ✗       │
│ private  │     ✓       │      ✗        │      ✗       │
└──────────┴─────────────┴───────────────┴──────────────┘

⚙️ Constructor vs Destructor

┌────────────────┬──────────────┬──────────────┐
│    Feature     │ Constructor  │  Destructor  │
├────────────────┼──────────────┼──────────────┤
│ Name           │  ClassName   │ ~ClassName   │
│ Parameters     │     Yes      │      No      │
│ Overloading    │     Yes      │      No      │
│ Called         │  At birth 🐣 │ At death ⚰️  │
└────────────────┴──────────────┴──────────────┘

🔄 Overloading vs Overriding

┌──────────────┬─────────────────┬─────────────────┐
│   Feature    │   Overloading   │   Overriding    │
├──────────────┼─────────────────┼─────────────────┤
│ Where        │   Same class    │ Parent-Child    │
│ Parameters   │   Different     │    Same         │
│ When         │ Compile-time ⚡ │ Runtime 🎭      │
│ Virtual      │   Not needed    │   Recommended   │
└──────────────┴─────────────────┴─────────────────┘

🎯 Best Practices

✅ 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

📁 File Navigation

📂 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

🎓 Learning Path

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

📚 Resources


⭐ Support This Project

If this helped you understand OOP, give it a star! 🌟


Built with 💙 for students mastering C++ fundamentals

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages