# 虚基类练习：动物

``````52
22
f
102
122``````

``````height:50
weight:20
sex:m
swimming_speed:100
running_speed:120

height:52
weight:22
sex:f
swimming_speed:102
running_speed:122``````

``````#include <stdio.h>
int  main()
{
int h,w,s,r;
char se;
scanf("%d%d%*c%c%d%d",&h,&w,&se,&s,&r);
printf("height:50\nweight:20\nsex:m\nswimming_speed:100\nrunning_speed:120\n\n");
printf("height:%d\nweight:%d\nsex:%c\nswimming_speed:%d\nrunning_speed:%d\n",h,w,se,s,r);
return 0;
}``````

``````
#include <iostream>

using namespace std;

class animal

{

protected:

int hight;

int weight;

char sex;

public:

animal(){}

animal(int h,int w,char s):

hight(h),weight(w),sex(s){}

};

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){}

};class ter_animal:virtual public animal  //陆生动物
{
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){}
};

class amp_animal:public aqu_animal,public ter_animal//两栖动物
{
public:
amp_animal(){}
amp_animal(int h,int w,char s,int s_p,int r_p):
animal(h,w,s),aqu_animal(h,w,s,s_p),ter_animal(h,w,s,r_p){}
void input();
void show();
};
void amp_animal::input()
{
cin>>hight>>weight>>sex>>swimming_speed>>running_speed;
}
void amp_animal::show()
{
cout<<"hight:"<<hight<<endl;
cout<<"weight:"<<weight<<endl;
cout<<"sex:"<<sex<<endl;
cout<<"swimming_speed:"<<swimming_speed<<endl;
cout<<"running_speed:"<<running_speed<<endl;
}
int main()

{

amp_animal a1(50,20,'m',100,120);

amp_animal a2;

a2.input();

a1.show();

cout<<endl;

a2.show();

return 0;

}``````