multiple inheritence question

Discussion in 'C++' started by Michael C. Starkie, Aug 14, 2003.

  1. 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?)
     
    Michael C. Starkie, Aug 14, 2003
    #1
    1. Advertising

  2. "Michael C. Starkie" <> 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
     
    Victor Bazarov, Aug 14, 2003
    #2
    1. Advertising

  3. On Wed, 13 Aug 2003 23:34:53 GMT, "Michael C. Starkie" <> 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.
     
    Dave Rahardja, Aug 14, 2003
    #3
  4. Michael C. Starkie

    Rolf Magnus Guest

    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.
     
    Rolf Magnus, Aug 14, 2003
    #4
  5. > <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??
     
    Senthilvel Samatharman, Aug 14, 2003
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. shruti tiwari via JavaKB.com

    multiple inheritence

    shruti tiwari via JavaKB.com, Mar 30, 2005, in forum: Java
    Replies:
    3
    Views:
    329
    shruti tiwari via JavaKB.com
    Apr 1, 2005
  2. shruti  via JavaKB.com

    multiple inheritence

    shruti via JavaKB.com, Mar 30, 2005, in forum: Java
    Replies:
    11
    Views:
    567
    Thomas G. Marshall
    Apr 3, 2005
  3. Ian McBride
    Replies:
    2
    Views:
    428
    Alf P. Steinbach
    Jul 19, 2003
  4. Derek Basch

    Multiple Inheritence and data attributes

    Derek Basch, May 6, 2005, in forum: Python
    Replies:
    1
    Views:
    232
  5. Neelesh Bodas

    Multiple Inheritence

    Neelesh Bodas, Jan 3, 2006, in forum: C++
    Replies:
    5
    Views:
    335
    mlimber
    Jan 3, 2006
Loading...

Share This Page