好心人指导一下,新手上路
#include<iostream>using namespace std;
class animal
{
public:
animal(int);
virtual ~animal(){cout<<"析构动物。。\n";}
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<<"创建动物\n";
}
class mammalia:public animal
{
public:
mammalia(int age):animal(age)
{
cout<<"创建哺乳类。。\n";
}
virtual ~mammalia()
{
cout<<"析构哺乳类。。\n";
}
virtual void propagate()
{
cout<<"哺乳类是胎生动物,通过拍泰来繁殖后代。\n";
}
};
class pig:public mammalia
{
public:
pig(int age):mammalia(age)
{
cout<<"创建猪类。。\n";
}
virtual ~pig()
{
cout<<"析构猪类,,\n";
}
virtual void sleep()
{
cout<<"猪喜欢在烂泥里睡觉。。\n";
}
virtual void eat()
{
cout<<"猪是杂食类动物。。\n";
}
virtual int show()
{
cout<<"因为猪长大要被人杀了吃,所以一般寿命是:"<<getage()<<endl;
}
virtual void propagate()
{
cout<<"猪也是通过胚胎繁殖后代,,\n";
}
virtual void move()
{
cout<<"猪朱是靠四条腿走路。。\n";
}
virtual void body()
{
cout<<"猪体表有毛。。\n";
}
};
class human:public mammalia
{
public:
human(int age):mammalia(age)
{
cout<<"创建人类。。\n";
}
virtual ~human()
{
cout<<"析构人类,,\n";
}
virtual void sleep()
{
cout<<"人喜欢在床上睡觉。。\n";
}
virtual void eat()
{
cout<<"大多数人不吃生食。。\n";
}
virtual int show()
{
cout<<"人类一般的寿命是:"<<getage()<<endl;
}
virtual void propagate()
{
cout<<"人也是通过胚胎繁殖后代,,\n";
}
virtual void move()
{
cout<<"人类是靠两条腿走路。。\n";
}
virtual void body()
{
cout<<"猪体表无毛。。\n";
}
};
class bird:public animal
{
public:
bird(int age):animal(age)
{
cout<<"创建鸟类。。\n";
}
virtual ~bird()
{
cout<<"析构鸟类。。\n";
}
virtual void propagate()
{
cout<<"鸟类是卵生动物,通过排卵来繁殖后代。\n";
}
virtual void sleep()
{
cout<<"鸟喜欢站着睡觉。。\n";
}
virtual void eat()
{
cout<<"大多数鸟类吃素。。\n";
}
virtual int show()
{
cout<<"鸟类一般的寿命是:"<<getage()<<endl;
}
virtual void move()
{
cout<<"鸟类可以飞。。\n";
}
virtual void body()
{
cout<<"鸟类体表被羽毛覆盖。。\n";
}
};
int main()
{
animal *p;
int choice=0;
while(1)
{
bool quit=false;
cout<<"(1)猪类;(2)人类;(3)鸟类;(4)退出;\n 请选择类种:";
cin>>choice;
switch(choice)
{
case 1:p=new pig(1);
break;
case 2:p=new human(80);
break;
case 3:p=new bird(30);
break;
case 4:quit=true;
cout<<"程序结束\n";
break;
default :quit=true;
break;
}
if(quit)
{
break;
}
p->show();
p->eat();
p->propagate();
p->move();
p->sleep();
p->body();
cout<<"\n";
delete p;
}
return 0;
}