By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,227 Members | 1,373 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,227 IT Pros & Developers. It's quick & easy.

multiple inheritence question

P: n/a
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
Share this Question
Share on Google+
4 Replies


P: n/a
"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

P: n/a
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

P: n/a
> <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

P: n/a
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 discussion thread is closed

Replies have been disabled for this discussion.