Free tutorials & notes in Hindi & English · Clean code examples · Mobile friendly learning
🟣 OOP  ·  Lesson 46

Multiple Inheritance

Written and reviewed by · Senior IT Faculty · 15+ years’ experience

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

C++
#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
}
Output:
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:

C++
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 → B

The diamond problem

Trouble appears when two bases share a common ancestor. The ancestor gets inherited twice, giving two copies and ambiguity:

The diamond
      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:

C++
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;   // OK

When 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.
💡 Keep it simple

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 virtual on 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.
🏋️ Practice

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.
  • virtual base classes keep just one shared copy.
  • Use it sparingly; composition is often simpler.

Frequently Asked Questions

What is multiple inheritance in C++?
Multiple inheritance is when a class derives from more than one base class at once, inheriting members from all of them. You list the bases separated by commas, such as class C : public A, public B, so C gains the features of both A and B.
What is the diamond problem in C++?
The diamond problem happens when a class inherits from two classes that both derive from a common base, so the shared base is inherited twice. This creates two copies of the base's members and ambiguity about which one to use. It is the main difficulty of multiple inheritance.
How does a virtual base class solve the diamond problem?
Declaring the common base as 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++?
If two base classes have a member with the same name, you qualify the call with the class name and scope operator, such as obj.Base1::show(). This tells the compiler exactly which inherited member you mean and clears the ambiguity.
Is multiple inheritance recommended in C++?
Multiple inheritance is powerful but can make designs complex and introduce the diamond problem, so it is used sparingly. It works well for combining independent capabilities, such as interface-like classes, but composition is often preferred for ordinary code reuse.

Multiple inheritance क्या है?

Multiple inheritance किसी class को एक साथ एक से ज़्यादा base class से derive होने देती है, उनके members जोड़ते हुए। आप bases को commas से अलग सूचीबद्ध करते हैं। C++ इसकी अनुमति देता है — कई भाषाएँ नहीं देतीं — पर इसके साथ समझने योग्य कठिनाइयाँ आती हैं।

एक बुनियादी उदाहरण

C++
#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 { };  // dono

int main() {
    SmartPhone s;
    s.photo();   // Camera se
    s.call();    // Phone se
}
Output:
Click
Ring

नाम ambiguity

अगर दो bases का एक ही नाम वाला member हो, सादा call ambiguous है — इसे class नाम से qualify करें:

C++
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 → B

Diamond problem

दिक्कत तब आती है जब दो bases एक साझा पूर्वज साझा करें। पूर्वज दो बार inherit होता है, दो copies और ambiguity देते हुए:

Diamond
      Device
      /    \
  Camera   Phone
      \    /
    SmartPhone   ← Device do baar inherit hota hai!

virtual base classes

हल: बीच की classes को साझा base virtual रूप से inherit कराएँ, ताकि केवल एक साझा copy हो:

C++
class Device { public: int id = 0; };
class Camera : virtual public Device { };   // virtual
class Phone  : virtual public Device { };   // virtual
class SmartPhone : public Camera, public Phone { };
// ab SmartPhone me EK Device, koi ambiguity nahi
// SmartPhone s; s.id = 5;   // OK

इसे कब इस्तेमाल करें

  • स्वतंत्र क्षमताओं को जोड़ना (interface-जैसी base classes)।
  • सामान्य पुनः-इस्तेमाल के लिए शायद ही — वहाँ composition ("has-a") पसंद करें।
  • हमेशा विचार करें कि single inheritance या composition सरल है या नहीं।
💡 सरल रखें

Multiple inheritance वैध और कभी आदर्श है, पर अति-जटिल करना आसान है। इसे केवल तब चुनें जब यह स्पष्ट रूप से design मॉडल करे।

आम गलतियाँ

  • Diamond problem अनदेखा करके duplicate base copies पाना।
  • साझा base चाहिए होने पर बीच की classes पर virtual भूलना।
  • Same-नाम members को qualify करने के बजाय ambiguous छोड़ना।
  • Multiple inheritance वहाँ इस्तेमाल करना जहाँ composition साफ़ होता।
🏋️ अभ्यास

एक Clock और एक Radio मॉडल करें, फिर एक ClockRadio जो दोनों inherit करे। हर base को एक show() दें और उन्हें scope operator से स्पष्ट रूप से call करें।

सारांश

  • Multiple inheritance एक साथ कई base classes से derive होती है।
  • Same-नाम members को Base:: से qualify करना ज़रूरी है।
  • Diamond problem साझा पूर्वज को दोहराता है।
  • virtual base classes केवल एक साझा copy रखती हैं।
  • इसे कम इस्तेमाल करें; composition अक्सर सरल है।
← Back to C++ Tutorial
🔗

Share this topic with a friend

यह topic किसी दोस्त को भेजें

Found it useful? Send it to a classmate learning the same thing.

अच्छा लगा? जो दोस्त यही सीख रहा है, उसे भेज दीजिए।

💻 Live Code Editor

This page's programs are ready here — run them, edit them, and learn. No installation needed.
Powered by OneCompiler. The code loads into the editor automatically — press Run to see the output. If the editor does not open, open it in a new tab.