#include<iostream> using namespace std; class Animal { 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; private: int itsage; }; 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 Propagete() { cout<<"哺乳类是胎生动物,通过胚胎来繁殖后代"<<endl; } }; class Brid:public Animal { public: Brid(int age):Animal(age) { cout<<"创建鸟类...."<<endl; } virtual ~Brid() { 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=0; bool quit=false; while(choice<4) { cout<<"1-猪类2-人类3-鸟类0-退出"<<endl; cin>>choice; switch(choice) { case 1:p=new Pig(1); break; case 2:p=new Human(80); break; case 3:p=new Brid(50); break; default:quit=true; break; } if(quit) { break; } p->Show(); p->Eat(); p->Propagate(); p->Move(); p->Sleep(); p->Body(); cout<<endl; delete p; } }
时间: 2024-10-19 07:01:27