定义描述交通工具的抽象类Vehicle,并由Vehicle类派生出两种交通工具——飞机类Plane和火车类Train,主函数完成相关的测试。

定义描述交通工具的抽象类Vehicle,并由Vehicle类派生出两种交通工具——飞机类Plane和火车类Train,主函数完成相关的测试。

Vehicle类结构说明:
Vehicle类的成员函数包括:
①公有函数成员float travelTime(float)是纯虚函数,它的功能是根据旅程距离计算旅程时间。
②公有函数成员void setSpeed(float)是纯虚函数,它的功能是用于设置交通工具速度。
Plane类结构说明:
Plane类公有继承自Vehicle类
Plane类新增的数据成员包括:
①私有数据成员:飞行速度speed(float)。
Plane类的函数成员包括:
①有参构造函数Plane(float),参数用于初始化飞行速度speed,参数默认值为1000。
②重载实现float travelTime(float)。
③重载实现void setSpeed(float)。
④公有成员函数float getSpeed()的功能是返回飞行速度。
Train类结构说明:
Train类公有继承自Vehicle类
Train类新增的数据成员包括:
①私有数据成员:行驶速度speed(float)。
Train类的成员函数包括:
①有参构造函数Train(float),参数用于初始化行驶速度speed,参数默认值为100。
②重载实现float travelTime(float)。
③重载实现void setSpeed(float)。
④公有成员函数float getSpeed()的功能是返回行驶速度。
裁判测试程序:
#include
using namespace std;

/请在这里填写答案/

//主函数
int main(){
Vehicle *v;
Plane p;
Train t;
int i;
float s, d;
cout<<“飞机的速度:”<<p.getSpeed()<<endl;
cout<<“火车的速度:”<<t.getSpeed()<<endl;
cout<<“请输入交通工具类型号(1飞机,2火车)、速度以及路程:”<<endl;
cin>>i>>s>>d;
if(i1||i2){
if(i==1){
v=&p;
v->setSpeed(s);
cout<<“飞机的旅程时间:”<travelTime(d)<<endl;
}
else{
v=&t;
v->setSpeed(s);
cout<<“火车的旅程时间:”<travelTime(d)<<endl;
}
}
return 0;
}
输入样例:
1 375 2700
输出样例:
飞机的速度:1000
火车的速度:100
请输入交通工具类型号(1飞机,2火车)、速度以及路程:
飞机的旅程时间:7.2

class Vehicle{
public:
virtual float travelTime(float)=0;
virtual void setSpeed(float)=0;

};


class Plane:public Vehicle
{
private:
	float speed;

public:
	Plane(){speed=1000;}
	float travelTime(float s){return s/speed;}
	void setSpeed(float v){speed=v;}
	float getSpeed(){return speed;}
};



class Train:public Vehicle
{
private:
	float speed;

public:
	Train(){speed=100;}
	float travelTime(float s){return s/speed;};
	void setSpeed(float v){speed=v;}
	float getSpeed(){return speed;}
};
  • 4
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值