Polymorphism 465
14
LISTING14.5 Common Base Classes
0: // Listing 14.5
1: // Common base classes
2:
3: #include <iostream>
4: using namespace std;
5:
6: typedef int HANDS;
7: enum COLOR { Red, Green, Blue, Yellow, White, Black, Brown } ;
8:
9: class Animal // common base to both horse and bird
10: {
11: public:
12: Animal(int);
13: virtual ~Animal() { cout << “Animal destructor...\n”; }
14: virtual int GetAge() const { return itsAge; }
15: virtual void SetAge(int age) { itsAge = age; }
16: private:
17: int itsAge;
18: };
19:
20: Animal::Animal(int age):
21: itsAge(age)
22: {
23: cout << “Animal constructor...\n”;
24: }
25:
26: class Horse : public Animal
27: {
28: public:
29: Horse(COLOR color, HANDS height, int age);
30: virtual ~Horse() { cout << “Horse destructor...\n”; }
31: virtual void Whinny()const { cout << “Whinny!... “; }
32: virtual HANDS GetHeight() const { return itsHeight; }
33: virtual COLOR GetColor() const { return itsColor; }
34: protected:
35: HANDS itsHeight;
36: COLOR itsColor;
37: };
38:
39: Horse::Horse(COLOR color, HANDS height, int age):
40: Animal(age),
41: itsColor(color),itsHeight(height)
42: {
43: cout << “Horse constructor...\n”;
44: }
45:
46: class Bird : public Animal
47: {
48: public: