• No results found

Multiple Inheritance

In document (OOP)_01-45(22-08-2009) updated (Page 34-41)

Sometimes we want to reuse characteristics of more than one parent class, in that case we need to inherit a class from more than one classes.

Example 1– Multiple Inheritance

Consider the example of an imaginary specie Mermaid used in fairy tales that lives in water having features both of a women as well as of a fish, In Object Oriented programming perspective Mermaid can be derived from two classes Women and Fish.

C++ Code:

/*Program to demonstrate simple multiple inheritance*/

class Fish { };

Mermaid

Woman Fish

};

class Mermaid : public Woman , public Fish {

};

Our Mermaid class inherits features of both woman and fish suppose our woman class has method wald() and fish cclass has method swim then our mermaid class can use both methods i.e can walk as well as can swim.

c++ code:

#include <iostream>

#include <stdlib.h>

using namespace std;

/*Program to demonstrate simple multiple inheritance*/

class Fish {

public:

void swim(){

cout<<"\n In method swim";

} };

class Woman {

public:

void walk(){

cout<<"\n In method walk"<<endl;

} };

class Mermaid : public Woman,public Fish {

Mermaid

Woman Fish

void walk() void swim()

};

int main(int argc, char *argv[]) {

Mermaid mermaid;

/*This Mermaid object will have two implicit objects one of Fish class and one of Woman class*/

mermaid.swim();

mermaid.walk();

system("PAUSE");

return 0;

}

Output:

In method4 swim In method walk

Example 2– Multiple Inheritance

Take another example of amphibious vehicle (vehicle that can run on land as well as on water) so it has properties of both land as well as of water vehicle. The general hierarchy in this case will be,

Here we have added a general Vehicle class as well to add all common functions of Land Vehicles and Water Vehicles in that class, and specific functions of Land and Water vehicle in their respective classes then we have derived Amphibious Vehicle class from Land Vehicle and Water Vehicle classes (we can do the same in first example as well concerning Woman, Fish and Mermaid).

Amphibious Vehicle

Land Vehicle Water Vehicle

Vehicle

Car Boat

class Vehicle {

};

class WaterVehicle : public Vehicle {

};

class LandVehicle : public Vehicle {

};

class AmphibiousVehicle : public LandVehicle,public WaterVehicle

{ };

Suppose we have a changeGear method in Vehicle class that is applicable to both water and land vehicle, we also have Float and Move methods in water and land vehicles respectively then our amphibious vehicle will have all these methods,

C++ code:

#include <iostream>

#include <stdlib.h>

using namespace std;

/*Multiple Inheritance in case of Amphibious Vehicle*/

class Vehicle {public:

void changeGear(){ cout<<"\nI am Vehicle changeGear() function..\n";}

};

class WaterVehicle : public Vehicle {

public:

void Float(){ cout<<"\nI am float function of Water Vehicle";}

};

class LandVehicle : public Vehicle

{

public:

void Move(){ cout<<"\nI am move function of Land Vehicle"<<endl;}

};

class AmphibiousVehicle : public LandVehicle,public WaterVehicle

{

};

int main(int argc, char *argv[]) {

AmphibiousVehicle amphibious;

amphibious.Float();

/*Calling Float function of Water Vehicle class*/

amphibious.Move();

/*Calling Move function of Land Vehicle class*/

system("PAUSE");

return 0;

}

Output:

I am float function of Water Vehicle I am move function of Land Vehicle

Advantage of Multiple Inheritance:

As was the case with simple (single) inheritance multiple inheritance also decreases redundant code as we can inherit a class from many classes and can use their functions without the need to write them again.

However there are more disadvantages of multiple inheritance, than its advantages.

Problems with Multiple Inheritance

Amphibious vehicle hierarchy is a complicated as this class is derived from two classes that will make code more complex and less understandable however this is obvious as amphibious vehicle is a complicated vehicle. It is generic problem.

Reduced understanding

Due to increased complexity of class hierarchy the object model becomes difficult it understand especially for someone who is looking it first time.

Duplicate features

As we are deriving a single class from more than one class so there is a chance of duplication of features (same methods in both parents), following problems may arise due to duplicate features,

Problem 1: Ambiguity

Consider the class hierarchy of Mermaid class below,

As mermaid also need to eat and its both parents have their own methods of eating so here question arises,

Which eat operation Mermaid should inherit as both functions are available?

Solution – We can solve this problem by explicitly calling eat method from any of the parent classes in Mermaid class according to behaviour of

Mermaid (i.e. if it eats like a Woman we can call eat method of Woman class and if eats like Fish we can call method of Fish class), for this we will

Override the Common method in multiply inherited class and in that class overridden method we will call the appropriate base class function.

Mermaid

Woman Fish

eat

eat

Example C++ Code

#include <iostream>

#include <stdlib.h>

using namespace std;

/*Program to demonstrate simple multiple inheritance*/

class Fish { public:

void eat(){

cout<<"\n In Fish eat method ";

} };

class Woman {

public:

void eat(){

cout<<"\n In Woman eat method \n"<<endl;

} };

class Mermaid : public Woman,public Fish {

public:

void eat(){

Mermaid

Woman Fish

eat

eat

eat

Override eat method in Mermaid class

Invoke eat operation of desired parent class

Woman::eat();

} };

int main(int argc, char *argv[]) {

Mermaid mermaid;

/*This Mermaid object will have two implicit objects one of Fish class and one of Woman class*/

mermaid.eat();

/*Calling Mermaid eat method*/

system("PAUSE");

return 0;

}

In document (OOP)_01-45(22-08-2009) updated (Page 34-41)

Related documents