Tutorial Study Image

Types of inheritance in C++


May 31, 2023, Learn eTutorial
230

C++ Inheritance

There are mainly five different types of inheritance in C++:

  • Single Inheritance
  • Multiple Inheritance
  • Multilevel Inheritance
  • Hybrid Inheritance
  • Hierarchical Inheritance

Single inheritance

Single inheritance mainly occurs whenever a derived class inherits only from the new base class.

C++ Inheritance

In the figure above, A is the base class and B is the derived class; in this case, the child class will inherit only one parent class.

Program for single inheritance


class Base {
  public:
    float salary = 1000;
};
class Derived: public Base {
  public:      float b
    void sum() {
      cout << " Total Salary is: " << (salary + bonus) << endl;
    }
};
int main() {
  Derived x;
  cout << "Your Salary will be:" << x.salary << endl;
  cout << "Your Bonus will be:" << x.bonus << endl;
  x.sum();
  return 0;
}

 

Output:


Your Salary will be:1000
Your Bonus will be:400
Total Salary is:1400

Explanation of the above program

In the preceding example, Base is the class name as well as it is considered the parent class, which contains the salary property with the value of 1000.

In the same way, there is another class called Derived, which is the offspring class that inherits the parent class's properties as well as having a property called a bonus with a value of 400.

Sum(), a function in the child class, is used to combine the salary and bonus. The "Derived" class which is also a child class, creates an object called "x" in the main function. By calling the object, the properties, as well as the sum function from the derived class, in order to add the salary and bonus just to produce the result as output.

Multiple Inheritance

Multiple inheritance is the term used to describe a derived class's (child class's) ability to inherit from more than one base class (parent class).

C++ Inheritance

A and B are base classes in the diagram above, and C is a derived class that derives from multiple base classes (parent class).

Program using multiple base classes


class Base1 {
  public:
    float salary = 1000;
};

class Base2 {
  public:     float b
};

class Derived: public Base1, public Base2 {
  public: 
    void sum() {
      cout << "Your Total Salary is: " << (salary + bonus) << endl;
    }
};
int main() {
  Derived x;
  x.sum();
  return 0;
}


 

Output:


Your Total Salary will be:1400

Explanation of the above program

Two base classes, Base1 and Base2, which have the properties of salary as well as bonus, are present in the abovementioned program. A class called "derived" will inherit from its Base1 and Base2 parent classes. The function sum in the derived class is used to return the total of the salary and bonus.

The sum() function, which will add bonus and salary and output the result, is called using an object named x that was formed from a derived class in the main function.

Diamond problem in Multiple Inheritance

C++ Inheritance

We might run into a diamond problem in multiple inheritances. Let's look at the picture above for an example: Since both "B" and "C" classes are inherited from the same Base class, "A," and "D" is inherited both from "B" and "C," this problem is known as the "diamond problem" since it results in the formation of a diamond shape.

In this case, the "B" and "C" classes will have the member variable of class "A" because they are just inherited from "A," because "D" is inherited just from the "B" and "C," it will also contain two copies of "A's" member variable, one from "B" and one from "C," thus causing misunderstanding and confusing the compiler as to which of the two copies of A's member variable should be used in D.

The Diamond Problem's solution

Virtual inheritance offers a solution to the diamond problem or issue. It is a strategy that assures that the second-level derivatives that are grandchildren inherit just one copy of the superclasses or base classes member variables. The class "A" member variable will only be copied once to classes "B" and "C" as a result of class "A" being declared as a virtual base class in the example above.

Multilevel Inheritance

Multilevel inheritance is the term used to describe when a derived (child) class simultaneously inherits the base class and serves as the base class (parent class) to another class. In multilevel inheritance, the number of levels, or derived classes, can be any number.

C++ Inheritance

In the figure above, class "B" is inherited from class "A," as well as from class "B" is now the base class (parent class) for class "C," which is inherited just from class "B." The property of Classes "A" and "B" is now inherited by Class "C."

An example program for multiple inheritances


class BaseClass {
  public:
    void print() {
      cout << " an example of  Multilevel Inheritance in C++";
    }
};

class DerivedClass: public BaseClass {};

//  class which is derived from another derived class
class DerivedClass2: public DerivedClass {};

int main() {
  DerivedClass2 Obj;
  Obj.print();
  return 0;
}

 

Output:


an example of  Multilevel Inheritance in C++

Explanation of the above program

There is a base class called BaseClass in the program mentioned above that includes the message. This is an illustration of multilevel inheritance; the base class (parent class) is inherited by the derived class named DerivedClass, and the subsequent derived class named DerivedClass2 is inherited by the previous derived class.

The fact that an object of DerivedClass2 with the name obj is generated in the main method and that this object calls the print function of the base class, the top-level parent class, demonstrates how DerivedClass2 inherits the BaseClass and DerivedClass properties.

Hierarchical Inheritance

Hierarchical inheritance is the process of more than one class deriving properties from a single base class.

C++ Inheritance

In the diagram above, "A" serves as the base class, while "B" and "C" are derived classes that derive from "A," the parent class. "D," and "E" are derived classes that inherited the base class "B," which is derived from class "A," and "E" and "G" are derived classes that inherited the base class "C," which is derived from class "A."

An example program for Hierarchical Inheritance:


class Single_base_class {
  public:
    int x, y;
    void data() {
      cout << "\nPlease enter the value of x and y ->\n";
      cin >> x << y;
    }
};
// class derived1 is just derived from the single_base_class
class Derived1: public Single_base_class {
  public: 
    void product() {
      cout << "\nThe Product is= " << x * y;
    }
};

//class derived1 is also derived from the class single_base_class
class Derived2: public Single_base_class {
  public: 
    void sum() {
      cout << "\nThe Sum is= " << x + y;
    }
};
int main() {
  Derived1 obj1;
  Derived2 obj2;
  obj1.data();
  obj1.product();
  obj2.data();
  obj2.sum();
  return 0;
}

 

Output:


Please enter the  value of x and y:
3
3
The Product is= 9
Enter the value of x and y:
10
10
The Sum is= 20

Explanation of the above program

In the above example, there is a base class named "Single base class" that consists of a data function that will collect the user input. The base class "Single base class" has a derived class called "Derived1" that will extend the base class “Single base class”. Derived1 class has a product function that is used to multiply two values, in this case, "x" and "y." One additional derived class, "Derived2," which also derives from the "Single base class," includes the sum function to add the integers. In the main function, an object is created and utilized to call the derived class function, which results in the output that is displayed above.

Hybrid Inheritance

In C++, hybrid inheritance combines one or more types of inheritance.

C++ Inheritance

We can see multiple inheritance types, or a combination of distinct inheritance types, in the diagram above.

An example program for Hybrid Inheritance:


class World {
  public:
    World() {
      cout << "This is  our World!\n";
    }
};

//here is Single Inheritance
class Continent: public World {
  public: 
    Continent() {
      cout << "This is our Continent\n";
    }
};

class Country {
  public:
    Country() {
      cout << "This is our Country\n";
    }
};

// here is multiple Inheritance
class India: public Continent, public Country {
  public: 
    India() {
      cout << "This is India!";
    }
};

int main() {
  India myworld;
  return 0;
}

 

Output:


This is our World!
This is our  Continent
This is our Country
This is India!

Explanation of the above program

In the program shown above, there is a base class called World and a derived class called Continent that extends "World." This is an example of single inheritance. India is a derived class that inherits both the "Continent" and "Country" classes from the base class Country and here we are able to see multiple inheritances. So, in this case, hybrid inheritance is demonstrated.

Conclusion

We reviewed the various types and levels of inheritance and how they are utilized appropriately. Inheritance is a basic idea in programming since it promotes the reusability and reliability of the code.