Inheritance- IV
The following topics are covered in this section:
When we have more than one base class for a derived class, we call it as multiple inheritance. So far we have only seen the case where a derived class has one base class. In this section we will deal with the features and problems associated with multiple inheritance. Some programmers advise against the use of multiple inheritance. In fact Java does not permit multiple inheritance.
For a basic explanation about multiple inheritance, assume that you have created two classes called ‘bacteria’ and ‘virus’ to model the two different life forms. Now, maybe you discover a new life form that has the features of both bacteria and virus. Instead of creating a new class with all the properties again you can simply derive a new class from both bacteria and virus (hence bacteria and virus will be the two base classes).
Let’s call the derived class as ‘crossbreed’. The program is written below:
The output will be:
Bacteria constructor
Virus constructor
This is the crossbreed of bacteria and virus
Value of x for bacteria is : 10
This is crossbreed disp function.
The main points to be noted are:
There is another problem associated with multiple inheritance. There is a good chance that your derived class can have more than one copy of the base class. Consider three classes as shown in the figure below:
The following diagram is better suited to explain the problem inherent in multiple inheritance.
The problem with the above form of inheritance is that both base1 and base2 will have a copy of the class ‘mainbase’. When the class ‘derived’ is obtained from these two classes, it will end up with two copies of ‘mainbase’. If mainbase had a member data called ‘count’, then ‘derived’ class cannot access the member ‘count’ by saying:
count=1;
The ‘derived’ class has to specify which copy of ‘count’ it wants to access using the scope resolution operator.
base1::count=1;
Mostly you wouldn’t want such a case and so C++ introduces virtual base class to prevent the ‘derived’ class from obtaining two copies of the same class. You can specify in the derived class that the base classes are ‘virtual’. Check out the example below:
The output is:
The count is : 1
Now there is no problem when referring to the variable ‘count’. Though the two classes ‘base1’ and ‘base2’ are inheriting ‘mainbase’ as virtual, both of them will individually have their own copy of ‘count’. The ‘virtual’ effect occurs only when some other class uses these two classes as its base class. In that case that derived class will have only one copy of ‘mainbase’.
Alternative Representation of Inheritance:
In all the earlier figures for inheritance we have used a downward pointing arrow to denote inheritance. The following representation is also used for inheritance:
In this case the arrow points from the subclass to the superclass. This also means that the class or RacingCars is inherited (or derived) from the GeneralCars class. Do not get confused with the two methods of representing inheritance.
Go back to the Contents Page 2
Copyright © 2004 Sethu Subramanian All rights reserved.