Return to the C++ Demos Home Page

SAMS Teach Yourself C++ in 21 Days

Chapter Thirteen

Prog 1 :  Multiple Inheritance

// STYC++ in 21 chapter 13 prog 1
//
//   Example of Multiple Inheritance
//
//

#include <iostream.h>

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 << "I can fly! I can fly! I can 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 MNumber = 2;
int main()
{
	Horse* Ranch[MNumber];
	Bird* Aviary[MNumber];
	Horse * pHorse;
	Bird * pBird;
	int choice,i;

	
	for (i=0; i<MNumber; i++)
	{
		cout << "\n(1)Horse (2)Pegasus: ";
		cin >> choice;
		
		if (choice == 2)  pHorse = new Pegasus;
		else pHorse = new Horse;
		
		Ranch[i] = pHorse;
	}

	for (i=0; i<MNumber; i++)
	{
		cout << "\n(1)Bird (2)Pegasus: ";
		cin >> choice;
	
		if (choice == 2) pBird = new Pegasus;
		else pBird = new Bird;
		
		Aviary[i] = pBird;
	}

	cout << "\n";

	for (i=0; i<MNumber; i++)
	{
		cout << "\nRanch[" << i << "]: " ;
		Ranch[i]->Whinny();
		delete Ranch[i];
	}

	for (i=0; i<MNumber; i++)
	{
		cout << "\nAviary[" << i << "]: " ;
		Aviary[i]->Chirp();
		Aviary[i]->Fly();
		delete Aviary[i];
		cout << endl;
	}

	return 0;

}

Prog 2 : Constructors from both classes called and member func ambiguity fixin
(Actually got this code from the SAMS site and just commented at the spots that 
show you the pertinent point .  Believe it or not, I have been retyping this up to now.  Code reuse baby.)

//  STYC++ in 21 chapter 13 prog 2 
//
//  Showing how to satisfy both constructors of two inherited 
//  classes and how to resolve an ambiguity when the classes both 
//  inherit from the same base class
//
//  Ugh.


#include <iostream>
using namespace std;

typedef int HANDS;

enum COLOR { Red, Green, Blue, Yellow, White, Black, Brown } ;


class Animal        // common base to both horse and bird
{
public:
	Animal(int);
	virtual ~Animal() { cout << "Animal destructor...\n"; }
	virtual int GetAge() const { return itsAge; }
	virtual void SetAge(int age) { itsAge = age; }
private:
	int itsAge;
};

Animal::Animal(int age):
itsAge(age)
{
	cout << "Animal constructor...\n";
}

class Horse : public Animal   // Horse class
{
    public:
	Horse(COLOR color, HANDS height, int age);
	virtual ~Horse() { cout << "Horse destructor...\n"; }
	virtual void Whinny()const { cout << "Whinny!... "; }
	virtual HANDS GetHeight() const { return itsHeight; }
	virtual COLOR GetColor() const { return itsColor; }
    protected:
	HANDS itsHeight;
	COLOR itsColor;
};

Horse::Horse(COLOR color, HANDS height, int age):
Animal(age),
itsColor(color),itsHeight(height)
{
	cout << "Horse constructor...\n";
}

class Bird : public Animal   // Bird class.. It scores and rebounds.
{
    public:
	Bird(COLOR color, bool migrates, int age);
	virtual ~Bird() {cout << "Bird destructor...\n";  }
	virtual void Chirp()const { cout << "Chirp... ";  }
	virtual void Fly()const
				{ cout << "I can fly! I can fly! I can fly! "; }
	virtual COLOR GetColor()const { return itsColor; }
	virtual bool GetMigration() const { return itsMigration; }
    protected:
	COLOR itsColor;
	bool itsMigration;
};

Bird::Bird(COLOR color, bool migrates, int age):
Animal(age),
itsColor(color), itsMigration(migrates)
{
	cout << "Bird constructor...\n";
}

class Pegasus : public Horse, public Bird
{
    public:
	void Chirp()const { Whinny(); }
	Pegasus(COLOR, HANDS, bool, long, int);
	virtual ~Pegasus() {cout << "Pegasus destructor...\n";}
	virtual long GetNumberBelievers() const
	{ return  itsNumberBelievers; }
	virtual COLOR GetColor()const { return Horse::itsColor; }  // no ambiguity
	virtual int GetAge() const { return Horse::GetAge(); }
    private:
	long itsNumberBelievers;
};

Pegasus::Pegasus(
	COLOR aColor,
	HANDS height,
	bool migrates,
	long NumBelieve,
	int age):
	Horse(aColor, height,age),    // calling constructor of one class
	Bird(aColor, migrates,age),   // then another......
	itsNumberBelievers(NumBelieve)
{
	cout << "Pegasus constructor...\n";
}


// the driver	
int main()
{
	Pegasus *pPeg = new Pegasus(Red, 5, true, 10, 2);
	int age = pPeg->GetAge();
	
	cout << "This pegasus is " << age << " years old.\n";
	delete pPeg;
	
	return 0;
} // eos

Return to the C++ Demos Home Page