#include
using namespace std;
//定义抽象类Vehicle
class Vehicle
{
public:
virtual float travelTime(float)=0; //计算旅程时间
virtual void setSpeed(float)=0; //设置交通工具速度
};
//飞机类
class Plane:public Vehicle
{
private:
float speed; //飞行速度
public:
Plane(float s=1000) {speed=s;}
float travelTime(float d) {return d/speed;} //计算旅程时间
void setSpeed(float s) {speed=s;} //设置飞行速度
float getSpeed() {return speed;} //返回飞行速度
};
//火车类
class Train:public Vehicle
{
private:
float speed; //行驶速度
public:
Train(float s=100) {speed=s;}
float travelTime(float d) {return d/speed;} //计算旅程时间
void setSpeed(float s) {speed=s;} //设置行驶速度
float getSpeed() {return speed;} //返回行驶速度
};
//主函数
int main(){
Vehicle *v; //定义Vehicle类型指针
Plane p; //定义Plane类型对象
Train t; //定义Train类型对象
int i; //交通工具类型号
float s, d; //交通工具速度和路程
cout<'飞机的速度:'<<p.getSpeed()<<endl; //输出飞机的速度
cout<'火车的速度:'<<t.getSpeed()<<endl; //输出火车的速度
cout<'请输入交通工具类型号(1飞机,2火车)、速度以及路程:'<<endl;
cin>>i>>s>>d; //输入交通工具类型号、速度和路程
if(i==1||i==2){ //判断交通工具类型号是否合法
if(i==1){ //如果是飞机
v=&p; //用Vehicle类型指针指向Plane类型对象
v->setSpeed(s); //设置速度
cout<'飞机的旅程时间:'<travelTime(d)<<endl; //计算并输出旅程时间
}
else{ //如果是火车
v=&t; //用Vehicle类型指针指向Train类型对象
v->setSpeed(s); //设置速度
cout<'火车的旅程时间:'<travelTime(d)<<endl; //计算并输出旅程时间
}
}
return 0;
}