MULTIPLE INHERITANCE
INTRODUCTION
MAIN PART
If we take the Hydroplane for an example, one would notice that this object has a properties like the
Speed boat and some of the properties would come from the Airplane.
Yeah, we have some methods that come from the Airplane, because the Hydroplane can fly in the air.
But the Hydroplane can behave like the Speed boat when is gliding on the top of the water.
And if we add this two facts, we could say that the Hydroplane has some characteristics like two
different classes and it is possible to utilize that with multiple inheritance.
Now, when the concept of multiple inheritance is visualized, we could turn toward technical point of
view.
Let's say that we have the First and the Second classes created.
class First{ … };
class Second{ … };
This problem can be solved with two levels of inheritance, but the fact that the classes the First and the
Second are on the same level of inheritance is lost that way.< LINK TO PREVIOUS ATEMPT>
EXAMPLES
#1 The solution that will use two classes: the Plain and the Speed boat. Then we create the class
Hydroplane as a combination of two classes.
CODE:
#include <iostream>
class CPlain{
private:
int max_speed_on_water = 20;
public :
int maxSpeedOnWater( void ) { return max_speed_on_water; }
};
class CSpeedBoat{
private:
int max_speed_in_air = 200;
public :
int maxSpeedInAir( void ) { return max_speed_in_air; }
};
int
main( void )
{
CHydroplane HP;
cout<< “ The Hydroplane is in the air”<< HP.state()<<endl;
HP.change();
return EXIT_SUCCESS;
}
In this example program, we have tree classes: the SpeedBoat, the Airplane and the Hydroplane. The
classes the SpeedBoat and the Airplane are on the same level of hierarchy. Later, it might be good idea
to add one parent class the Vehicle. The class Hydroplane is created from classes the SpeedBoat and
the Plain, in the first and the second case the type of inheritance is the public one.
#2 The solution that will try to model the Sphinx. As you might heard, there is monument in Egypt that
is presenting some mythological creature also known as the Sphinx.
This creature has: the wings like a bird, body like a lion and the head and bosom like a woman. It is
stated in the Giza, close to some famous pyramids.
So in order to model the Sphinx we could have three classes: the Bird, the Lion and the Woman.
In each of those classes we could have method like this:
Then we could create class the Sphinx, as a combination of tree classes like this:
Then you can add some more data to class CSphinx, in order to describe: the location, the size, etc...
double dLength, dWidth, dHeight;
double dX, dY;
bool bNorth;
That mythological creature was famous by one puzzle. Try to find that puzzle and create appropriate
method.
In order to create that method, you will need to use the string and the mayor part of that method will
look something like this:
if ( answer == respond ){ cout<<” The Sphinx dies “;} else { cout <<” You die “;}
However, you could use some for loop instead, of overloaded operator == as we have shown in this
example.
In this example, I will not write any code, but I will provide the guidelines on how to model the Atom
from tree classes: the Electron, the Proton and the Neutron.
In order to analyze this problem we will start from the class Particle. After that, we will have classes :
the Electron, the Proton and the Neutron. This tree classes will save information about particular
particle and its count in the Atom of the Hydrogen, the Oxygen or the Nitrogen for example.
Beside the numbers of particles in some atom, we could save information about: the mass, the size, the
spin, the charge etc...
Be careful, it is possible to have the isotopes as well. The isotopes are the same kind of the Atom, but
with some different properties. For example, the Hydrogen could have two more isotopes: the
Deuterium and the Tricium. In order to make this work, one would need some look up table that will
contain information on each and every atom type. Yeah, that is a lot of work and it could be done with
some textual files, however more advanced programmers could even think of some different data
structures that will allow efficient data storage and search. Some even more advanced programmers
would see the potential use of simple data base too.
The next thing that you could add, is up to you. Find some periodical table and figure out what could be
useful.
ADDITIONAL DISCUSION
In the first example, we have two parent classes on the same level of hierarchy, that way we keep
information that classes the Plane and the Speed boat are equal.
As a exercise I have asked you to create the parent class the Vehicle. After you analyze the classes you,
should notice that both of those two classes have the speed and the method that will present that speed.
In this particular case I have avoided the most efficient solution, because we have not mention the
diamond problem and virtual inheritance as its adequate solution.
Do, you can model this relationship among those two classes with two layers of inheritance, but that
way you would loose the fact that classes the Plane and the Speed boat are on the same level.
I need to say that the best solution would have private inheritance when the Plain is in the air and then
it would change into public when is on the water. However, this implementation is out of our reach
still.
As additional exercise you could create classes: the hybrid car as a combination of the electric car and
the usual car, then you could model James Bond's Lotus Esprint S1 from 1976 as a combination of the
car and submarine or the Hovercraft as a combination of the Speed boat and the Car.
In the example two, we have the Sphinx, which has distinctive features of tree different kinds.
However, if you try to model the Human, and you would like to say that it has: the Heart, the Liver, the
Eyes etc... the containment could be the better option.
As a general conclusion, I need to say that blending will not be properly explained with multiple
inheritance, because if we observe the Nectarine as a combination of the Peach and the Plume we might
get our selfs into bit rough waters. The one more example could be the Peas, that has two colors(the
Green and the Yellow) and two types of skin( the Smooth and the Wrinkled).
For the third example we have mentioned the Atom, but there could be more layers to that structure:
the Molecules, the Quarks and the Leptons.
Similar example could be the model of the Earth it has: the Cities, the Provinces, the Countries and the
Continents as well.
One example that will become hard to model with the classes is the Graph that contains more threes
within.
CONCLUSION
It is usual to say that we have broaden and widen our level of knowledge. However, I would like to add
some more dimensions to this phrase.
Usage of multiple inheritance could be very good option in many situations, but for some cases we
could have better alternatives as well. Picking right model is not easy task, that we have learned.