multiple inheritence question

M

Michael C. Starkie

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?)
 
V

Victor Bazarov

Michael C. Starkie said:
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
 
D

Dave Rahardja

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.
 
R

Rolf Magnus

Senthilvel said:
<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.
 
S

Senthilvel Samatharman

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??
 

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

No members online now.

Forum statistics

Threads
474,139
Messages
2,570,805
Members
47,356
Latest member
Tommyhotly

Latest Threads

Top