类的综合案例——纯虚函数与抽象类( 加强对接口与多态,以及派生类构造函数的理解 )
注意派生类构造函数的写法。
1 #include <iostream> 2 3 using namespace std; 4 5 enum COLOR { Red, Green, Blue, Yellow, White, Black, Brown }; 6 7 class Animal 8 { 9 public: 10 Animal() = default; 11 Animal(int); 12 virtual ~Animal(); 13 virtual int GetAge() const { return itsAge; } 14 virtual void SetAge(int age) { itsAge = age; } 15 virtual void Sleep() const = 0; //抽象基类,有5个纯虚函数 16 virtual void Eat() const = 0; 17 virtual void Reproduce() const = 0; //繁殖 18 virtual void Move() const = 0; 19 virtual void Speak() const = 0; 20 private: 21 int itsAge; 22 }; 23 24 Animal::Animal(int age): itsAge(age) 25 { 26 cout << "Animal构造函数被调用...\n"; 27 } 28 Animal::~Animal() 29 { 30 cout << "Animal析构函数被调用...\n"; 31 } 32 33 class Mammal : public Animal 34 { 35 public: 36 Mammal() = default; 37 Mammal(int age) : Animal(age) //将age传递给基类Animal的构造函数,使用基类的构造函数构造派生类的基类部分的成员 38 { 39 cout << "Mammal构造函数被调用...\n"; 40 } 41 virtual ~Mammal() 42 { 43 cout << "Mammal析构函数被调用...\n"; 44 } 45 virtual void Reproduce() const //只覆盖了基类Animal的Reproduce函数,基类的其他4个纯虚函数没有重写,所以Mammal类还是一个抽象类(一个具有4个纯虚函数的抽象类) 46 { 47 cout << "Mammal reproduction depicted...\n"; 48 } 49 }; 50 51 class Fish : public Animal 52 { 53 public: 54 Fish() = default; 55 Fish(int age) : Animal(age) //将age传递给基类Animal的构造函数,使用基类的构造函数构造派生类的基类部分的成员 56 { 57 cout << "Fish构造函数被调用...\n"; 58 } 59 virtual ~Fish() 60 { 61 cout << "Fish的析构函数被调用...\n"; 62 } 63 virtual void Sleep() const { cout << "Fish sleeping...\n"; } //Fish类实现了Animal类的所有纯虚函数,所以Fish类不是抽象类 64 virtual void Eat() const { cout << "fish eating...\n"; } 65 virtual void Reproduce() const { cout << "fish reproducing...\n"; } 66 virtual void Move() const { cout << "Fish moving...\n"; } 67 virtual void Speak() const { cout << "Fish speaking...\n"; }; 68 }; 69 70 class Horse : public Mammal 71 { 72 public: 73 Horse() = default; 74 Horse(int age, COLOR color) : Mammal(age), itsColor(color) 75 { 76 cout << "Horse构造函数被调用...\n"; 77 } 78 virtual ~Horse() 79 { 80 cout << "Horse析构函数被调用...\n"; 81 } 82 virtual void Speak() const { cout << "Horse speaking\n"; }//由于Horse继承于Mammal,Mammal已经实现了Animal的Reproduce纯虚函数,所以horse只需实现剩余4个纯虚函数即可 83 virtual void Sleep() const { cout << "Horse sleeping...\n"; } 84 virtual void Eat() const { cout << "Horse eating"; } 85 virtual void Move() const { cout << "Horse Moving...\n"; } 86 virtual COLOR GetItsColor() const { return itsColor; } //在继承关系中,最好将所有成员函数都声明为虚函数 87 protected: 88 COLOR itsColor; //COLOR为前面定义的枚举类型 89 }; 90 91 class Dog : public Mammal 92 { 93 public: 94 Dog() = default; 95 Dog(int age, COLOR color) : Mammal(age), itsColor(color) 96 { 97 cout << "Dog构造函数被调用...\n"; 98 } 99 virtual ~Dog() 100 { 101 cout << "Dog析构函数被调用...\n"; 102 } 103 virtual void Sleep() const { cout << "Dog sleeping...\n"; } 104 virtual void Eat() const { cout << "Dog eating...\n"; } 105 virtual void Reproduce() const { cout << "Dog reproducing...\n"; } //覆盖了父类Mammal的reproduce函数 106 virtual void Move() const { cout << "Dog moving...\n"; } 107 virtual void Speak() const { cout << "Dog speaking...\n"; } 108 protected: 109 COLOR itsColor; 110 }; 111 112 int main() 113 { 114 Animal *pAnimal = nullptr; 115 int choice; 116 bool fQuit = false; 117 while (fQuit == false) 118 { 119 cout << "(1)Dog (2)Horse (3)Fish (0)Quit: "; 120 cin >> choice; 121 122 switch (choice) //pAnimal可以指向其任意的子类对象 123 { //此处体现了类的多态性 124 case 1: 125 pAnimal = new Dog(5, Brown); 126 break; 127 case 2: 128 pAnimal = new Horse(4, Black); 129 break; 130 case 3: 131 pAnimal = new Fish(5); 132 break; 133 default: 134 fQuit = true; 135 break; 136 } 137 if (fQuit == false) 138 { 139 pAnimal->Speak(); 140 pAnimal->Eat(); 141 pAnimal->Reproduce(); 142 pAnimal->Move(); 143 pAnimal->Sleep(); 144 delete pAnimal; 145 cout << endl; 146 } 147 } 148 149 return 0; 150 }