Try it here
Subscribe
Multiple Inheritance in C++

Multiple Inheritance in C++ and the Diamond Problem

multiple_inheritance_in_c++_and_the_diamond_problem

Unlike many other object-oriented programming languages, C++ allows multiple inheritance.

Multiple inheritance allows a child class to inherit from more than one parent class.

At the outset, it seems like a very useful feature. But a user needs to be mindful of a few gotchas while implementing this feature.

In the examples below, we will cover a few scenarios that one needs to be mindful about.

We'll start with a simple example to explain this concept in C++.

#include <iostream>

class LivingThing {
protected:
    void breathe() {
        std::cout << "I'm breathing as a living thing." << std::endl;
    }
};

class Animal : protected LivingThing {
protected:
    void breathe() {
        std::cout << "I'm breathing as an animal." << std::endl;
    }
};

class Reptile : protected LivingThing {
protected:
    void crawl() {
        std::cout << "I'm crawling as a reptile." << std::endl;
    }
};

class Snake : protected Animal, protected Reptile {
public:
    void breathe() {
        std::cout << "I'm breathing as a snake." << std::endl;
    }

    void crawl() {
        std::cout << "I'm crawling as a snake." << std::endl;
    }
};

int main() {
    Snake snake;

    snake.breathe();
    snake.crawl();

    return 0;
}

The output of this code is as follows:

I'm breathing as a snake.
I?m crawling as a snake.

In the example above, we have a base class named as LivingThing. The Animal and Reptile classes inherit from it. Only the Animal class overrides the method breathe(). The Snake class inherits from the Animal and Reptile classes. It overrides their methods. In the example above, there is no problem. Our code works well.

Now, we?ll add a bit of complexity.

What if Reptile class overrides the breathe() method?

The Snake class would not know which breathe() method to call. This is the "Diamond Problem".

Diamond Problem

Look at the code below. It is like the code in the example above, except that we have overridden the breathe() method in the Reptile class.

#include <iostream>

class LivingThing {
protected:
    void breathe() {
        std::cout << "I'm breathing as a living thing." << std::endl;
    }
};

class Animal : protected LivingThing {
protected:
    void breathe() {
        std::cout << "I'm breathing as an animal." << std::endl;
    }
};

class Reptile : protected LivingThing {
public:
    void breathe() {
        std::cout << "I'm breathing as a reptile." << std::endl;
    }

    void crawl() {
        std::cout << "I'm crawling as a reptile." << std::endl;
    }
};

class Snake : public Animal, public Reptile {

};

int main() {
    Snake snake;

    snake.breathe();
    snake.crawl();

    return 0;
}

If you try compiling the program, it won?t. You?ll be staring at an error message like the one below.

member 'breathe' found in multiple base classes of different types

The error is due to the "Diamond Problem" of multiple inheritance. The Snake class does not know which breathe() method to call.

In the first example, only the Animal class had overridden the breathe() method. The Reptile class had not. Hence, it wasn't very difficult for the Snake class to figure out which breathe() method to call. And the Snake class ended up calling the breathe() method of the Animal class.

In the second example, the Snake class inherits two breathe() methods. The breathe() method of the Animal and Reptile class. Since we haven?t overridden the breathe() method in the Snake class, there is ambiguity.

C++ has many powerful features such as multiple inheritance. But, it is not necessary that we use all the features it provides.

Virtual inheritance solves the classic "Diamond Problem". It ensures that the child class gets only a single instance of the common base class.

In other words, the Snake class will have only one instance of the LivingThing class. The Animal and Reptile classes share this instance.

This solves the compile time error we receive earlier. Classes derived from abstract classes must override the pure virtual functions defined in the base class.

Writer profile pic

Uk01 on Sep 13, 2020 at 12:05 am


If you like dEexams.com and would like to contribute, you can write your article here or mail your article to admin@deexams.com . See your article appearing on the dEexams.com main page and help others to learn.



Post Comment

Comments( 0)

×

Forgot Password

Please enter your email address below and we will send you information to change your password.