R6-1 【2020CPP26】交通工具类的实现

定义描述交通工具的抽象类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<iostream>

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(i==1||i==2){

        if(i==1){

            v=&p;

            v->setSpeed(s);

            cout<<"飞机的旅程时间:"<<v->travelTime(d)<<endl;

        }

        else{

            v=&t;

            v->setSpeed(s);

            cout<<"火车的旅程时间:"<<v->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(float a=1000.0){

        speed=a;

    }

    float travelTime(float a){

        return a/speed;

    }

    void setSpeed(float a){

        speed=a;

    }

    float getSpeed(){

        return speed;

    }

};

class Train:public Vehicle{

    private:

    float speed;

    public:Train(float a=100.0){

        speed=a;}

    float travelTime(float a){

        return a/speed;}

    void setSpeed(float a){

        speed=a;}

    float getSpeed(){return speed;}

};

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值