Menu
Coddy logo textTech

Multiple Inheritance

Part of the Object Oriented Programming section of Coddy's C++ journey — lesson 53 of 104.

C++ allows a class to inherit from multiple base classes simultaneously. This is called multiple inheritance and lets you combine features from different class hierarchies.

To inherit from multiple classes, separate them with commas:

class Printer {
public:
    void print() {
        std::cout << "Printing document" << std::endl;
    }
};

class Scanner {
public:
    void scan() {
        std::cout << "Scanning document" << std::endl;
    }
};

class MultiFunctionDevice : public Printer, public Scanner {
public:
    void copy() {
        scan();
        print();
    }
};

The MultiFunctionDevice class now has access to methods from both Printer and Scanner:

MultiFunctionDevice mfd;
mfd.print();  // From Printer
mfd.scan();   // From Scanner
mfd.copy();   // Its own method

Constructors are called in the order the base classes are listed in the inheritance declaration (left to right), and destructors in reverse order. You can pass arguments to each base constructor in the initialization list:

class MultiFunctionDevice : public Printer, public Scanner {
public:
    MultiFunctionDevice() : Printer(), Scanner() {}
};

A potential issue arises when both base classes have a member with the same name. This creates ambiguity, and you must use the scope resolution operator to specify which one you mean:

class A { public: void show() {} };
class B { public: void show() {} };
class C : public A, public B {};

C obj;
obj.A::show();  // Call A's version
obj.B::show();  // Call B's version

Multiple inheritance is powerful but can lead to complex situations, especially when base classes share a common ancestor. We'll explore that challenge in the next lesson.

challenge icon

Challenge

Easy

Let's build a smart home device system that combines capabilities from multiple base classes using multiple inheritance. You'll create devices that can both connect to WiFi and be controlled by voice, then combine these features into a unified smart speaker.

You'll organize your code across three files:

  • WiFiDevice.h: Define a WiFiDevice class that represents any device capable of connecting to a network:
    • A protected std::string networkName member
    • A constructor that takes a network name and stores it
    • A public connect() method that prints: Connecting to WiFi: <networkName>
    • A public status() method that prints: WiFi Status: Connected
  • VoiceControl.h: Define a VoiceControl class that represents any device with voice capabilities:
    • A protected std::string assistantName member
    • A constructor that takes an assistant name and stores it
    • A public listen() method that prints: <assistantName> is listening...
    • A public status() method that prints: Voice Status: Active
  • main.cpp: Read three inputs (each on a separate line):
    1. Device name (string)
    2. WiFi network name (string)
    3. Voice assistant name (string)

    Define a SmartSpeaker class that publicly inherits from both WiFiDevice and VoiceControl:

    • A private std::string deviceName member
    • A constructor that takes all three parameters, passes the network name to WiFiDevice and the assistant name to VoiceControl using the initialization list, and stores the device name
    • A powerOn() method that prints: <deviceName> powering on..., then calls connect() and listen()
    • A showAllStatus() method that prints the device name, then uses the scope resolution operator to call both status() methods to resolve the ambiguity

    Create a SmartSpeaker object with the input values. Call powerOn(), print a blank line, then call showAllStatus().

For example, with inputs Echo, HomeNetwork, and Alexa:

Echo powering on...
Connecting to WiFi: HomeNetwork
Alexa is listening...

Echo Status:
WiFi Status: Connected
Voice Status: Active

The showAllStatus() method should print the device name followed by " Status:" on one line, then call each base class's status() method using WiFiDevice::status() and VoiceControl::status() to avoid ambiguity since both base classes have a method with the same name.

Cheat sheet

C++ supports multiple inheritance, allowing a class to inherit from multiple base classes simultaneously:

class DerivedClass : public BaseClass1, public BaseClass2 {
    // Class body
};

The derived class has access to members from all base classes:

class Printer {
public:
    void print() {
        std::cout << "Printing document" << std::endl;
    }
};

class Scanner {
public:
    void scan() {
        std::cout << "Scanning document" << std::endl;
    }
};

class MultiFunctionDevice : public Printer, public Scanner {
public:
    void copy() {
        scan();   // From Scanner
        print();  // From Printer
    }
};

Base class constructors are called in the order they appear in the inheritance list (left to right). Use the initialization list to pass arguments to each base constructor:

class MultiFunctionDevice : public Printer, public Scanner {
public:
    MultiFunctionDevice() : Printer(), Scanner() {}
};

When multiple base classes have members with the same name, use the scope resolution operator to resolve ambiguity:

class A { public: void show() {} };
class B { public: void show() {} };
class C : public A, public B {};

C obj;
obj.A::show();  // Call A's version
obj.B::show();  // Call B's version

Try it yourself

#include <iostream>
#include <string>
#include "WiFiDevice.h"
#include "VoiceControl.h"

using namespace std;

// TODO: Define SmartSpeaker class that publicly inherits from both WiFiDevice and VoiceControl
// - Private member: deviceName (string)
// - Constructor: takes deviceName, networkName, assistantName
//   Use initialization list to pass networkName to WiFiDevice and assistantName to VoiceControl
// - powerOn() method: prints "<deviceName> powering on...", then calls connect() and listen()
// - showAllStatus() method: prints "<deviceName> Status:", then calls both status() methods
//   using scope resolution (WiFiDevice::status() and VoiceControl::status())

class SmartSpeaker {
    // TODO: Implement the SmartSpeaker class here
};

int main() {
    string deviceName, networkName, assistantName;
    getline(cin, deviceName);
    getline(cin, networkName);
    getline(cin, assistantName);

    // TODO: Create a SmartSpeaker object with the input values
    // TODO: Call powerOn()
    // TODO: Print a blank line
    // TODO: Call showAllStatus()

    return 0;
}
quiz iconTest yourself

This lesson includes a short quiz. Start the lesson to answer it and track your progress.

All lessons in Object Oriented Programming