What is Hierarchical Inheritance in C++?

Video Tutorial
FREE
Inheritance  thumbnail
This video belongs to
Object Oriented Programming in Java Course Online
4 modules
Certificate
Topics Covered

When more than one class is inherited from a single base class, we call it Hierarchical Inheritance. The child classes inherit the features of the parent class. It allows code reusability and improves the readability of code.

As the base class features are reused as often as needed, it improves maintainability and reduces development costs.

Syntax Of Hierarchical Inheritance In C++

Let us now go through the syntax of hierarchical inheritance in C++.

Parent : This is the base class, which contains the common characteristics of the derived classes. The derived classes inherit features from this class.

child1 : First derived class inheriting properties from the base class.

child2 : Second derived class inheriting properties from the base class.

visibility_mode : Visibility modes control the accessibility of the base class by its derived classes.

How Does Hierarchical Inheritance Works In C++?

In this section, we shall describe how hierarchical inheritance works in C++. In hierarchical inheritance, we shall have one base class and more than one child, class. These child classes may or may not have multiple child classes. All the child classes will acquire/inherit the properties of the base class. This forms a tree-like hierarchical structure, as shown in the figure below :

working of Hierarchical Inheritance in C++

In the above figure, class G is the base class. Class B and Class E are the derived classes of Class G. Class A and Class C are the derived classes of Class B, and Class D and Class F are derived classes of Class E.

This forms a hierarchy-like structure. Thus subclasses B and E serve as the base classes for A, C, D, and F, respectively. This is how hierarchical inheritance is used in C++.

Visibility Modes

How a child class inherits from its parent class is determined by various Visibility modes in C++. Visibility modes control the accessibility of the Super Class by its Sub Classes. It is defined with each derived class separately during Hierarchical Inheritance.

The term Visibility mode must not be confused with the term Access Specifier. While the former controls the accessibility of inherited members in the class, the latter controls the accessibility of members within the class. There are three types of Visibility Modes in C++ : Public, Private, and Protected.

Visibility Modes in Hierarchical Inheritance

Public visibility modes

Public Visibility Modes allow the public members of the base class to become the public members of the child class and the protected members of the base class from becoming the protected members of the child class. The private members of the base class cannot be inherited. Thus using public visibility mode, all-access specifier stays as they are.

The public member of the base class will be accessible to the objects of the child classes. The private members of the base class won't be accessible to anyone, and the protected members would be accessible only within the derived classes.

Let us now go through the syntax of Public Visibility Mode.

As you can see in the above syntax, the visibility mode of each child class has been set to Public.

Let us see what happens when we use Public Visibility mode to access public, private, and protected data members of the base class.

Output :

Explanation :

  • In our Parent class, we defined three data members: public, private, and protected.
  • We have three child classes, child1child1, child2child2, and child3child3, inheriting the Parent Class through Public Visibility Mode.
  • Thus, a public member of Parent Class, i.e., a, will become a public member of the child classes child1child1, child2child2, and child3child3. Thus a can be accessed through the objects of the child classes,xx, yy, and zz.
  • The protected member of Parent Class, i.e., c, will become a protected member of the child classes child1child1, child2child2, and child3child3. Thus, an error is shown on the screen whenever we try accessing c through the objects of the child classes,xx, yy, and zz.
  • As we know, private members of a class are never inherited. Thus the error is shown when we try to access private member b of the Base Class through objects of the derived classes.

Private visibility modes

Private Visibility Modes allow the public members of the base class to become the private members of the child class and the protected members of the base class to become the private members of the child class. These private members cannot be further inherited in other classes. They can be accessed only through member functions of the same class. The private members of the base class cannot be inherited.

Let us now go through the syntax of Private Visibility Mode.

As you can see in the above syntax, the visibility mode of each child class has been set to Private.

Let us see what happens when we use Private Visibility mode to access public, private, and protected data members of the base class.

Output :

Explanation :

  • In our Parent class, we defined three data members: public, private, and protected.
  • We have three child classes, child1child1, child2child2, and child3child3, inheriting the Parent Class through Private Visibility Mode.
  • Thus, public members of Parent Class, i.e., a, will become private members of the child classes child1child1, child2child2, and child3child3. Thus a cannot be accessed through the objects of the child classes, xx, yy, and zz, and thus the error will be thrown when we try doing the same.
  • The protected member of Parent Class, i.e., c, will become a private member of the child classes child1child1, child2child2, and child3child3. Thus, an error is shown on the screen whenever we try accessing c through the objects of the child classes, xx, yy, and zz.
  • As we know, private members of a class are never inherited. This error is shown when we try to access private member b of the Base Class through objects of the derived classes.

Protected visibility modes

Protected Visibility Modes allow the public members of the base class to become the protected members of the child class and the protected members of the base class to become the protected members of the child class. The private members of the base class cannot be inherited.

Thus public members will be changed to protected members in the child class, and private and protected members will remain the same. None of the data members will be accessible outside of the derived classes.

Let us now go through the syntax of Protected Visibility Mode.

As you can see in the above syntax, the visibility mode of each child class has been set to Private.

Let us see what happens when we use Protected Visibility mode to access public, private, and protected data members of the base class.

Output :

Explanation :

  • In our Parent class, we defined three data members: public, private, and protected.
  • We have three child classes, child1child1, child2child2, and child3child3, inheriting the Parent Class through Protected Visibility Mode.
  • Thus, a public member of Parent Class, i.e., will become a protected member of the child classes child1child1, child2child2, and child3child3. Thus a cannot be accessed through the objects of the child classes, xx, yy, and zz, and thus the error will be thrown when we try doing the same.
  • The protected member of Parent Class, i.e., c, will become a protected member of the child classes child1child1, child2child2, and child3child3. Thus, an error is shown on the screen whenever we try accessing c through the objects of the child classes, xx, yy, and zz.
  • As we know, private members of a class are never inherited. This error is shown when we try to access private member b of the Base Class through objects of the derived classes.

Summary of visibility modes

The three visibility modes discussed above are summarised in the table below :

Summary of visibility modes

What Is The Use Of Hierarchical Inheritance In C++?

We use Hierarchical Inheritance in C++ in places where we need to achieve hierarchy. E.g., a company has to maintain data of all its employees and segregate them by their domains, such as sales, technical, human resources, operations, etc. A technical person can be either a developer or a tester.

use of Hierarchical Inheritance in C++

Thus we can see a hierarchical structure is formed inside the company. If we want to represent the different classes of employees through code, it can be achieved using Hierarchical Inheritance in C++. By defining subclasses from existing parent classes, we will have to write less code for each subclass since they already inherit the common code from the parent class. This is known as code reusability in C++ and is one important pillar of Object-Oriented Programming.

Example

Here is an example of hierarchical inheritance in C++. We create a parent class that takes the input of two integers from the user. We create three derived classes to use the inputs from the base class and perform mathematical operations such as summation, difference, and multiplication with the inputted integers.

Output :

Explanation :

In the above example, our class Parent has two data members, aa and bb. It reads the values of aa and bb using the function Inputdata(). Class child1 inherits class Parent and performs summation operation using the data members from the Parent Class and printing it to the screen. Class child2 is also inheriting class Parent and performing subtraction operations using the data members from the Parent Class and printing it to the screen. Similarly, Class child3 inherits class Parent and performs multiplication operations using the data members from the base class and printing the product.

Learn More

You can learn more about inheritance in C++ here : Inheritance in C++

Conclusion

  • In hierarchical inheritance there is one base class and more than one child class.
  • We use Visibility modes to determine the accessibility of a base class to its child classes.
  • There are three visibility modes in C++, public, private, and protected.
  • A derived class only inherits the public and protected members while inheriting a base class. The private members are never inherited.
  • Public visibility modes allow the public members of the base class to become public members of the derived class and protected members of the base class from becoming protected members of the derived class.
  • Private visibility mode changes the public and protected members of the base class to private members of the derived class.
  • Protected visibility mode changes the public and protected members of the base class to protected members of the derived class.