阅读以下说明和C++代码,填充代码中的空缺,将解答填入答题纸的对应栏内。 [说

admin2022-08-02  30

问题 阅读以下说明和C++代码,填充代码中的空缺,将解答填入答题纸的对应栏内。[说明]某应急交通控制系统(TraficControlSystem)在红灯时控制各类车辆(Vehicle)的通行,其类图如下图所示,在紧急状态下应急车辆红灯时也可通行,其余车辆按正常规则通行。下面的C++代码实现以上设计,请完善其中的空缺。[C++代码]#include<typeinfo>#include<iostream>using namespace std;class Vehicle {/*抽象基类,车辆*/public:virtual void run()=0;};class Emergency {  /*抽象基类,可在红灯时通行的接口,函数均为纯虚函数*/public:______=0;    //isEmergent()函数接口______=0;    //runRedLight()函数接口};clasS Car:public Vehicle{public:~Car(){}void run(){/*代码略*/  ));class Truck:public Vehicle{public:~Truck(){}void run(){  /*代码略*/  });class PoliceCar:______ {private:bool isEmergency;public:PoliceCar():Car(),Emergency() { this->isEmergency=false;}PoliceCar(bool b):Car(),Emergency()  {this->isEmergency=b;}~P0liceCar(){    }bool isEmergent(){    return ______}void runRedLight()  {    /*代码略*/    });/*类Ambulance、FireEngine实现代码略*/class TraficControlsystem {  /*交通控制类*/private:Vehicle*v[24];    int numVehicles;/*在构造函数中设置初始值为0*/public:void control(){  //控制在紧急情况下应急车辆红灯通行,其他情况按常规通行for(int i=0;i<numVehicles; i++){Emergency *ev=dynamic_cast<Emergency*>(v);if(ev !=0)  ______->runRedLight();else ______->run();}}void add(Vehicle*vehicle) { v[numVehicles++] =vehicle; }/*添加车辆*/void shutDown() {for(int i=0;  i<numVehicles; i++) { delete v;}  }};int main(){TraficControlSystem*tcs=new  TraficControlSystem;tcs->add(new Car());  tcs->add(new PoliceCar());tcs->add(new Ambulance()); tcs->add(new Ambulance(true));tcs->add(new FireEngine(true));tcs->add(new FireEngine());tcs->add(new Truck());tcs->contr01();tcs->ShutDown();delete tcs;}

选项

答案

解析 virtual bool isEmergent()
virtual void nmRedLight()
public Car, public Emergency
this->isEmergency
ev
v
转载请注明原文地址:https://tihaiku.com/congyezige/2425932.html

最新回复(0)