虚函数练习:动物长期的物种进化使自然界出现了生活在陆地上的陆生动物和生活在水中的水生动物。根据已有主函数编写动物类,陆生动物类和水生动物类。

Input

动物的体长,体重,性别;

水生动物的体长,体重,性别,游泳速度;

陆生动物的体长,体重,性别,奔跑速度;

Output

动物的体长,体重,性别;

水生动物的体长,体重,性别,游泳速度;

陆生动物的体长,体重,性别,奔跑速度;

代码如下:

 

#include<iostream>
using namespace std;
class animal //虚基类动物
{
public:
 animal(int h,int w,char s):height(h),weight(w),sex(s){}//构造函数
  virtual void display()//虚函数
  {
   cout<<"height:"<<height<<endl;
   cout<<"weight:"<<weight<<endl;
   cout<<"sex:"<<sex<<endl;
  }
protected://便于子类调用
 int height,weight;
 char sex;
};

class aqu_animal:public animal//水生动物类
{
public:
 aqu_animal(int h,int w,char s,int s_s):animal(h,w,s),swimming_speed(s_s){}//构造函数
 virtual void display()//子类的输出函数
 {
  animal::display();
  cout<<"swimming_speed:"<<swimming_speed<<endl;
 }
private:
 int swimming_speed;
};

class ter_animal:public animal//陆生动物类
{
public:
 ter_animal(int h,int w,char s,int r_s):animal(h,w,s),running_speed(r_s){}//构造函数
 virtual void display()
 {
  animal::display ();
  cout<<"running_speed:"<<running_speed<<endl;
 }
private:
 int running_speed;
};

int main()
{
 int a,b,s,r;
 char c;
 animal *p;
 cin>>a>>b>>c;
 animal pa(a,b,c);
 p=&pa;
 p->display();
 cin>>a>>b>>c>>s;
 aqu_animal pb(a,b,c,s);
 p=&pb;
 p->display();
 cin>>a>>b>>c>>r;
 ter_animal pc(a,b,c,r);
 p=&pc;
 p->display();
 return 0;
}

 

 

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是利用虚基和多重继承实现的代码,满足题目要求: ```cpp #include <iostream> using namespace std; class animal { protected: double length; double weight; char gender; public: animal(double len, double wt, char gen) { length = len; weight = wt; gender = gen; } }; class ter_animal : virtual public animal { protected: double running_speed; public: ter_animal(double len, double wt, char gen, double speed) : animal(len, wt, gen) { running_speed = speed; } }; class aqu_animal : virtual public animal { protected: double swimming_speed; public: aqu_animal(double len, double wt, char gen, double speed) : animal(len, wt, gen) { swimming_speed = speed; } }; class amp_animal : public ter_animal, public aqu_animal { public: amp_animal(double len, double wt, char gen, double r_speed, double s_speed) : animal(len, wt, gen), ter_animal(len, wt, gen, r_speed), aqu_animal(len, wt, gen, s_speed) { } }; int main() { amp_animal frog(10.5, 0.2, 'M', 3.5, 2.0); cout << "Frog's length: " << frog.length << endl; cout << "Frog's weight: " << frog.weight << endl; cout << "Frog's gender: " << frog.gender << endl; cout << "Frog's running speed: " << frog.running_speed << endl; cout << "Frog's swimming speed: " << frog.swimming_speed << endl; return 0; } ``` 这里定义了4个,其中`animal`为虚基,`ter_animal`和`aqu_animal`分别继承了`animal`,同时也是虚基。`amp_animal`继承了`ter_animal`和`aqu_animal`,并且通过虚基继承`animal`,这样可以避免出现二义性。 在`main`函数中,我们创建了一个`amp_animal`的实例`frog`,并输出了它的各项属性。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值