The concept of inheritance in the programming world is analogous to inheritance in the real world. In the real world, you pass traits and characteristics from one generation to the other. Similarly, other subclasses derive or inherit the properties of one class. The class whose members are inherited is the base class or parent class and the subclasses which are derived from the base class are termed as the derived classes or child classes. Being one of the core concepts of object-oriented programming, inheritance allows you to implement code reusability by defining subclasses inheriting from existing classes. 

Hierarchical Inheritance in C++ 

Hierarchical Inheritance in C++ refers to the type of inheritance that has a hierarchical structure of classes. A single base class can have multiple derived classes, and other subclasses can further inherit these derived classes, forming a hierarchy of classes. The following diagram illustrates the structure of Hierarchical Inheritance in C++.

Hierarchical_Inheritance_In_C_P_P_Chart

Now, understand Hierarchical Inheritance in C++ with the help of an example. There are 3 major branches derived from modern science. They are- Physics, Chemistry, and Biology. These 3 branches are further divided into sub-branches, which are further classified into other specialized disciplines. Here, Modern Science is the base class which is further inherited by 3 subclasses- Physics, Chemistry, and Biology. And these subclasses are further inherited by other derived classes, structuring into a hierarchy of classes. 

Want a Top Software Development Job? Start Here!

Full Stack Developer - MERN StackExplore Program
Want a Top Software Development Job? Start Here!

Use of Hierarchical Inheritance in C++

Hierarchical Inheritance in C++ is useful in the cases where a hierarchy has to be maintained. Most of the schools and colleges maintain the data of their students in hierarchical form. For example, a college has to maintain the data of the engineering students and segregate them according to their branches such as the IT branch, mechanical branch, and so on.  You can achieve such a scenario can by Hierarchical Inheritance in C++ easily. Similar is the case with the companies where they have to maintain the data of their employees according to the different departments.

Syntax to Implement Hierarchical Inheritance in C++

You can use the following syntax to achieve Hierarchical Inheritance in C++:

class base_class

{

    //data members

    //member functions

};

class derived_class1 : visibility_mode base_class

{

    //data members

    //member functions

};

class derived_class2 : visibility_mode base_class

{

    //data members

    //member functions

};

Description of the Syntax

  • visibility_mode: It provides the visibility mode in the class declaration. It specifies how the members of the base class will be inherited by the derived class. In the case of Hierarchical Inheritance in C++, it separately defines the visibility mode with every derived class during inheritance.
  • base_class: This is the name of the class from which the derived class inherits its properties and characteristics.
  • derived_class1: This is the name of the first derived class that inherits the properties of the base class using the visibility mode.
  • derived_class2: This is the name of the second derived class that inherits the properties of the base class using the visibility mode.

NOTE: There can be n number of derived classes of a single base class.

Visibility Modes in Hierarchical Inheritance in C++

The visibility mode determines control over the base class members within the child classes by specifying how the base class features will be inherited by the derived class. There are three types of visibility modes of Hierarchical Inheritance in C++:

1. Public Visibility Mode

Public visibility mode does not affect the working of access specifiers. All the access specifiers remain as they are. This means that the private members remain private i.e., not accessible by anyone, public members remain public i.e., accessible in all the derived classes as well outside the derived classes. And protected members remain protected, i.e., accessible only within the derived classes. 

The following code snippet illustrates how to apply the public visibility mode in a derived class in Hierarchical Inheritance in C++:

class base_class

{

    //data members

    //member functions

};

class derived_class1 : public base_class

{

    //data members

    //member functions

};

class derived_class2 : public base_class

{

    //data members

    //member functions

};

NOTE: Here, the visibility modes of the derived classes can be different from each other as well.

The following code illustrates the working of public visibility mode with all three access specifiers of the base class in Hierarchical Inheritance in C++:

#include <bits/stdc++.h>

using namespace std;

class base_class

{

public:

    int var1;

protected:

    int var2;

private:

    int var3;

}; 

class derived_class1 : public base_class

{

    //data members

    //member functions

};

class derived_class2 : public base_class

{

    //data members

    //member functions

}; 

// main function

int main()

{

    derived_class1 obj1;

    derived_class2 obj2;

     // Accessible

    cout << "Value of var1 accessed by obj1 is" << obj1.var1 << endl;

    // Accessible

    cout << "Value of var1 accessed by obj2 is" << obj2.var1 << endl; 

    // Not accessible

    cout << "Value of var2 accessed by obj1 is" << obj1.var2 << endl;

    // Not accessible

    cout << "Value of var2 accessed by obj2 is" << obj2.var2 << endl;

    // Not accessible

    cout << "Value of var3 accessed by obj1 is" << obj1.var3 << endl;

    // Not accessible

    cout << "Value of var3 accessed by obj2 is" << obj2.var3 << endl;

}

Hierarchical_Inheritance_In_C_P_P_1  

In the above example, both derived_class1 and derived_class2 have public visibility mode so var1 being the public member, is accessible by the objects of both derived classes. On the other hand, the private and protected members are not accessible out of the derived classes. Hence, an error is thrown by the compiler when the objects of these derived classes try to access the private and protected members.

Learn 15+ In-Demand Tools and Skills!

Automation Testing Masters ProgramExplore Program
Learn 15+ In-Demand Tools and Skills!

2. Private Visibility Mode

In private visibility mode, both the public and protected access specifiers become private. This makes all the members inaccessible outside of the class in which they are defined. You can only access them through the member functions of the same class. And since the private members can not be inherited, you can also not inherit further these members.

The following code snippet illustrates how to apply the private visibility mode in a derived class in Hierarchical Inheritance in C++:

class base_class

{

    //data members

    //member functions

};

class derived_class1 : private base_class

{

    //data members

    //member functions

};

class derived_class2 : private base_class

{

    //data members

    //member functions

};

NOTE: Here, the visibility modes of the derived classes can be different from each other as well.

The following code illustrates the working of private visibility mode with all three access specifiers of the base class in Hierarchical Inheritance in C++:

#include <bits/stdc++.h>

using namespace std;

class base_class

{

public:

    int var1;

protected:

    int var2;

private:

    int var3;

};

 class derived_class1 : private base_class

{

    //data members

    //member functions

};

 class derived_class2 : private base_class

{

    //data members

    //member functions

}; 

// main function

int main()

{

    derived_class1 obj1;

    derived_class2 obj2;

    // Not Accessible

    cout << "Value of var1 accessed by obj1 is" << obj1.var1 << endl;

    // Not Accessible

    cout << "Value of var1 accessed by obj2 is" << obj2.var1 << endl;

    // Not accessible

    cout << "Value of var2 accessed by obj1 is" << obj1.var2 << endl;

    // Not accessible

    cout << "Value of var2 accessed by obj2 is" << obj2.var2 << endl;

    // Not accessible

    cout << "Value of var3 accessed by obj1 is" << obj1.var3 << endl;

    // Not accessible

    cout << "Value of var3 accessed by obj2 is" << obj2.var3 << endl;

}

/Hierarchical_Inheritance_In_C_P_P_2  

In the above example, both derived_class1 and derived_class2 have private visibility mode so var1, var2, and var3 become private members in the derived class, and are not accessible by the objects of both derived classes. 

3. Protected Visibility Mode

In protected visibility mode, the public members become protected and the private and protected members remain the same. This means that you cannot access the public members, just like the protected members outside the derived classes. The private members remain inaccessible even in the derived classes and can only be accessed by the member functions of the class in which the private members are defined.

The following code snippet illustrates how to apply the protected visibility mode in a derived class in Hierarchical Inheritance:

class base_class

{

    //data members

    //member functions

};

class derived_class1 : protected base_class

{

    //data members

    //member functions

};

class derived_class2 : protected base_class

{

    //data members

    //member functions

};

NOTE: Here, the visibility modes of the derived classes can be different from each other as well. 

The following code illustrates the working of protected visibility mode with all three access specifiers of the base class in Hierarchical Inheritance:

#include <bits/stdc++.h>

using namespace std; 

class base_class

{

public:

    int var1;

protected:

    int var2;

private:

    int var3;

};

class derived_class1 : protected base_class

{

    //data members

    //member functions

};

class derived_class2 : protected base_class

{

    //data members

    //member functions

};

// main function

int main()

{

    derived_class1 obj1;

    derived_class2 obj2;

    // Not Accessible

    cout << "Value of var1 accessed by obj1 is" << obj1.var1 << endl;

    // Not Accessible

    cout << "Value of var1 accessed by obj2 is" << obj2.var1 << endl; 

    // Not accessible

    cout << "Value of var2 accessed by obj1 is" << obj1.var2 << endl;

    // Not accessible

    cout << "Value of var2 accessed by obj2 is" << obj2.var2 << endl; 

    // Not accessible

    cout << "Value of var3 accessed by obj1 is" << obj1.var3 << endl;

    // Not accessible

    cout << "Value of var3 accessed by obj2 is" << obj2.var3 << endl;

}

Hierarchical_Inheritance_In_C_P_P_3 

In the above example, both derived_class1 and derived_class2 protect the visibility mode so var1 becomes the protected member, while var2 and var3 remain the same. This makes var1 and var2 inaccessible outside the derived classes while var3 remains the same i.e., the private member, so it is not accessible anywhere except the base class itself.

The following chart illustrates the control of the derived classes over the members of the base class in different visibility modes in hierarchical inheritance :

 

BASE CLASS

DERIVED CLASS

DERIVED CLASS

DERIVED CLASS

PUBLIC

PROTECTED

PRIVATE

PUBLIC 

Public

Protected

Private

PROTECTED

Protected

Protected

Private

PRIVATE

Not Inherited 

Not Inherited 

Not Inherited 

Want a Top Software Development Job? Start Here!

Full Stack Developer - MERN StackExplore Program
Want a Top Software Development Job? Start Here!

Examples

The following examples illustrate Hierarchical Inheritance in C++.

Example 1

The following example illustrates the working of the default constructor in Hierarchical Inheritance.

#include <iostream>

using namespace std;

// base class

class university

{

public:

    // default constructor of the base class

    university()

    {

        cout << "I am the default constructor of the base class.\n\n";

    }

};

// derived class 1 inheriting the base class

class student: public university

{};

// derived class 2 inheriting the base class 

class faculty: public university

{};

int main()

{

    // create the objects of the derived classes

    student obj1; // constructor of base class will be called             

    faculty obj2; // constructor of the base class will be called

    return 0;

}

Hierarchical_Inheritance_In_C_P_P_4 

In the above example, two derived classes student and faculty inherit the same base class university. This represents the structure of Hierarchical Inheritance. The default constructor of the base class is called as soon as it creates the objects of both of these derived classes.

Example 2

The following example illustrates the working of data members and member functions in the Hierarchical Inheritance in C++.

#include <iostream>

using namespace std;

// base class

class university

{

private:

    string university_name;

public:

    string getUniversityName()

    {

        university_name = "XYZ University";

        return university_name;

    }

};

// derived class 1 inheriting the base class

class student: public university

{

public:

    void display_student()

    {

        cout << "I am a student of the " << getUniversityName() << ".\n\n";

    }

};

// derived class 2 inheriting the base class

class faculty: public university

{

public:

    void display_faculty()

    {

        cout << "I am a professor in the " << getUniversityName() << ".\n\n";

    }

};

int main()

{

    // object of the derived class 1

    student obj1; 

    // call the display_student() function of the derived class 1

    obj1.display_student();

    // object of the derived class 2

    faculty obj2; 

    // call the display_student() function of the derived class 2

    obj2.display_faculty();

    return 0;

}

Hierarchical_Inheritance_In_C_P_P_5. 

In the above example, the base class university is inherited by the subclasses student and faculty. The public member function getUniversityName() is accessible to both of these derived classes. The functions display_student() and display_faculty() access the getUniversityName() function of the base class to print the university name which is common for both the student and the faculty.

Example 3

The following example illustrates how to access data members of the base class in the derived classes.

#include <iostream>

using namespace std;

// base class

class dimension

{

public:

    int length = 5;

    int breadth = 6;

};

// derived class 1

class perimeter : public dimension

{

public:

    // printPerimeter() function of the derived class 1

    // to print the perimeter

    void printPerimeter()

    {

        // calculating the perimeter using the 

        // dimensions in the base class.

        cout << "The calculated perimeter is: " << 2 * (length + breadth) << "\n\n";

    }

};

// derived class 2

class area : public dimension

{

public:

    // printArea() function of the derived class 2

    // to print the perimeter

    void printArea()

    {

        // calculating the area using the 

        // dimensions in the base class.

        cout << "The calculated area is: " << (length * breadth) << "\n\n";

    }

};

int main()

{

    // object of the derived class 1

    perimeter obj1;

    // object of the derived class 2

    area obj2;

    // call printPerimeter() function of the derived class 1

    obj1.printPerimeter();

    // call printArea() function of the derived class 2

    obj2.printArea();

    return 0;

}

Hierarchical_Inheritance_In_C_P_P_6 

In the above example, the derived classes- perimeter and area inherits the base class dimension. The data members- length and breadth, of the base class are directly accessible to the member functions of the derived classes. The member functions findPerimeter() and findArea() access the length and breadth variables of the base class to calculate and print perimeter and area respectively.

Want a Top Software Development Job? Start Here!

Full Stack Developer - MERN StackExplore Program
Want a Top Software Development Job? Start Here!

Final Thoughts!

To quickly sum up, in this article, you learned about the ins and outs of hierarchical inheritance in C++. You started with a general introduction to inheritance and its uses in real life and then gradually moved on to hierarchical inheritance in C++. You understood the uses of hierarchical inheritance and the syntax to implement it with a combination of base and child classes. You understood hierarchical inheritance with different visibility modes and wrapped up with a few examples to demonstrate the usage of hierarchical inheritance.

If you want to understand the C++ concepts in greater detail, you can check out Simplilearn’s complete guide on C++ for beginners. This guide will help you get started with the fundamental concepts of C++.

If you want to start your professional career as a Full Stack Web Developer in a top tech giant, you should check out Simplilearn’s 9 month Full Stack Web Development training. This training is led by industry experts and is available throughout the course to guide you at every step. At the end of the course, you will have mastered some of the trending technologies such as Java and its core libraries such as Spring, Hibernate, JPA, etc., DevOps technologies, Agile, HTML, CSS, JS, etc. 

If you have a knack for keeping yourself up to date with new technologies, you should try out some of our free online courses as well.

Happy Learning!

Our Software Development Courses Duration And Fees

Software Development Course typically range from a few weeks to several months, with fees varying based on program and institution.

Program NameDurationFees
Caltech Coding Bootcamp

Cohort Starts: 15 Apr, 2024

6 Months$ 8,000
Full Stack Java Developer

Cohort Starts: 2 Apr, 2024

6 Months$ 1,449
Automation Test Engineer

Cohort Starts: 3 Apr, 2024

11 Months$ 1,499
Full Stack Developer - MERN Stack

Cohort Starts: 3 Apr, 2024

6 Months$ 1,449