Multiple Inheritence

N

Neelesh Bodas

This might be slightly off-topic.

Many books on C++ consider multiple inheritence as an "advanced"
concept. Bruce Eckel says in TICPP, volume 2 that "there was (and
still is) a lot of disagreement about whether [multiple inheritence]
is essential in C++".

Are there any disadvantages of using multiple inheritence?
 
V

Victor Bazarov

Neelesh said:
This might be slightly off-topic.

Many books on C++ consider multiple inheritence as an "advanced"
concept. Bruce Eckel says in TICPP, volume 2 that "there was (and
still is) a lot of disagreement about whether [multiple inheritence]
is essential in C++".

Are there any disadvantages of using multiple inheritence?

Definitely. It forces you to think a bit, like any other part of the
language which you've never used before. It also forces others who are
to read your code to think a bit: class hierarchies can become rather
non-trivial. Of course, qualifying all those things as disadvantages
would be subjective, and it does get outweighed by better design and
abstraction of concepts you can achieve with MI, not that one necessarily
does achieve those goals, but one can.

V
 
G

Guest

Neelesh said:
This might be slightly off-topic.

Many books on C++ consider multiple inheritence as an "advanced"
concept. Bruce Eckel says in TICPP, volume 2 that "there was (and
still is) a lot of disagreement about whether [multiple inheritence]
is essential in C++".

Are there any disadvantages of using multiple inheritence?

I'm mainly a client of hierarchies based on multiple inheritance
(e.g. I use ATL/WTL) and solutions I use are pretty easy to understand.
But I've never created my own solution based on multiple inheritance.
From my - the architect/developer - side multiple inheritance always
seems as increasing complexity of solution and not very obvious.

So, I'd say multiple inheritance seems to be good tool but only in
hands of very experienced developers.

Cheers
 
E

Earl Purple

Neelesh said:
This might be slightly off-topic.

Many books on C++ consider multiple inheritence as an "advanced"
concept. Bruce Eckel says in TICPP, volume 2 that "there was (and
still is) a lot of disagreement about whether [multiple inheritence]
is essential in C++".

Are there any disadvantages of using multiple inheritence?

Well the disadvantage is the diamond of course and any other clashing.

The advantage is that your class can implement 2 properties and define
from 2 base-classes (preferably abstract ones) that each provide an
interface to one property.
 
D

Derek

Neelesh said:
This might be slightly off-topic.
Many books on C++ consider multiple inheritence as an "advanced"
concept. Bruce Eckel says in TICPP, volume 2 that "there was (and
still is) a lot of disagreement about whether [multiple inheritence]
is essential in C++".
Are there any disadvantages of using multiple inheritence?

It may not be essential, but it's pretty useful at times, and is
certainly used in the standard C++ library (iostream for example
combines istream and ostream).

I wouldn't say that there are any real disadvantages other than added
complexity, but it all depends on your application.

Here's a simple example:


#include <iostream>

using namespace std;

class refillable
{
protected:
int max;
int current;
public:
int gauge() { return current; }
bool fill(int amount)
{
if ( amount <= max )
current = amount;
else current = max;
}
bool consume()
{
if ( !current )
return false;
--current;
return true;
}
};

class Battery: public refillable
{
public:
Battery() { max = 100; current = 100; }
bool charge()
{
if ( current < max )
++current;
return true;
}
};

class Fuel: public refillable
{
public:
Fuel() { max = 500; current = 0; }
};

class Lubricant: public refillable
{
public:
Lubricant() { max = 20; current = 20; }
};

class Engine
{
protected:
bool running;
public:
Fuel gas;
Lubricant oil;
Battery battery;
virtual bool start()
{
if ( !gas.gauge() || !oil.gauge() || !battery.gauge() )
return false;
gas.consume();
battery.consume();
return (running = true);
}
virtual bool stop() { running = false; return true; }
virtual bool isRunning() { return running; }
};

class Transmission
{
public:
typedef enum { gReverse = -1, gNeutral = 0, gFirst, gSecond,
gThird, gFourth, gFifth } gear_type;
virtual gear_type inGear() { return in_gear; }
virtual bool changeGear(gear_type gear) { in_gear = gear; return
true; }
Transmission() { in_gear = gNeutral; }
protected:
gear_type in_gear;
};

class Vehicle: virtual public Engine, virtual public Transmission
{
};

int main()
{
Vehicle car;

if ( !car.start() )
{
cout << "Car won't start" << endl;
if ( !car.gas.gauge() )
{
cout << "Adding some gas" << endl;
car.gas.fill(10);
}
if ( !car.oil.gauge() )
{
cout << "Adding some oil" << endl;
car.oil.fill(5);
}
if ( !car.battery.gauge() )
cout << "Battery is dead" << endl;
if ( !car.start() )
{
cout << "Car still won't start" << endl;
return 0;
}
else
cout << "Car starts now" << endl;
}
else
cout << "Car started" << endl;

car.changeGear(Transmission::gFirst);

return 0;
}
 
M

mlimber

Derek said:
Neelesh said:
This might be slightly off-topic.
Many books on C++ consider multiple inheritence as an "advanced"
concept. Bruce Eckel says in TICPP, volume 2 that "there was (and
still is) a lot of disagreement about whether [multiple inheritence]
is essential in C++".
Are there any disadvantages of using multiple inheritence?

It may not be essential, but it's pretty useful at times, and is
certainly used in the standard C++ library (iostream for example
combines istream and ostream).

I wouldn't say that there are any real disadvantages other than added
complexity, but it all depends on your application.

Here's a simple example:


#include <iostream>

using namespace std;

class refillable
{
protected:
int max;
int current;
public:
int gauge() { return current; }
bool fill(int amount)
{
if ( amount <= max )
current = amount;
else current = max;
}
bool consume()
{
if ( !current )
return false;
--current;
return true;
}
};

class Battery: public refillable
{
public:
Battery() { max = 100; current = 100; }
bool charge()
{
if ( current < max )
++current;
return true;
}
};

class Fuel: public refillable
{
public:
Fuel() { max = 500; current = 0; }
};

class Lubricant: public refillable
{
public:
Lubricant() { max = 20; current = 20; }
};

class Engine
{
protected:
bool running;
public:
Fuel gas;
Lubricant oil;
Battery battery;
virtual bool start()
{
if ( !gas.gauge() || !oil.gauge() || !battery.gauge() )
return false;
gas.consume();
battery.consume();
return (running = true);
}
virtual bool stop() { running = false; return true; }
virtual bool isRunning() { return running; }
};

class Transmission
{
public:
typedef enum { gReverse = -1, gNeutral = 0, gFirst, gSecond,
gThird, gFourth, gFifth } gear_type;
virtual gear_type inGear() { return in_gear; }
virtual bool changeGear(gear_type gear) { in_gear = gear; return
true; }
Transmission() { in_gear = gNeutral; }
protected:
gear_type in_gear;
};

class Vehicle: virtual public Engine, virtual public Transmission
{
};
[snip]

It's better to use private inheritance here. Better still would be to
use simple composition rather than inheritance.

http://www.parashift.com/c++-faq-lite/private-inheritance.html#faq-24.2
http://www.parashift.com/c++-faq-lite/private-inheritance.html#faq-24.3

Cheers! --M
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,056
Latest member
GlycogenSupporthealth

Latest Threads

Top