校园车辆管理系统:区分职工车辆和外来车辆收费

本代码使用 C++ 实现一个校园车辆管理系统,区分职工车辆和外来车辆,并分别计算收费。系统使用面向对象编程,定义基类 CarAccess,并派生出 StaffCarVisitCar 两个子类。

程序要求:

  1. 创建基类 CarAccess,包含以下数据成员: * carno:车牌号,8 个字符的字符串变量,protected。 * carCount:现存车辆数量,整数类型。 * fee:收费情况,浮点数类型,protected。2. CarAccess 构造函数:carCount++。3. CarAccess 析构函数:carCount--。4. CarAccess 虚函数 Print():输出车牌号和驶入时间。5. CarAccess 虚函数 GetFee():根据驶出时间计算费用。6. 派生两个子类:StaffCar(内部职工车辆)和 VisitCar(临时外来车辆)。7. StaffCarVisitCar 中分别包含 carCount 变量,记录对应类型的车辆数量。8. StaffCarVisitCar 都有 Print() 函数,分别输出车牌号、驶入时间和车辆类型(S 或 V)。9. StaffCarVisitCar 都有 GetFee() 函数,分别计算职工车辆和外来车辆的停车费用。假设职工车辆已缴费,驶出时不收费。

**代码示例:**c++#include using namespace std;#include <string.h>

const int CARNO_DIGIS = 8;const int ACCESS_TIMES = 100;

// 基类 CarAccessclass CarAccess {protected: char carno[CARNO_DIGIS + 1]; float fee;public: static int carCount; CarAccess(const char carno, int time) { strcpy(this->carno, carno); carCount++; } virtual ~CarAccess() { carCount--; } virtual float GetFee(int time) = 0; virtual void Print() = 0; char GetCarno() { return carno; }};int CarAccess::carCount = 0;

// 职工车辆 StaffCarclass StaffCar : public CarAccess {public: static int carCount; StaffCar(const char *carno, int time) : CarAccess(carno, time) { carCount++; } ~StaffCar() { carCount--; } float GetFee(int time) override { return 0.0; // 职工车辆已缴费 } void Print() override { cout << carno << ' ' << time << ' S' << endl; }};int StaffCar::carCount = 0;

// 外来车辆 VisitCarclass VisitCar : public CarAccess {public: static int carCount; VisitCar(const char *carno, int time) : CarAccess(carno, time) { carCount++; } ~VisitCar() { carCount--; } float GetFee(int time) override { return 8.0 * (time - inTime); // 每小时 8 元 } void Print() override { cout << carno << ' ' << time << ' V' << endl; }private: int inTime;};int VisitCar::carCount = 0;

void Depart(CarAccess *p, int time) { if (p != NULL) { cout << p->GetCarno() << ' ' << p->GetFee(time) << endl; delete p; }}

int main() { CarAccess *cars[ACCESS_TIMES] = {NULL}; int n; cin >> n;

for (int i = 0; i < n; i++) {        char carno[CARNO_DIGIS + 1];        cin >> carno;        int InOut; cin >> InOut;        int curtime; cin >> curtime;

    if (InOut == 1) // 车辆驶入        {            char type; cin >> type;            if (type == 'S')                cars[i] = new StaffCar(carno, curtime);            else if (type == 'V')                cars[i] = new VisitCar(carno, curtime);            else                i--;        }        else if (InOut == 2) // 车辆驶出        {            if (i == 0) break;            bool flag = false;            for (int j = i - 1; j >= 0; j--) {                if (cars[j] == NULL) continue;                if (strcmp(cars[j]->GetCarno(), carno) == 0) {                    Depart(cars[j], curtime);                    cars[j] = NULL;                    flag = true;                    break;                }            }            if (!flag)                cout << 'err' << endl;        }        else            break;    }    cout << CarAccess::carCount << ' ' << StaffCar::carCount << ' ' << VisitCar::carCount << endl;

for (int k = 0; k < n; k++)        if (cars[k] != NULL) {            cars[k]->Print();            delete cars[k];        }    return 0
C++实现校园车辆管理系统,区分职工车辆和外来车辆收费

原文地址: https://www.cveoy.top/t/topic/oioN 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录