Coupon Accepted Successfully!


Hierarchical Inheritance

We learned that in inheritance, one class could be inherited from one or more classes. In addition, new members are added to the derived class. The inheritance also supports the hierarchical arrangement of a program. Several programs require the hierarchical arrangement of classes, in which derived classes share the properties of the base class. A hierarchical unit exhibits top-down style through splitting a compound class into several simple subclasses. The program based on it is illustrated as follows:

11.9 Write a program to show hierarchical inheritance.
Red Yellow = Orange Red Blue = Violet = Reddishbrown
Red Blue = Violet Blue Yellow = Green = Bluishbrown
Blue Yellow = Green Red Yellow = Orange = Yellowishbrown
Explanation: For each color, a separate class with constructors is declared. The classes red, blue, and yellow are base classes. The class orange is derived from red and yellow. The class green is derived from blue and yellow. The class violet is derived from red and blue.

The class reddishbrown is derived from orange and violet; the class yellowishbrown is derived from green and orange; and, lastly, the class bluishbrown is derived from violet and green.

In function main(), objects of the classes reddishbrown, yellowishbrown, and bluishbrown are declared. When the objects are declared, constructors are executed from the base to derived classes. When executed, the constructor displays the class name (color name). The output shows the names of different colors and resulting colors after their combination.

Test Your Skills Now!
Take a Quiz now
Reviewer Name