描述
長期的物種進化使兩棲動物既能活躍在陸地上,又能遊動於水中。利用虛基類建立一個類的多重繼承,包括動物(animal,屬性有體長,體重和性別),陸生動物(ter_animal,屬性增加了奔跑速度),水生動物(aqu_animal,屬性增加了游泳速度)和兩棲動物(amp_animal)。其中兩棲動物保留了陸生動物和水生動物的屬性。
要求: animal只有帶參的構造函數。其他函數根據需要自行設置
輸入
兩棲動物的體長,體重,性別,游泳速度,奔跑速度(running_speed)
輸出
初始化的兩棲動物的體長,體重,性別,游泳速度,奔跑速度(running_speed)和輸入的兩棲動物的體長,體重,性別,游泳速度,奔跑速度(running_speed)
樣例輸入
52 22 f 102 122
樣例輸出
height:52 weight:22 sex:f swimming_speed:102 running_speed:122
//#pragma GCC optimize(2)
#include <iostream>
using namespace std;
class animal
{
protected:
int high;
int wigh;
char sex;
public:
animal(int a,int b,char c):high(a),wigh(b),sex(c){}
};
class ter:virtual public animal
{
protected:
int speed;
public:
ter(int a=0,int b=0,char c='\0',int d=0):animal(a,b,c),speed(d){}
};
class aqu:virtual public animal
{
protected:
int speed;
public:
aqu(int a,int b,char c,int d):animal(a,b,c),speed(d){}
};
class amp:public aqu,public ter
{
public:
//虛基類中只定義一個帶形參的構造函數,則間接或直接的派生類的構造函數都要對虛基類初始化
amp(int a=0,int b=0,char c=0,int x=0,int y=0):animal(a,b,c),aqu(a,b,c,x)//,ter(a,b,c,y)
{
}
friend ostream &operator<<(ostream &out,amp &obj)
{
out<<"height:"<<obj.high<<endl;
out<<"weight:"<<obj.wigh<<endl;
out<<"sex:"<<obj.sex<<endl;
out<<"swimming_speed:"<<obj.aqu::speed<<endl;
out<<"running_speed:"<<obj.ter::speed<<endl;
}
friend istream &operator>>(istream &in,amp &obj)
{
in>>obj.high>>obj.wigh>>obj.sex>>obj.aqu::speed>>obj.ter::speed;
}
};
int main()
{
amp a;
cin>>a;
cout<<a;
return 0;
}