English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

C++ Multiple, multi-level, and hierarchical inheritance

In this article, you will learn about C ++Different inheritance models in programming: multiple inheritance with examples, multi-level and hierarchical inheritance.

InheritanceIt is one of the core features of object-oriented programming languages. It allows software developers to derive a new class from an existing class. The derived class inherits the functions of the base class (existing class).

C ++There are various inheritance models in programming.

C ++Multiple inheritance

In C ++In programming, not only can a class be derived from a base class, but a class can also be derived from a derived class. This form of inheritance is called multiple inheritance.

class A
{ 
... .. ... 
};
class B: public A
{
... .. ...
};
class C: public B
{
... ... ...
};

Here, class B is derived from the base class A, and class C is derived from the derived class B.

Example1:C ++Multiple inheritance

#include <iostream>
using namespace std;
class A
{
    public:
      void display()
      {
          cout << "Content of the base class.";
      }
};
class B : public A
{
};
class C : public B
{
 
};
int main()
{
    C obj;
    obj.display();
    return 0;
}

Output result

Content of the base class.

In this program, class C is derived from class B (which is derived from the base class A).

The obj object of class C is defined in the main() function.

When the display() function is called, the display() in class A will be executed. This is because there is no display() function in class C and B.

The compiler first looks for the display() function in class C. Since this function does not exist in class C, it will look for the function in class B (because C is derived from B).

This display() function does not exist in class B either, so the compiler looks for it in class A (because B is derived from A).

If there is a display() function in C, the compiler will override the display() function of class A (becauseMember function overriding).

C ++multiple inheritance

In C ++In programming, a class can derive from multiple parent classes. For example: the Bat class is derived from the Mammal and WingedAnimal base classes. This makes sense because bats are both mammals (Mammal) and winged animals (WingedAnimal).

Example2:C ++Multiple inheritance in programming

#include <iostream>
using namespace std;
class Mammal {
  public:
    Mammal()
    {
      cout << "Mammals can be born directly." << endl;
    }
};
class WingedAnimal {
  public:
    WingedAnimal()
    {
      cout << "Winged animals can flap their wings." << endl;
    }
};
class Bat : public Mammal, public WingedAnimal {
};
int main()
{
    Bat b1;
    return 0;
}

Output result

Mammals can be born directly.
Winged animals can flap their wings.

Ambiguity in Multiple Inheritance

The most obvious problem with multiple inheritance occurs during function overriding.

Assume that two base classes have the same function, but the function is not rewritten in the derived class.

If the compiler tries to call this function using an object of the derived class, it will display an error. This is because the compiler does not know which function to call. For example,

class base1
{
  public:
     void someFunction( )
     { .... ... .... }  
};
class base2
{
    void someFunction( )
     { .... ... .... } 
};
class derived : public base1, public base2
{
    
};
int main()
{
    derived obj;
    obj.someFunction() // Error!  
}

This problem can be solved by using the scope resolution operator to specify which function belongs to base1or base2to solve

int main()
{
    obj.base1::someFunction( );  // Call base1Class functions
    obj.base2::someFunction();   // Call base2Class functions
}

C ++Hierarchical inheritance

If multiple classes inherit from a base class, it is called hierarchical inheritance. In hierarchical inheritance, all common functions in the subclass are included in the base class.

For example: physics, chemistry, and biology all come from science classes.

Syntax of hierarchical inheritance

class base_class {
     ... .. ...
}
class first_derived_class: public base_class {
     ... .. ...
}
class second_derived_class: public base_class {
     ... .. ...
}
class third_derived_class: public base_class {
     ... .. ...
}