Problem B: 驾驶员与汽车

网友投稿 589 2022-11-29

Problem B: 驾驶员与汽车

Problem B: 驾驶员与汽车

Problem B: 驾驶员与汽车

Description

我们知道,目前我国的驾照大致可分为A、B、C三种,其中C证只能开小型客车(货车),B证可开中、小型客车(货车),A证没有限制。现在请定义如下几个类:

1. Automobile:抽象类,具有数据成员double speed,纯虚函数virtual void run() const = 0。

2. 六种车型,即小型车Benz、Buick;中型车Zhongba、Beiqi;以及大型车Dayu、Jianghuai。它们都是Automobile的子类。

3. Driver类,具有string name和char type两个数据成员,前者是司机的名字,后者是司机持有的驾照类型(A、B或C)。提供Drive(Automobile *)方法,根据驾照类型判定该司机是否可以驾驶指定的汽车。

Input

输入分多行。第一行是一个整数M>0,表示之后有M个测试用例。

每个测试用例包括四部分:司机姓名(不含空白符)、驾照类型(A、B或C)、车型(分别用字母a~f表示六种车型,对应的车型可以从main()中看出)以及该车的行驶速度(double类型范围内的正数)。

Output

输出共M行,每个测试用例对应一行输入,具体格式参见样例。

HINT

1.使用typeid来判定一个基类指针实际指向的对象的类型。

2.注意:append.cc中已经给出了Driver类的一个方法,不要在Driver类重复定义了。

#include #include #include #include using namespace std;class Automobile {protected: double speed;public: Automobile(double s) : speed(s) {} virtual void run() const = 0; virtual ~Automobile() { cout << "An automobile is erased!" << endl; }};class Benz : public Automobile {public: Benz(double speed) : Automobile(speed) {} ~Benz() { cout << "A Benz is erased!" << endl; } void run() const { cout << setprecision(2) << setiosflags(ios::fixed); cout << "Benz at speed of "<run(); break; case 'B': if (typeid(*automobile) == typeid(Dayu) || typeid(*automobile) == typeid(Jianghuai)) cout<<"Driver "<run(); } break; case 'C': if (typeid(*automobile) != typeid(Benz) && typeid(*automobile) != typeid(Buick)) cout<<"Driver "<run(); } break; }}int main(){ string name; char type; double speed; char automobileType; int cases; Automobile *automobile; cin>>cases; for (int i = 0; i < cases; i++) { cin>>name>>type>>automobileType>>speed; Driver driver(name, type); switch (automobileType) { case 'a': automobile = new Benz(speed); break; case 'b': automobile = new Buick(speed); break; case 'c': automobile = new Zhongba(speed); break; case 'd': automobile = new Beiqi(speed); break; case 'e': automobile = new Dayu(speed); break; case 'f': automobile = new Jianghuai(speed); break; } driver.Drive(automobile); delete automobile; } return 0;}

版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:Problem B: Person类与Student类的关系
下一篇:Problem A: 字符串类(I)
相关文章

 发表评论

暂时没有评论,来抢沙发吧~