6-2 车的不同行为

6-2 车的不同行为

分数 10

全屏浏览题目

切换布局

作者 杨军

单位 四川师范大学

定义一个车(vehicle)基类,有虚函数Run、Stop等成员函数,由此派生出自行车(bicycle)类、汽车(motorcar)类,它们都有Run、Stop等成员函数。完成这些类使得主函数可以运行并得到正确的输出结果。

函数接口定义:

完成类代码

裁判测试程序样例:

 

/* 请在这里填写答案 */
int main(int argc, char const *argv[])
{
    Vehicle veh;
    Bicycle bic;
    Motorcar mot;
    run(veh);
    run(bic);
    run(mot);
    return 0;
}

输入样例:

输出样例:

在这里给出相应的输出。例如:

Vehicle run
Bicycle run
Motorcar run

代码长度限制

16 KB

时间限制

400 ms

内存限制

64 MB

这个还好,比较简单,不多说了.....

#include<iostream>
#include <string>
using namespace std;

/* 请在这里填写答案 */
class Vehicle
{
public:
    virtual void Run()
    {
        cout<<"Vehicle run"<<endl;
    }
    virtual void Stop()
    {
        cout<<"Vehicle stop"<<endl;
    }
};
class Bicycle :public Vehicle
{
public:
    virtual void Run()
    {
        cout<<"Bicycle run"<<endl;
    }
    virtual void Stop()             //其实答案没有关于stop的结果,但是我还是写上了
    {
        cout<<"Bycicle stop"<<endl;
    }
};
class Motorcar :public Vehicle
{
public:
    virtual void Run()
    {
        cout<<"Motorcar run"<<endl;
    }
    virtual void Stop()
    {
        cout<<"Motorcar stop"<<endl;
    }  
};
void run(Vehicle &p)
{
    p.Run();
}
void stop(Vehicle &p)
{
    p.Stop();                       //把所有关于stop的内容删掉,本题也能过,但是既然题目要求 
                                    //能stop所以还是写了吧
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值