Multiple Inheritance in C++
In this lesson: Multiple inheritance in C++, the diamond problem and virtual base classes — examples with output. In Hindi & English.
What is multiple inheritance?
Multiple inheritance lets a class derive from more than one base class at once, combining their members. You list the bases separated by commas. C++ allows it — many languages do not — but it comes with pitfalls to understand.
A basic example
#include <iostream>
using namespace std;
class Camera { public: void photo() { cout << "Click\n"; } };
class Phone { public: void call() { cout << "Ring\n"; } };
class SmartPhone : public Camera, public Phone { }; // both
int main() {
SmartPhone s;
s.photo(); // from Camera
s.call(); // from Phone
}Click
Ring
Name ambiguity
If two bases have a member with the same name, a plain call is ambiguous — qualify it with the class name:
class A { public: void show() { cout << "A\n"; } };
class B { public: void show() { cout << "B\n"; } };
class C : public A, public B { };
// C c; c.show(); // ERROR: ambiguous
// c.A::show(); // OK → A
// c.B::show(); // OK → BThe diamond problem
Trouble appears when two bases share a common ancestor. The ancestor gets inherited twice, giving two copies and ambiguity:
Device
/ \
Camera Phone
\ /
SmartPhone ← inherits Device twice!virtual base classes
The fix: make the middle classes inherit the common base as virtual, so only one shared copy exists:
class Device { public: int id = 0; };
class Camera : virtual public Device { }; // virtual
class Phone : virtual public Device { }; // virtual
class SmartPhone : public Camera, public Phone { };
// now SmartPhone has ONE Device, no ambiguity
// SmartPhone s; s.id = 5; // OKWhen to use it
- Combining independent capabilities (interface-like base classes).
- Rarely for ordinary reuse — prefer composition ("has-a") there.
- Always consider whether single inheritance or composition is simpler.
Multiple inheritance is legal and sometimes ideal, but easy to overcomplicate. Reach for it only when it clearly models the design.
Common mistakes
- Ignoring the diamond problem and getting duplicate base copies.
- Forgetting
virtualon the middle classes when a shared base is needed. - Leaving same-named members ambiguous instead of qualifying them.
- Using multiple inheritance where composition would be clearer.
Model a Clock and a Radio, then a ClockRadio that inherits both. Give each base a show() and call them unambiguously with the scope operator.
Summary
- Multiple inheritance derives from several base classes at once.
- Same-named members must be qualified with
Base::. - The diamond problem duplicates a shared ancestor.
virtualbase classes keep just one shared copy.- Use it sparingly; composition is often simpler.
Frequently Asked Questions
What is multiple inheritance in C++?
class C : public A, public B, so C gains the features of both A and B.What is the diamond problem in C++?
How does a virtual base class solve the diamond problem?
virtual when the middle classes inherit from it ensures only one shared copy of that base exists in the final class. This removes the duplication and the ambiguity, so members of the common base are accessed unambiguously.How do you resolve ambiguity in multiple inheritance in C++?
obj.Base1::show(). This tells the compiler exactly which inherited member you mean and clears the ambiguity.