Multiple Inheritance and virtual inheritance

Unlike other languages, C++ allows multiple inheritance.

Unfortunately multiple inheritance leads to lots of problems: If a variable or method is declared in more than one superclass.

Therefore we wil not do discuss many details. If you're interested, read pages 163 - 166 of C++ in a nutshell.

Here are some guidelines to make multiple inheritance feasable. This is what Java and Delphi do.

A class may inherit from one "regular" superclass.

All other superclass must be pure abstract (interfaces) which means: Only pure virtual functions (no implementations) and no attributes (variables).

Why would you need multiple inheritance? Everytime something is multiple things at one time. E.g. a "FlyingCar" could be a subclass of "Car" and of "Airplane".

Example:

class Car {
public:
  virtual void driveTo(Location *l)=0;
};

class Airplane {
public:
  virtual void flyTo(Location *l)=0;
};

class FlyingCar : public Car, public Airplane {
public:
  virtual void flyTo(Location *l);
  virtual void driveTo(Location *l);
};

Now suddently getter methods make much more sense! A superclass can define virtual getter and setter methods without actually defining the attribute!

Example:

class Airplane {
public:
  virtual int getHeightAboveGround()=0;
  virtual void setHeightAboveGround(int h)=0;
};

//Practice: Define the following classes:

Notes on virtual inheritance