Advertisement



< Prev
Next >



C++ Hierarchical Inheritance




In our previous article, we introduced you to one of the types of inheritance - multilevel inheritance. Today we are going to discuss the another type of inheritance in C++ named - hierarchical inheritance. Hierarchical inheritance is performed when we have to replicate a hierarchical structure in a computer program, for example - when features of a class at the top-most-level is 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, multiple inheritance can be performed in various ways -




Syntax of Hierarchical Inheritance


To understand the concept of hybrid 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

PYTHON


Advertisement