/*长期的物种进化使自然界出现了生活在陆地上的陆生动物和生活在水中的水生动物。
根据已有主函数编写动物类,陆生动物类和水生动物类。
Input
动物的体长,体重,性别;
水生动物的体长,体重,性别,游泳速度;
陆生动物的体长,体重,性别,奔跑速度;
Output
动物的体长,体重,性别;
水生动物的体长,体重,性别,游泳速度;
陆生动物的体长,体重,性别,奔跑速度;
*/
#include <iostream>
using namespace std;
class animal
{
protected:
int height;
int weight;
char sex;
public:
animal() {}
animal(int h,int w,char s):
height(h),weight(w),sex(s) {}
virtual void display()
{
cout<<"height:"<<height<<endl;
cout<<"weight:"<<weight<<endl;
cout<<"sex:"<<sex<<endl;
}
};
class aqu_animal:virtual public animal
{
protected:
int swimming_speed;
public:
aqu_animal() {}
aqu_animal(int h,int w,char s,int s_p):
animal(h,w,s),swimming_speed(s_p) {}
void display()
{
cout<<"height:"<<height<<endl;
cout<<"weight:"<<weight<<endl;
cout<<"sex:"<<sex<<endl;
cout<<"swimming_speed:"<<swimming_speed<<endl;
}
};
class ter_animal:virtual public animal //lu生动物
{
protected:
int running_speed; //速度
public:
ter_animal() {}
ter_animal(int h,int w,char s,int r_p):animal(h,w,s),running_speed(r_p) {}
void display()
{
cout<<"height:"<<height<<endl;
cout<<"weight:"<<weight<<endl;
cout<<"sex:"<<sex<<endl;
cout<<"running_speed:"<<running_speed<<endl;
}
};
int main()
{
int a,b,s,r;
char c;
animal *p;
cin>>a>>b>>c;
animal pa(a,b,c);
p=&pa;
p->display();
cin>>a>>b>>c>>s;
aqu_animal pb(a,b,c,s);
p=&pb;
p->display();
cin>>a>>b>>c>>r;
ter_animal pc(a,b,c,r);
p=&pc;
p->display();
return 0;
}
虚基类练习 动物2
最新推荐文章于 2020-10-30 14:21:17 发布