C++程序设计(三)——虚基类的多重继承

C++程序设计

今天不想多说,直接上题目和代码。

长期的物种进化使两栖动物能活跃在陆地上,又能游动于水中。利用虚基类建立一个类的多重继承,包括动物(animal,属性有体长、体重和性别),陆生动物(ter_animal,属性增加了奔跑速度),水生动物(aqu_animal,属性增加了游泳速度)。
INPUT:两栖的体长,体重,性别,游泳速度,奔跑速度
OUTPUT:两栖动物的体长,体重,性别,游泳速度,奔跑速度

题很简单,不想多做解释。下面展示代码:

#include <iostream>
using namespace std;
class animal 
{
public :
	double length;
	double weight;
	string sex;
	animal();
	animal(double, double, string);
};
animal::animal()
{
	cout << "default created." << endl;
}
animal::animal(double an_length, double an_weight, string an_sex)
{
	length = an_length;
	weight = an_weight;
	sex = an_sex;
	cout << "alright created." << endl;
}
class ter_animal :virtual public animal
{
public:
	double run_speed;
	ter_animal();
	ter_animal(double, double, string, double);
};
ter_animal::ter_animal()
{
	cout << "default ter_animal created." << endl;
}
ter_animal::ter_animal(double ter_length,double ter_weight,string ter_sex, double ter_run_speed):animal(ter_length,ter_weight,ter_sex)
{
	run_speed = ter_run_speed;
}
class aqu_animal :virtual public animal
{
public:
	double swim_speed;
	aqu_animal();
	aqu_animal(double, double, string, double);
};
aqu_animal::aqu_animal()
{
	cout << "default aqu_animal created." << endl;
}
aqu_animal::aqu_animal(double aqu_length, double aqu_weight, string aqu_sex, double aqu_swim_speed) :animal(aqu_length, aqu_weight, aqu_sex)
{
	swim_speed = aqu_swim_speed;
}
class amp_animal :virtual public ter_animal, virtual public aqu_animal
{
public :
	amp_animal();
	amp_animal(double, double, string, double, double);
};
amp_animal::amp_animal()
{
	cout << "default amp_animal created." << endl;
}
amp_animal::amp_animal(double amp_length, double amp_weight, string amp_string, double amp_run_speed, double amp_swim_speed) :ter_animal(amp_length, amp_weight, amp_string, amp_run_speed), aqu_animal(amp_length, amp_weight, amp_string, amp_swim_speed), animal(amp_length, amp_weight, amp_string)
{
	cout << "输入的两栖动物属性值为:" << endl;
	cout << "体长: " << amp_length << endl;
	cout << "体重: " << amp_weight << endl;
	cout << "性别: " << amp_string << endl;
	cout << "跑步速度: " << amp_run_speed << endl;
	cout << "游泳速度: " << amp_swim_speed << endl;
}
int main()
{
	cout << "输入两栖动物的属性" << endl;
	double length;
	double weight;
	string sex;
	double run_speed;
	double swim_speed;
	cin >> length >> weight >> sex >> run_speed >> swim_speed;
	amp_animal Amp(length,weight,sex,run_speed,swim_speed);
	cout << "**********************************" << endl;
	cout << "输出的两栖动物属性值为:" << endl;
	cout << "体长: " << Amp.length << endl;
	cout << "体重: " << Amp.weight << endl;
	cout << "性别: " << Amp.sex << endl;
	cout << "跑步速度: " << Amp.run_speed << endl;
	cout << "游泳速度: " << Amp.swim_speed << endl;
	return 0;
}

如果有的朋友觉得我的代码有哪里还可以改进,欢迎交流分享你的想法。让我们共同进步。以上,今天的编程课又编了个寂寞。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值