×

Search anything:

Hierarchical inheritance in C++

Binary Tree book by OpenGenus

Open-Source Internship opportunity by OpenGenus for programmers. Apply now.

Reading time: 20 minutes

Inheritance is the process of inheriting properties of objects of one class by objects of another class.
The class which inherits the properties of another class is called Derived or Child or Sub class and the class whose properties are inherited is called Base or Parent or Super class.
When more than one classes are derived from a single base class, such inheritance is known as Hierarchical Inheritance, where features that are common in lower level are included in parent class.
Problems where hierarchy has to be maintained can be solved easily using this inheritance.

topic of image

Some examples :
Civil, Computer, Mechanical, Electrical are derived from Engineer.
Natural language, Programming language are derived from Language.

Syntax :

class base_classname
{
    properties;
    methods;
};

class derived_class1:visibility_mode base_classname
{
    properties;
    methods;
};

class derived_class2:visibility_mode base_classname
{
    properties;
    methods;
};
... ... ...
... ... ...
class derived_classN:visibility_mode base_classname
{
    properties;
    methods;
};

Access Specifiers

There are 3 access specifiers for a class/struct/Union in C++. These access specifiers define how the members of the class can be accessed. Of course, any member of a class is accessible within that class(Inside any member function of that same class). Moving ahead to type of access specifiers, they are:

Public - The members declared as Public are accessible from outside the Class through an object of the class.

Protected - The members declared as Protected are accessible from outside the class BUT only in a class derived from it.

Private - These members are only accessible from within the class. No outside Access is allowed.

Inheritance and Access Specifiers

Inheritance in C++ can be one of the following types:

1)Public Inheritance
2)Private Inheritance
3)Protected inheritance

topic of image

Public Inheritance :
All Public members of the Base Class become Public Members of the derived class.
All Protected members of the Base Class become Protected Members of the Derived Class.

Private Inheritance :
All Public members of the Base Class become Private Members of the Derived class.
All Protected members of the Base Class become Private Members of the Derived Class.

Protected Inheritance :
All Public members of the Base Class become Protected Members of the derived class.
All Protected members of the Base Class become Protected Members of the Derived Class.

Example

// C++ program to implement  
// Hierarchical Inheritance 
#include <iostream> 
using namespace std; 
  
// base class 
class Vehicle  
{ 
  public: 
    Vehicle() 
    { 
      cout << "This is a Vehicle" << endl; 
    } 
}; 
  
  
// first sub class  
class Car: public Vehicle 
{ 
  
}; 
  
// second sub class 
class Bus: public Vehicle 
{ 
      
}; 
  
// main function 
int main() 
{    
    // creating object of sub class will 
    // invoke the constructor of base class 
    Car obj1; 
    Bus obj2; 
    return 0; 
} 

Output :

This is a Vehicle
This is a Vehicle

Question

Which type of inheritance leads to diamond problem?

Multiple
Multi-level
Single level
Hierarchical
When 2 or more classes inherit the same class using multiple inheritance and then one more class inherits those two base classes, we get a diamond like structure. Here, ambiguity arises when same function gets derived into 2 base classes and finally to 3rd level class, because same name functions are being inherited.
Harshita Sahai

Harshita Sahai

Maintainer at OpenGenus | Previously Software Developer, Intern at OpenGenus (June to August 2019) | B.Tech in Information Technology from Guru Gobind Singh Indraprastha University (2017 to 2021)

Read More

Improved & Reviewed by:


OpenGenus Tech Review Team OpenGenus Tech Review Team
Hierarchical inheritance in C++
Share this