You are on page 1of 6

April 6, 2016 Lab 09

Student Name: Roll No: Section:

LAB 09: FURTHER TYPES OF INHERITANCE

Lab Objectives:
 Multi-level Inheritance
 Hierarchal Inheritance
 Multiple Inheritance

1. MULTILEVEL INHERITANCE
We have been using simple class hierarchies consisting of only a single base class and a derived class.
However, you can build hierarchies that contain as many layers of inheritance as you like. As mentioned,
it is perfectly acceptable to use a derived class as a base class of another. For example, given three
classes called A, B, and C, C can be derived from B, which can be derived from A. When this type of
situation occurs, each derived class inherits all of the traits found in all of its base classes. In this case,
C inherits all aspects of B and A.

CLASS A

CLASS B

CLASS C
Figure 1. Multi-Level Inheritance

To see how a multilevel hierarchy can be useful, consider example 01. In it, the derived class (from previous
lab) Triangle is used as a base class to create the derived class called ColorTriangle. ColorTriangle
inherits all of the traits of Triangle and TwoDShape and adds a field called color, which holds the color
of the triangle.

Page 1 of 6
April 6, 2016
Student Name: Roll No: Section:
Example 01:

Page 2 of 6
April 6, 2016 Lab 09
Student Name: Roll No: Section:

The main() part of the program:

 Declares three objects of different classes: two ColorTriangle, and one Triangle.
 Values to these objects are assigned using constructors.
 These values are then displayed using the member functions.
Notice that we don’t define any objects of the base class Triangle. We use this as a general class whose sole
purpose is to act as a base from which other classes are derived.

Run Example 01, write down the output:

Mention the accessibility (Yes/No) in the following table:

Member Accessible to Accessible to Accessible to


TwoDShape Objects Triangle Objects ColorTriangle Objects

width
area
color
ShowDim()
ShowStyle()
ShowColor()

Page 3 of 6
April 6, 2016 Lab 09
Student Name: Roll No: Section:

2. HIERARCHICAL INHERITANCE
In this inheritance, more than one derived classes are created from a single base, as illustrated in the figure
below. Also consider example 02.

CLASS A

CLASS B CLASS C
Figure 2. Hierarchical Inheritance
Example 02:

Page 4 of 6
April 6, 2016 Lab 09
Student Name: Roll No: Section:
3. MULTIPLE INHERITANCE
A class can be derived from more than one base class. This is called multiple inheritance. Figure
03 shows how this looks when a class C is derived from base classes A and B.

CLASS B CLASS A

CLASS C
Figure 3. Hierarchical Inheritance

The general syntax for multiple inheritance is as follows:

class A // base class A


{ };
class B // base class B
{ };
class C : public A, public B // C is derived from A and B
{ };
Example 03:

Page 5 of 6
April 6, 2016
Student Name: Roll No: Section:

4. ASSIGNMENTS
Lab Assignment 01: Run example 01, attach the output and specify:
 the base and derived class(es)
 type of inheritance

Lab Assignment 02: Run example 02 (by adding relevant main function code; create objects), attach the
output and specify:
 the base and derived class(es)
 type of inheritance

Lab Assignment 03: Run example 03 (by adding relevant main function code; create objects), attach the
output and specify:
 the base and derived class(es)
 type of inheritance

Page 6 of 6

You might also like