Advertisement



< Prev
Next >



C++ Hierarchical Inheritance




In our previous article, we have introduced you to one of the types of inheritance in C++ i.e. multilevel inheritance. Moving on, we are going to discuss another type of inheritance i.e. hierarchical inheritance.

Hierarchical inheritance is performed to replicate a hierarchical structure in a computer program, for example, when the features of a class at the topmost-level are shared by many classes at the lower level in a hierarchy through inheritance.

The class at the topmost-level is known as base class and the classes that inherit the features of the class at the topmost-level are called subclasses or derived classes.




What features a subclass inherits from its base class through inheritance?



Note - Base class members marked with private visibility-mode are never inherited.




Rules of inheritance -


Before introducting the syntax of hierarchical inheritance, let's refresh the importance concepts of inheritance. Depending on the value of visibility-mode, inheritance can be performed in various ways -




Syntax of Hierarchical Inheritance


To understand the concept of hierarchical inheritance, let us take an example which replicates a hierarchical structure to a computer program, where:




Syntax of hierarchical inheritance
//C++ Hierarchical Inheritance 


class Sports
{
//members of a top-level-base-class i.e. Sports
}



class Football : visibility-mode Sports
{
//members of a derived-class at a lower level i.e. Football 
}



class Hockey : visibility-mode Sports
{
//members of another derived-class at a lower level i.e. Hockey
}



class Cricket : visibility-mode Sports
{
//members of another derived-class at a lower level i.e. Cricket
}


class JrFootballTeam : visibility-mode Football
{
//members of another derived-class at a lower level than class Football
}


class SrFootballTeam : visibility-mode Football
{
//members of another derived-class at a lower level than class Football
}





Advertisement




Please Subscribe

Please subscribe to our social media channels for daily updates.


Decodejava Facebook Page  DecodeJava Twitter Page Decodejava Google+ Page




Advertisement



Notifications



Please check our latest addition

C#, PYTHON and DJANGO


Advertisement