473,769 Members | 2,143 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Multiple Inheritence

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?

Jan 3 '06 #1
5 2124
Neelesh Bodas wrote:
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
Jan 3 '06 #2
Neelesh Bodas wrote:
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
--
Mateusz Łoskot
http://mateusz.loskot.net
Jan 3 '06 #3

Neelesh Bodas wrote:
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.

Jan 3 '06 #4
Neelesh Bodas wrote:
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.ga uge() )
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::g First);

return 0;
}

Jan 3 '06 #5
Derek wrote:
Neelesh Bodas wrote:
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-lit....html#faq-24.2
http://www.parashift.com/c++-faq-lit....html#faq-24.3

Cheers! --M

Jan 3 '06 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
4144
by: John | last post by:
Hi, I am trying to create a class heirarchy similar to the following: // base interface class ICar { public: virtual void start() = 0; }; // add members to that interface, but retain base capabilities
2
4338
by: Graham Banks | last post by:
Does using multiple inheritance introduce any more performance overhead than single inheritance?
0
1534
by: agrawal_rajesh | last post by:
Its interesting to know that We can create multiple interfaces in a single class in visiual studio 6.0 ATL. But I am surprised to see that we can't create the same in Visual studio .Net. In .Net Every Class must have only one Interface. I need to create multiple interfaces in a single class so that I can have only one CLSID and client should call coCreateInstance only once. If any body has worked in this in .Net then pls let me know the...
20
10084
by: km | last post by:
Hi all, In the following code why am i not able to access class A's object attribute - 'a' ? I wishto extent class D with all the attributes of its base classes. how do i do that ? thanks in advance for enlightment ... here's the snippet #!/usr/bin/python
5
2113
by: Tony Johansson | last post by:
Hello Experts! I just play around just to try to understand this about multiple inheritance. You have all the class definition below and at the bottom you have the main program. So here I use multiple inheritance. One top class called MBase and below this we have D1 and D2 on the same level.
22
23384
by: Matthew Louden | last post by:
I want to know why C# doesnt support multiple inheritance? But why we can inherit multiple interfaces instead? I know this is the rule, but I dont understand why. Can anyone give me some concrete examples?
5
1436
by: Bob Rundle | last post by:
I heard that managed C++ in VS 2005 supports multiple inheritence. Is this true? Bob Rundle
47
4034
by: Larry Smith | last post by:
I just read a blurb in MSDN under the C++ "ref" keyword which states that: "Under the CLR object model, only public single inheritance is supported". Does this mean that no .NET class can ever support multiple inheritance. In C++ for instance I noticed that the compiler flags an error if you use the "ref" keyword on a class with multiple base classes. This supports the above quote. However, under the "CodeClass2.Bases" property (part...
4
3350
by: Mukesh_Singh_Nick | last post by:
Does PHP support multiple inheritence? If not, how would one workaround it when one needs a class to inherit from multiple classes?
0
9579
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, well explore What is ONU, What Is Router, ONU & Routers main usage, and What is the difference between ONU and Router. Lets take a closer look ! Part I. Meaning of...
0
9422
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10208
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
9987
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8867
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development projectplanning, coding, testing, and deploymentwithout human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7404
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupr who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6662
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5294
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
3952
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.