Multiple Inheritance Question

This is a discussion on Multiple Inheritance Question within the C++ Programming forums, part of the General Programming Boards category; Hola Amigos: Fresh out of an exam... okay.. one base class called Vehicle 4 derived classes: Airplane and Bus from ...

  1. #1
    Registered User
    Join Date
    Jan 2002
    Posts
    63

    Multiple Inheritance Question

    Hola Amigos:

    Fresh out of an exam...

    okay.. one base class called Vehicle

    4 derived classes: Airplane and Bus from Vehicle
    and: Car and Truck from Bus

    Vehicle had a constructor declared ...

    all the subsequent classes did not re-declare any constructors...
    so my thought was when you called them you made them all of the type vehicle..


    is this correct? why/why not?
    SS3X

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >all the subsequent classes did not re-declare any constructors...
    If no constructors are declared then the compiler will create default constructors. So if you have this code:
    Code:
    class Vehicle
    {
    public:
      int type;
    
      Vehicle()
        : type ( 1 )
      {}
    };
    
    class Bus : public Vehicle
    {
    };
    The compiler actually creates a default constructor for Bus:
    Code:
    class Bus : public Vehicle
    {
    public:
      Bus()
        : Vehicle()
      {}
    };
    >so my thought was when you called them you made them all of the type vehicle..
    They are. Since Bus is derived from Vehicle, a Bus is a Vehicle. However, if you declare the object to be that of a Vehicle you will lose any extras that Bus added.

    -Prelude
    My best code is written with the delete key.

  3. #3
    Seeking motivation... endo's Avatar
    Join Date
    May 2002
    Posts
    537
    This sint multiple inheritance though, that would be like this:

    class Vehicle
    {
    }

    class Bus : virtual public Vehicle
    {
    }

    class Aircraft : virtual public Vehicle
    {
    }

    class AirBuse : public Bus, public AirCraft
    {
    }

    This is not recommended though and many languages dont even allow it, just thought you'd like to know
    Couldn't think of anything interesting, cool or funny - sorry.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Multiple If Statements Question.
    By thekautz in forum C++ Programming
    Replies: 3
    Last Post: 11-07-2008, 03:06 PM
  2. Question about classes and inheritance
    By ~Kyo~ in forum C++ Programming
    Replies: 4
    Last Post: 10-23-2004, 06:36 PM
  3. Inheritance question
    By CompiledMonkey in forum C++ Programming
    Replies: 5
    Last Post: 12-19-2003, 01:20 PM
  4. Multiple Inheritance Ambiguity
    By FillYourBrain in forum C++ Programming
    Replies: 21
    Last Post: 08-23-2002, 11:31 AM
  5. Multiple virtual inheritance
    By kitten in forum C++ Programming
    Replies: 3
    Last Post: 08-10-2001, 11:04 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21