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, hierarchical inheritance.

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

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 is 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 -


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 where -




Syntax of hierarchical inheritance
class Sports
{
//members of top-level-base-class, Sports
}



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



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



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


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


class SrFootballTeam : visibility-mode Football
{
//members of another derived-class at 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