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所以还是写了吧
}