Loading...
C++ Inheritance Types

C++ Inheritance Types

In this tutorial, we will learn to use Inheritance Access Control (public, protected and private) in C++ with the help of examples.

Inheritance Types

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

In C++, we have 5 different types of Inheritance. Namely,

  1. Single Inheritance
  2. Multiple Inheritance
  3. Hierarchical Inheritance
  4. Multilevel Inheritance
  5. Hybrid Inheritance (also known as Virtual Inheritance)

1. Single Inheritance in C++

This is the simplest type of inheritance. In the single inheritance, one derived class can inherit from only one base class.
For example, the class Derived is inheriting from only one Class Base.

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

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


Example 1: C++ Single Inheritance

#include <iostream>
using namespace std;
class A
{
    public:
      void display(){
          cout<<"Base class A content.";
      }
};
// sub class derived from base class 
class B : public A
{

};

int main(){
    B obj;
    obj.display();
    return 0;
}

Output

Base class A content.

In this program, class B is derived from class A. The obj object of class B is defined in the main() function.

When the display() function is called, display() in class A is executed. It's because there is no display() function in class B.

The function also doesn't exist in class B, so the compiler looks for it in class A (as B is derived from A).


2. Multiple Inheritance in C++

Multiple Inheritance is a feature of C++ where a single derived class can inherit property from more than one base class. For example, as explained below, class Derived inherits property from both Class Base1 and Class Base2.

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

In C++ programming, a class can be derived from more than one parents. For example: A class Parrot is derived from base classes Animal and Bird. It makes sense because Parrot is a Animal as well as a Bird.


Example 2: C++ Multiple Inheritance in C++ Programming

#include <string>
using namespace std;

// First base class 
class Animal {
  public:
    Animal() {
      cout << "Animals can Run." << endl;
    }
};
// Second base class 
class Bird {
  public:
    Bird() {
      cout << "Birds can Fly." << endl;
    }
};
// sub class derived from two base classes 
class Parrot: public Animal, public Bird {

};
int main(){

    // creating object of sub class will invoke the constructor of base classes 
    Parrot b1;
    return 0;
}

Output

Animals can Run.
Birds can Fly.

Ambiguity in Multiple Inheritance

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

Suppose, two base classes have a same function which is not overridden in derived class.

If you try to call the function using the object of the derived class, compiler shows error. It's because compiler doesn't 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 using scope resolution function to specify which function to class either base1 or base2.

int main()
{
    obj.base1::someFunction( );  // Function of base1 class is called
    obj.base2::someFunction();   // Function of base2 class is called.
}

3. Hierarchical Inheritance in C++

In hierarchical inheritance, more than one sub class is inherited from a single base class. In this, all features that are common in child classes are included in the base class.

For example: Football, Cricket, Badminton are derived from Sports class.


Syntax of Hierarchical Inheritance

class base_class {
     ... .. ...
}

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

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

Example 3: C++ Program to implement Hierarchical Inheritance

#include <iostream>
using namespace std;

// base class 
class Animal
{
    public:
        Animal(){
            cout<<"Animals Can Run.";
        }
};
// First Sub class 
class Dog : public Animal
{

};
// Second Sub class 
class Cat : public Animal
{

};

int main(){
    Cat obj1;
    Dog obj;2

    return 0;
}

Output

Animals Can Run.
Animals Can Run.

4. Multilevel Inheritance in C++

In multilevel inheritance, the derived class inherits property from another derived class.
For example, class B inherits from class A and class C inherits property from class B.

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

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


Example 4: C++ Multilevel Inheritance

#include <iostream>
using namespace std;

class Animal
{
    public:
    Animal()
      {
          cout<<"This is a Animal.";
      }
};

class fourLegs : public Animal
{
    public:
    fourLegs()
      {
          cout<<"Objects with 4 legs are Animals.";
      }
};

class Cow : public fourLegs
{
    public:
    Cow()
      {
          cout<<"Cow has 4 Legs.";
      }
};

int main()
{
    Cow obj;
    obj.display();
    return 0;
}

Output

This is a Animal.
Objects with 4 legs are Animals.
Cow has 4 Legs.

5. Hybrid (Virtual) Inheritance in C++

Hybrid inheritance is a combination of more than one type of inheritance (Combining Hierarchical inheritance and Multiple Inheritance.).
For example:- A child and parent class relationship that follows multiple and hierarchical inheritance both can be called hybrid inheritance.


Example 5: C++ Hybrid (Virtual) Inheritance

#include <iostream>
using namespace std;

class Animal{
    public:
    Animal()
      {
          cout<<"This is a Animal.";
      }
};

class Fare{
    public:
    Fare()
      {
          cout<<"Fare of Animal.";
      }
};

class Dog : public Animal
{

};

class Cat : public fourLegs, public Fare
{

};
int main(){

    Car obj2;
    return 0;
}

Output

This is a Animal.
Fare of Animals.

Next Tutorial

We hope that this tutorial helped you develop better understanding of the concept of Inheritance Types in C++.

Keep Learning : )

In the next tutorial, you'll learn about C++ Polymorphism.

- Related Topics