cc32b_demo-32dk2j_cpp_纯虚函数与抽象类2-txwtech
cc32b_demo-32dk2j_cpp_纯虚函数与抽象类2-txwtech
//纯虚函数是用来继承用的
//纯虚函数
//抽象类-抽象数据类型
//*任何包含一个或者多个纯虚函数的类都是抽象类
//*不要/不能创建这个类的对象,应该/只能继承它
//*务必覆盖从这个类继承的纯虚函数
//实现纯虚函数-----------可写可以不写
//C++接口
//就是只包含纯虚函数的抽象基类
//一个指向基类的指针可以指向它的派生类,指向它的子子孙孙
//通常在编程语句中用 abstract 修饰的类是抽象类。在C++中,含有纯虚拟函数的类称为抽象类,它不能生成对象;在java中,含有抽象方法的类称为抽象类,同样不能生成对象。
//抽象类是不完整的,它只能用作基类。在面向对象方法中,抽象类主要用来进行类型隐藏和充当全局变量的角色。
1 #include <iostream>//txwtech,cc32b_demo-纯虚函数与抽象类demo2 2 using namespace std; 3 //一个指向基类的指针可以指向它的派生类,指向它的子子孙孙 4 //通常在编程语句中用 abstract 修饰的类是抽象类。在C++中,含有纯虚拟函数的类称为抽象类,它不能生成对象;在java中,含有抽象方法的类称为抽象类,同样不能生成对象。 5 6 //抽象类是不完整的,它只能用作基类。在面向对象方法中,抽象类主要用来进行类型隐藏和充当全局变量的角色。 7 8 9 enum COLOR {Red,Green,Blue,Yellow,White,Black,Brown}; 10 11 class Animal 12 { 13 public: 14 Animal(int); 15 virtual ~Animal() { cout << "Animal析构函数被调用...\n"; } 16 virtual int GetAge() const { return itsAge; } 17 virtual void SetAge(int age) { itsAge = age; } 18 virtual void Sleep() const = 0;//纯虚函数 19 virtual void Eat() const = 0; 20 virtual void Reproduce() const = 0; 21 virtual void Move() const = 0; 22 virtual void Speak() const = 0; 23 24 private: 25 int itsAge; 26 }; 27 Animal::Animal(int age) :itsAge(age) 28 { 29 cout << "Animal构造函数被调用。。。、\n"; 30 } 31 class Mammal :public Animal 32 { 33 public: 34 Mammal(int age) :Animal(age) 35 { 36 cout << "Mammal构造函数被调用。。。\n"; 37 } 38 virtual ~Mammal() { cout << "Mammal析构函数被调用..."; } 39 virtual void Reproduce() const 40 { 41 cout << "Mammal Reproduction depiacted...\n"; 42 } 43 }; 44 class Fish :public Animal 45 { 46 public: 47 Fish(int age) :Animal(age) 48 { 49 cout << "fish的构造函数被调用。。。\n"; 50 } 51 virtual ~Fish() 52 { 53 cout << "fish的析构函数被调用。。。\n"; 54 } 55 virtual void Sleep() const { cout << "fish snoring...\n"; } 56 virtual void Eat() const { cout << "fish feeding...\n"; } 57 virtual void Reproduce() const { cout << "fish laying eggs...\n"; } 58 virtual void Move() const { cout << "fish swimming...\n"; } 59 virtual void Speak() const {} 60 61 }; 62 class Horse :public Mammal 63 { 64 public: 65 Horse(int age, COLOR color) :Mammal(age), itsColor(color) 66 { 67 cout << "horse构造函数被调用。。。\n"; 68 } 69 virtual ~Horse() 70 { 71 cout << "horse析构函数被调用。。。\n"; 72 } 73 virtual void Speak() const {cout<<"Whinny!...\n";} 74 virtual void Sleep() const {cout<<"Horse snoring ...\n";} 75 virtual COLOR GetItsColor() const { return itsColor; } 76 virtual void Eat() const { cout << "Horse feeding...\n"; } 77 virtual void Move() const { cout << "horse running...\n"; } 78 protected: //虽然是私有,但可以被继承 79 COLOR itsColor; 80 81 }; 82 class Dog :public Mammal 83 { 84 public: 85 Dog(int age, COLOR color) :Mammal(age), itsColor(color) 86 { 87 cout << "dog构造函数被调用\n"; 88 } 89 virtual ~Dog() 90 { 91 cout << "dog析构函数被调用\n"; 92 } 93 virtual void Speak() const { cout << "dog Whoof...\n"; } 94 virtual void Sleep() const { cout << "dog Snoring...\n"; } 95 virtual void Eat() const { cout << "dog Eatting...\n"; } 96 virtual void Move() const { cout << "dog running...\n"; } 97 virtual void Reproduce() const 98 { 99 cout << "Dogs reproducing...\n"; 100 } 101 protected: 102 COLOR itsColor; 103 104 }; 105 106 int main() 107 { 108 Animal *pAnimal = 0; 109 int choice; 110 bool fQuit = false; 111 while (fQuit == false) 112 { 113 cout << "1.Dog 2.Horse 3.Fish 0.Quit:"; 114 cin >> choice; 115 switch (choice) 116 { 117 case 1: 118 pAnimal = new Dog(5,Brown); 119 break; 120 case 2: 121 pAnimal = new Horse(4, Black); 122 break; 123 case 3: 124 pAnimal = new Fish(5); 125 default: 126 fQuit = true; 127 break; 128 } 129 if (fQuit == false) 130 { 131 pAnimal->Speak(); 132 pAnimal->Eat(); 133 pAnimal->Reproduce(); 134 pAnimal->Move(); 135 pAnimal->Sleep(); 136 delete pAnimal; 137 cout << endl; 138 } 139 } 140 //getchar(); 141 return 0; 142 }
欢迎讨论,相互学习。
cdtxw@foxmail.com