Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > multiple inheritence question

Reply
Thread Tools

multiple inheritence question

 
 
Michael C. Starkie
Guest
Posts: n/a
 
      08-13-2003
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?)

 
Reply With Quote
 
 
 
 
Victor Bazarov
Guest
Posts: n/a
 
      08-14-2003
"Michael C. Starkie" <(E-Mail Removed)> 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


 
Reply With Quote
 
 
 
 
Dave Rahardja
Guest
Posts: n/a
 
      08-14-2003
On Wed, 13 Aug 2003 23:34:53 GMT, "Michael C. Starkie" <(E-Mail Removed)> 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.

 
Reply With Quote
 
Rolf Magnus
Guest
Posts: n/a
 
      08-14-2003
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.

 
Reply With Quote
 
Senthilvel Samatharman
Guest
Posts: n/a
 
      08-14-2003
> <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??




 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Multiple Inheritence Neelesh Bodas C++ 5 01-03-2006 08:09 PM
Multiple Inheritence and data attributes Derek Basch Python 1 05-06-2005 09:49 PM
multiple inheritence shruti via JavaKB.com Java 11 04-03-2005 02:25 PM
multiple inheritence shruti tiwari via JavaKB.com Java 3 04-01-2005 12:58 PM
delete, polymorphism, and multiple inheritence Ian McBride C++ 2 07-19-2003 06:15 PM



Advertisments