虚基类——动物

问题及代码:

/*。
*Copyright(c)2014,烟台大学计算机学院
*All right reserved,
*文件名:test.cpp
*作者:liu_feng_zi_
*完成日期:2015年6月25日
*版本号:v1.0
*问题描述:虚基类————动物。长期的物种进化使两栖动物既能活跃在陆地上,又能游动于水中。
利用虚基类建立一个类的多重继承,包括动物(animal,属性有体长,体重和性别),
陆生动物(ter_animal,属性增加了奔跑速度),水生动物(aqu_animal,属性增加了游泳速度)和两栖动物(amp_animal)。
其中两栖动物保留了陆生动物和水生动物的属性。
*输入描述:
*程序输出:
*/
#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) {}
};
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 ter_animal,public aqu_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>>height>>weight>>sex>>swimming_speed>>running_speed;
    }
    void show()
    {
        cout<<"height:"<<height<<endl;
        cout<<"weight:"<<weight<<endl;
        cout<<"sex:"<<sex<<endl;
        cout<<"swimming_speed:"<<swimming_speed<<endl;
        cout<<"running_speed:"<<running_speed<<endl;
    }
};
/*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
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值