Connect with Expertise | Find Experts, Get Answers, Share Insights

multiple inheritence question

Michael C. Starkie
 
Posts: n/a
#1: Jul 19 '05
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?)


Victor Bazarov
 
Posts: n/a
#2: Jul 19 '05

re: multiple inheritence question


"Michael C. Starkie" <mike@bogus.com> wrote...[color=blue]
> 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?)[/color]

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


Dave Rahardja
 
Posts: n/a
#3: Jul 19 '05

re: multiple inheritence question


On Wed, 13 Aug 2003 23:34:53 GMT, "Michael C. Starkie" <mike@bogus.com> wrote:
[color=blue]
>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?)[/color]

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.

Senthilvel Samatharman
 
Posts: n/a
#4: Jul 19 '05

re: multiple inheritence question


> <snip>[color=blue]
> 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.[/color]

<snip>

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




Rolf Magnus
 
Posts: n/a
#5: Jul 19 '05

re: multiple inheritence question


Senthilvel Samatharman wrote:
[color=blue][color=green]
>> <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.[/color]
>
> <snip>
>
> A little confused victor, Is that not 12 wheels as Outback will have
> one Car subobject,one SUV subobject and one FourWheelVehicle
> subobject??[/color]

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.

Closed Thread