Free tutorials & notes in Hindi & English · Clean code examples 🧰 Free PDF & Image Tools →
🟣 OOP  ·  Lesson 46

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

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.
← 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.
Next topicPolymorphism in C++ Your progress: 46 / 81 (57%) · Lesson 47 / 81 Continue →

How was this lesson?

Your feedback helps us make the content better.

Write a review or suggestion (optional)

After approval, your name and city may appear publicly with your review. Please do not write your email or phone number.

▶ CodingEasily on YouTube

Learn with video lessons in easy Hindi

Featured video: RDBMS Part 3 – Class XI Commerce IT

▶ Subscribe – it’s free