Вы находитесь на странице: 1из 6

THE SECOND TAKE ON THE BEWILDERING CONCEPT OF THE

MULTIPLE INHERITANCE

INTRODUCTION

Multiple inheritance, as a concept, contributes to expressiveness of the C++. As we have mentioned


earlier there are different types of inheritance: “is a” and “has a”. However, there are some
relationships between the classes as well.
This type of inheritance will enable us to combine two or more classes in order to get the new class
with less coding. In another words, we will use two classes and mix them together and get the new
class that has code from two or more classes.
In the way, this way of creating new class with multiple parents is like we have operation of addition
on inheritance applied.

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{ … };

class Combination: [ private | protected | public ] First,


[ private | protected | public ] Second
{ … };

From the definition of the class Combination we have some facts:


 we could use one of the three types of inheritance already mentioned,
 we need to state the name of the parent classes,
 we have classes the First and the Second on the same level of hierarchy.

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>

using namespace std;

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; }
};

class CHidroplane: public CPlain,


public CSpeedBoat{
private:
bool on_Water;
public :
CHydroplane( ){ on_Water = true; }

void changeState( void ) { on_Water = ! on_Water; }

bool state( void ) { return on_Water; }


};

int
main( void )
{
CHydroplane HP;
cout<< “ The Hydroplane is in the air”<< HP.state()<<endl;

HP.change();

cout<< “ The Hydroplane is in the air”<< HP.state()<<endl;

cout<<”The maximum speed on the water =”<<HP. maxSpeedOnWater()<<endl


<<”The maximum speee in the air =”<<HP. maxSpeedInAir<<endl;

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.

The class SpeedBoat has:


 private data used in order to note the maximum speed on the water.
 the public method that will return that maximum speed.
The class Plain has:
 the private data used in order to note the maximum speed in the air.
 the public method that will return that maximum speed.
The class Hydroplane inherits the maximum speed and the methods used in order to access that data.
Beside this, we have added some more stuff to our class:
 the private data, of boolean type used to establish the fact that the plain is in the air.
 the method change, that changes the state of variable on_Water.
 the method state, that returns the state on_Water.

In our main function we have:


 declared the object HP.
 presented the state of the object HP, is it in the air.
 changed the state of our object.
 presented the state of the object HP, after it has been changed.
 shown the maximum speeds on the water and in the air.

Hope, that this was enough explanations.

#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:

void fromLion( void ){ cout<<” the body “ << endl;}


void fromBird( void ){ coud<<” the wings ”<< endl;}
void fromWoman( void ){ cout<<” the head “<<endl
<<” the bosom “<<endl;}

Then we could create class the Sphinx, as a combination of tree classes like this:

class CSphinx : public CBird, public CLion, public CWoman.

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.

Just don't take this example too serious.

#3 The solution that models the atom.

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.

Вам также может понравиться