This repository contains examples and explanations of Object-Oriented Programming (OOP) concepts in C++.
- Object Creation (Stack vs. Heap)
- Constructors & Destructors
- Encapsulation & Abstraction
- Inheritance (Types & Examples)
- Diamond Problem & Virtual Inheritance
- Polymorphism (Compile-time & Run-time)
- Function & Operator Overloading
- Method Overriding
- Friend Functions
- Use of
const
in C++
C++ allows object creation in two ways:
Feature | Stack Allocation | Heap Allocation (new ) |
---|---|---|
Memory Location | Stack | Heap |
Lifetime | Automatic (destroyed when out of scope) | Manual (must use delete ) |
Performance | Faster | Slightly slower |
Syntax | ClassName obj; |
ClassName* obj = new ClassName(); |
π Use Stack Allocation for temporary objects.
π Use Heap Allocation when objects need to persist across multiple function calls.
Constructor: Special function that initializes an object.
Destructor: Cleans up when an object goes out of scope.
class Example {
public:
Example() { cout << "Constructor Called\n"; }
~Example() { cout << "Destructor Called\n"; }
};
π 3. Inheritance in C++
C++ supports different types of inheritance:
Type Description
Single One class inherits from another.
Multiple A class inherits from multiple base classes.
Multilevel Grandparent β Parent β Child.
Hierarchical Multiple classes inherit from a single base class.
Hybrid Combination of two or more types.
π Example: Single Inheritance
class Animal { public: void eat() { cout << "Eating...\n"; } };
class Dog : public Animal { public: void bark() { cout << "Barking...\n"; } };
int main() { Dog d; d.eat(); // Inherited d.bark(); // Own method }
π 4. Diamond Problem & Virtual Inheritance When a class inherits from two classes that share a common base class, ambiguity arises. Solution: Use virtual inheritance.
class A { public: void show() { cout << "Class A\n"; } }; class B : virtual public A {}; class C : virtual public A {}; class D : public B, public C {};
int main() { D obj; obj.show(); // No ambiguity }
π 5. Polymorphism Types:
Compile-time Polymorphism (Function & Operator Overloading) Run-time Polymorphism (Method Overriding using Virtual Functions)
π Function Overloading Example
class Math { public: int add(int a, int b) { return a + b; } double add(double a, double b) { return a + b; } }; π Method Overriding Example (Using Virtual Functions)
class Parent { public: virtual void show() { cout << "Parent class\n"; } };
class Child : public Parent { public: void show() override { cout << "Child class\n"; } };
int main() { Parent* p = new Child(); p->show(); // Calls Child's show() }
π 6. Operator Overloading (Adding Complex Numbers) Operator overloading allows user-defined types to use operators like +.
cpp Copy Edit class Complex { public: int real, imag; Complex(int r, int i) : real(r), imag(i) {}
Complex operator+(const Complex& c) {
return Complex(real + c.real, imag + c.imag);
}
void display() { cout << real << " + " << imag << "i\n"; }
};
int main() { Complex c1(2, 3), c2(4, 5); Complex c3 = c1 + c2; c3.display(); // Output: 6 + 8i }
π 7. Friend Function (Accessing Private Members) A friend function is not a class member but can access private data.
cpp Copy Edit class Sample { private: int num; public: Sample(int n) : num(n) {}
friend void display(const Sample& s);
};
void display(const Sample& s) { cout << "The number is: " << s.num << endl; }
π 8. Use of const in C++ const prevents modification of variables or methods.
cpp Copy Edit class Demo { private: int data; public: Demo(int d) : data(d) {}
void show() const { // Const function
cout << "Data: " << data << endl;
}
};
π Contributions π€
Feel free to fork this repository and contribute with improvements, examples, and explanations!
π License π
This project is open-source and available under the MIT License.
Let me know if you need **changes, additions, or formatting improvements**! π