Description
长期的物种进化使两栖动物既能活跃在陆地上,又能游动于水中。利用虚基类建立一个类的多重继承,包括动物(animal,属性有体长,体重和性别),陆生动物(ter_animal,属性增加了奔跑速度),水生动物(aqu_animal,属性增加了游泳速度)和两栖动物(amp_animal)。其中两栖动物保留了陆生动物和水生动物的属性。
Input
两栖动物的体长,体重,性别,游泳速度,奔跑速度(running_speed)
Output
初始化的两栖动物的体长,体重,性别,游泳速度,奔跑速度(running_speed)和输入的两栖动物的体长,体重,性别,游泳速度,奔跑速度(running_speed)
Sample Input
52
22
f
102
122
Sample Output
hight:50
weight:20
sex:m
swimming_speed:100
running_speed:120hight:52
weight:22
sex:f
swimming_speed:102
running_speed:122
HINT
前置代码及类型定义已给定如下,提交时不需要包含,会自动添加到程序前部
/*C++代码*/
#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){}
};
主函数已给定如下,提交时不需要包含,会自动添加到程序尾部
/*C++代码*/
int main()
{
amp_animal a1(50,20,‘m‘,100,120);
amp_animal a2;
a2.input();
a1.show();
cout<<endl;
a2.show();
return 0;
}
1 #include <iostream>
2
3 using namespace std;
4
5 class animal
6
7 {
8
9 protected:
10
11 int hight;
12
13 int weight;
14
15 char sex;
16
17 public:
18
19 animal(){}
20
21 animal(int h,int w,char s):
22
23 hight(h),weight(w),sex(s){}
24
25 };
26
27 class aqu_animal:virtual public animal
28
29 {
30
31 protected:
32
33 int swimming_speed;
34
35 public:
36
37 aqu_animal(){}
38
39 aqu_animal(int h,int w,char s,int s_p):
40
41 animal(h,w,s),swimming_speed(s_p){}
42
43 };class ter_animal:virtual public animal
44 {
45 protected:
46 int running_speed;
47 public:
48 ter_animal(){}
49 ter_animal(int h,int w,char s,int r_p):
50 animal(h,w,s),running_speed(r_p){}
51 };
52 class amp_animal:public aqu_animal,public ter_animal
53 {
54 public:
55 amp_animal(){}
56 amp_animal(int h,int w,char s,int s_p,int r_p):
57 animal(h,w,s),aqu_animal(h,w,s,s_p),ter_animal(h,w,s,r_p){}
58 void input()
59 {cin>>hight>>weight>>sex>>swimming_speed>>running_speed;}
60 void show()
61 {cout<<"hight:"<<hight<<endl;
62 cout<<"weight:"<<weight<<endl;
63 cout<<"sex:"<<sex<<endl;
64 cout<<"swimming_speed:"<<swimming_speed<<endl;
65 cout<<"running_speed:"<<running_speed<<endl;}
66 };
67 int main()
68
69 {
70
71 amp_animal a1(50,20,‘m‘,100,120);
72
73 amp_animal a2;
74
75 a2.input();
76
77 a1.show();
78
79 cout<<endl;
80
81 a2.show();
82
83 return 0;
84
85 }