虛基類練習:動物

Problem D: 虛基類練習:動物

[Submit][Status][Web Board]

Description

長期的物種進化使兩棲動物既能活躍在陸地上,又能遊動於水中。利用虛基類建立一個類的多重繼承,包括動物(animal,屬性有體長,體重和性別),陸生動物(ter_animal,屬性增加了奔跑速度),水生動物(aqu_animal,屬性增加了游泳速度)和兩棲動物(amp_animal)。其中兩棲動物保留了陸生動物和水生動物的屬性。

Input

兩棲動物的體長,體重,性別,游泳速度,奔跑速度(running_speed)

Output

初始化的兩棲動物的體長,體重,性別,游泳速度,奔跑速度(running_speed)和輸入的兩棲動物的體長,體重,性別,游泳速度,奔跑速度(running_speed)

Sample Input

5222f102122

Sample Output

hight:50
weight:20
sex:m
swimming_speed:100
running_speed:120

hight:52
weight:22
sex:f
swimming_speed:102
running_speed:122

HINT




#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  //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){}
     
     
};
 
 
////////////////////////////////////////////////
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),ter_animal( h, w, s,r_p),aqu_animal( h, w, s,s_p){}

    void input()
    {
        cin>>hight>>weight>>sex>>swimming_speed>>running_speed;
         
    }
    void 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;
 
}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章