In multiple inheritance, a class can be derived from multiple classes as shown below:

#include <iostream>
 
class Base1
{
    int m_base1_value{10};
 
public:
    int getValue()
    {
        return m_base1_value;
    }
};
 
class Base2
{
    int m_base2_value{20};
};
 
class Derived : public Base1, public Base2
{
    int m_derived_value{30};
};
 
int main(int argc, char const *argv[])
{
    Derived d;
 
    return 0;
}

Issues with multiple inheritance

The first issue with multiple inheritance is the member functions resolution. If base classes have same function, the compiler can’t decide what to pick and raise a compilation error. For example:

#include <iostream>
 
class Base1
{
    int m_base1_value{10};
 
public:
    int getValue()
    {
        return m_base1_value;
    }
};
 
class Base2
{
    int m_base2_value{20};
 
public:
    int getValue()
    {
        return m_base2_value;
    }
};
 
class Derived : public Base1, public Base2
{
    int m_derived_value{30};
};
 
int main(int argc, char const *argv[])
{
    Derived d;
 
    d.getValue(); // gives error
    return 0;
}

Although this problem can be solved by calling the function as:

d.Base1::getValue();
d.Base2::getValue();

However, it would still be a problem when there are many such base classes in multi hierarchical level.

Second issue is diamond problem. In diamond problem, a class inherit two base classes which in turns inherits from a common base class. The inheritance chain forms a diamond structure as shown below:

diamond problem in multiple inheritance

And the following program:

#include <iostream>
 
class Base
{
    int m_base1_value{10};
 
public:
    Base()
    {
        std::cout << "Creating base class" << '\n';
    }
 
    int getValue()
    {
        return m_base1_value;
    }
};
 
class Derived : public Base
{
    int m_derived_value{30};
};
 
class Derived2 : public Base
{
    int m_derived_value{30};
};
 
class FinalDerived : public Derived, public Derived2
{
    int m_derived_value{30};
};
 
int main(int argc, char const *argv[])
{
    FinalDerived d; // creates base class two times
    return 0;
}

If we run this program, we should see the Creating base class log two times which is an issue.

This diamond problem can be solved using virtual base classes.

References

  1. https://www.learncpp.com/cpp-tutorial/multiple-inheritance/