Multiple Inheritance to have the features from both parent : multiple base classes « Class « C++ Tutorial






#include <iostream>
using std::cout;
using std::cin;
using std::endl;

class Horse
{
  public:
    Horse() { cout << "Horse constructor... "; }
    virtual ~Horse() { cout << "Horse destructor... "; }
    virtual void Whinny() const { cout << "Whinny!... "; }
  private:
    int itsAge;
};

class Bird
{
  public:
    Bird() { cout << "Bird constructor... "; }
    virtual ~Bird() { cout << "Bird destructor... "; }
    virtual void Chirp() const { cout << "Chirp... ";  }
    virtual void Fly() const
    {
       cout << "fly! ";
    }
  private:
    int itsWeight;
};

class Pegasus : public Horse, public Bird
{
  public:
    void Chirp() const { Whinny(); }
    Pegasus() { cout << "Pegasus constructor... "; }
    ~Pegasus() { cout << "Pegasus destructor...  "; }
};

const int MagicNumber = 2;
int main(){
   Horse* Ranch[MagicNumber];
   Bird* Aviary[MagicNumber];
   Horse * pHorse;
   Bird * pBird;
   
   pHorse = new Pegasus;
   pHorse = new Horse;
   Ranch[0] = pHorse;

   pBird = new Pegasus;
   pBird = new Bird;
   Aviary[0] = pBird;

   Ranch[0]->Whinny();
   delete Ranch[0];
   
   Aviary[0]->Chirp();
   Aviary[0]->Fly();
   delete Aviary[0];

   return 0;
}








9.19.multiple base classes
9.19.1.An example of multiple base classes.
9.19.2.multiple inheritance with employees and degrees
9.19.3.multiple inheritance with English Distances
9.19.4.Multiple Inheritance to have the features from both parent
9.19.5.Resolving Ambiguity in Case of Multiple Inheritance Involving Common Base Classes
9.19.6.Multiple Inheritance in both private and public way
9.19.7.In cases of multiple inheritance: Constructors are called in order of derivation, destructors in reverse order
9.19.8.Multiple inheritance example