C++ Multiple Inheritance
🧬 C++ Multiple Inheritance
Multiple inheritance in C++ allows a class to inherit from more than one base class.
This means a derived class can combine features of multiple parent classes.
🔹 1. Basic Syntax
🔹 2. Simple Example
🔹 3. Ambiguity Problem
Occurs when both base classes have same method name.
🔹 4. Resolving Ambiguity (Scope Resolution)
🔹 5. Diamond Problem
❌ D has two copies of A
🔹 6. Solving Diamond Problem (Virtual Inheritance)
✔ Only one copy of A
🔹 7. Constructor Call Order
Output:
(Base classes are constructed left to right)
🔹 8. Destructor Call Order
Reverse of constructor:
🔹 9. When to Use Multiple Inheritance
✔ Combining independent features
✔ Mixin-like behavior
✔ Interfaces (abstract base classes)
❌ Avoid if it causes ambiguity or tight coupling
🔹 10. Multiple Inheritance vs Interfaces
Use abstract classes to mimic interfaces.
📌 Summary
-
A class can inherit from multiple base classes
-
Can cause ambiguity
-
Use scope resolution or virtual inheritance
-
Diamond problem is a common issue
-
Best used with abstract base classes
