473,507 Members | 2,477 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

multiple inheritence question

Car is a class, with four wheel variables. SUV is a class with four
wheel variables. Outback is a class that inherits from Car and SUV.
How many wheel variables does Outback have? (In general, how does
multiple inheritance work?)

Jul 19 '05 #1
4 2456
"Michael C. Starkie" <mi**@bogus.com> wrote...
Car is a class, with four wheel variables. SUV is a class with four
wheel variables. Outback is a class that inherits from Car and SUV.
How many wheel variables does Outback have? (In general, how does
multiple inheritance work?)


Well, in presented hierarchy the Outback object will have 8 wheels.
4 in its Car subobject, 4 in its SUV subobject. However, it would
probably be more correct to define the hierarchy this way:

class FourWheelVehicle {
Wheel wheels[4];
...
};

class Car : public virtual FourWheelVehicle { .. };
class SUV : public virtual FourWheelVehicle { .. };
class Outback : public Car, public SUV { .. };

which will provide you with only one 'FourWheelVehicle' subobject in
Outback, and therefore only four wheels.

The reason I suggest extracting FourWheelVehicle into a separate
base class is because there is too much common between Car and SUV
to ignore that. The common functionality and implementation should
be placed in the common base class.

Victor
Jul 19 '05 #2
On Wed, 13 Aug 2003 23:34:53 GMT, "Michael C. Starkie" <mi**@bogus.com> wrote:
Car is a class, with four wheel variables. SUV is a class with four
wheel variables. Outback is a class that inherits from Car and SUV.
How many wheel variables does Outback have? (In general, how does
multiple inheritance work?)


Why does SUV not inherit from Car? That would solve your four-wheel problem.

Multiple inheritance is exactly what it says--your derived class will have a
combination of members from all of its parents.

Jul 19 '05 #3
> <snip>
class FourWheelVehicle {
Wheel wheels[4];
...
};

class Car : public virtual FourWheelVehicle { .. };
class SUV : public virtual FourWheelVehicle { .. };
class Outback : public Car, public SUV { .. };

which will provide you with only one 'FourWheelVehicle' subobject in
Outback, and therefore only four wheels.


<snip>

A little confused victor, Is that not 12 wheels as Outback will have
one Car subobject,one SUV subobject and one FourWheelVehicle subobject??


Jul 19 '05 #4
Senthilvel Samatharman wrote:
<snip>
class FourWheelVehicle {
Wheel wheels[4];
...
};

class Car : public virtual FourWheelVehicle { .. };
class SUV : public virtual FourWheelVehicle { .. };
class Outback : public Car, public SUV { .. };

which will provide you with only one 'FourWheelVehicle' subobject in
Outback, and therefore only four wheels.


<snip>

A little confused victor, Is that not 12 wheels as Outback will have
one Car subobject,one SUV subobject and one FourWheelVehicle
subobject??


I could understand if you think 8, but where would the extra
FourWheelVehicle come from?
Anyway, since Car and SUV inherit virtually from FourWheelVehicle, there
will only be one copy of that base class in Outback. The Car and SUV
subobjects of Outback share their FourWheelsVehicle.

Jul 19 '05 #5

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

Similar topics

2
4321
by: Graham Banks | last post by:
Does using multiple inheritance introduce any more performance overhead than single inheritance?
7
1959
by: preetam | last post by:
Hi, This question is more towards design than towards c++ details. By looking at books on design patterns and various google threads on the same topic, I see that composition is favoured to...
5
2095
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...
22
23317
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...
5
1418
by: Bob Rundle | last post by:
I heard that managed C++ in VS 2005 supports multiple inheritence. Is this true? Bob Rundle
7
3371
by: serge | last post by:
How can I run a single SP by asking multiple sales question either by using the logical operator AND for all the questions; or using the logical operator OR for all the questions. So it's always...
5
2109
by: Neelesh Bodas | last post by:
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...
47
3953
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...
4
3331
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
7321
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,...
0
7377
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
7488
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5623
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 project—planning, coding, testing,...
0
4702
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...
0
3191
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...
0
3179
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1544
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 ...
0
412
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.