1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104
| #include<iostream> using namespace std; class Animal { private: int itsage; public: Animal(int); virtual ~Animal(){cout<<"析构动物"<<endl;} virtual int GetAge(){return itsage;} virtual void Sleep()=0; virtual void Propagate()=0; virtual void Move()=0; virtual void Body()=0; virtual void Eat()=0; virtual void Show()=0; }; Animal::Animal(int age):itsage(age) { cout<<"创建动物.."<<endl; } class Mammalia:public Animal { public: Mammalia(int age):Animal(age){cout<<"创建哺乳类.."<<endl;} virtual ~Mammalia(){cout<<"析构哺乳类.."<<endl;} virtual void Propagate(){cout<<"哺乳类是胎生动物,通过胚胎来繁殖后代。"<<endl;}
}; class Bird:public Animal { public: Bird(int age):Animal(age){cout<<"创建鸟类.."<<endl;} virtual ~Bird(){cout<<"析构鸟类.."<<endl;} virtual void Sleep(){cout<<"鸟类喜欢站着睡觉。"<<endl;} virtual void Eat(){cout<<"极个别鸟类喜欢吃肉,其他都吃素。"<<endl;} virtual void Propagate(){cout<<"鸟类是卵生动物,通过排卵来繁殖后代。"<<endl;} virtual void Move(){cout<<"鸟类可以飞。"<<endl;} virtual void Body(){cout<<"鸟类体表被覆羽毛,前肢变为翼。"<<endl;} virtual void Show(){cout<<"鸟类的一般寿命为:"<<GetAge()<<endl;} }; class Human:public Mammalia { public: Human(int age):Mammalia(age){cout<<"创建人类.."<<endl;} virtual ~Human(){cout<<"析构人类.."<<endl;} virtual void Sleep(){cout<<"人类要在床上睡觉。"<<endl;} virtual void Eat(){cout<<"大多数人不吃生食。"<<endl;} virtual void Propagate(){cout<<"人类通过胚胎繁殖后代。"<<endl;} virtual void Move(){cout<<"人类靠两条腿走路。"<<endl;} virtual void Body(){cout<<"人类体表无毛。"<<endl;} virtual void Show(){cout<<"人类的一般寿命为:"<<GetAge()<<endl;} }; class Pig:public Mammalia { public: Pig(int age):Mammalia(age){cout<<"创建猪类.."<<endl;} virtual ~Pig(){cout<<"析构猪类.."<<endl;} virtual void Sleep(){cout<<"猪喜欢在烂泥里睡觉。"<<endl;} virtual void Eat(){cout<<"猪是杂食类动物。"<<endl;} virtual void Propagate(){cout<<"猪也通过胚胎繁殖后代。"<<endl;} virtual void Move(){cout<<"猪靠四条腿走路。"<<endl;} virtual void Body(){cout<<"猪体表有毛。"<<endl;} virtual void Show(){cout<<"猪因为要被人宰了吃,所以一般寿命为:"<<GetAge()<<endl;} }; int main() {
Animal *p=0; int choice; bool quit=false; while(choice<4) { cout<<"(1)猪类(2)人类(3)鸟类(0)退出"; cin>>choice; switch(choice) { case 0:quit=true; break; case 1:p=new Pig(1); break; case 2:p=new Human(80); break; case 3:p=new Bird(50); break; default:cout<<"请输入0到3之间的数字"; break;
} if(quit) { break; } p->Show(); p->Eat(); p->Propagate(); p->Move(); p->Sleep(); p->Body(); cout<<endl; delete p; } return 0; }
|