UML类图:

代码:
| #include <iostream> |
| #include <string> |
| using namespace std; |
| |
| class Park; |
| |
| class Automobile { |
| public: |
| Automobile(string number) :plateNumber(number) {} |
| Automobile() = default; |
| void enter(Park* park); |
| void leave(Park* park); |
| string getNumber() { |
| return this->plateNumber; |
| } |
| ~Automobile() {} |
| protected: |
| virtual void pay(Park* park) = 0; |
| string plateNumber; |
| }; |
| |
| |
| class Park { |
| public: |
| Park(int number) :spaceNum(number), parkNum(0), income(0) { |
| spaces = new Automobile * [number]; |
| for (int i = 0; i < number; i++) { |
| spaces[i] = nullptr; |
| } |
| } |
| bool allocate(Automobile* pa) { |
| bool flag = false; |
| for (int i = 0; i < this->spaceNum; i++) { |
| if (spaces[i] == nullptr) { |
| spaces[i] = pa; |
| parkNum++; |
| cout << pa->getNumber() << "进入停车场,分配停车位" << endl; |
| flag = true; |
| break; |
| } |
| } |
| if (flag==false) { |
| cout << "无法为" << pa->getNumber() << "分配停车位" << endl; |
| } |
| return flag; |
| } |
| bool reclaim(Automobile* pa) { |
| bool flag = false; |
| for (int i = 0; i < this->spaceNum; i++) { |
| if (spaces[i] == pa) { |
| spaces[i] = nullptr; |
| parkNum--; |
| flag = true; |
| break; |
| } |
| } |
| if (flag == false) { |
| cout << "停车场中没有车牌为" << pa->getNumber() << "的汽车" << endl; |
| } |
| return flag; |
| } |
| void charge(int fare) { |
| this->income += fare; |
| } |
| void showInfo() { |
| cout << "停车场目前停放了" << this->parkNum << "辆汽车"; |
| if (this->parkNum != 0) { |
| cout << ":"; |
| for (int i = 0; i < spaceNum; i++) { |
| if (spaces[i] != nullptr) { |
| cout << spaces[i]->getNumber() << ","; |
| } |
| } |
| } else { |
| cout << ","; |
| } |
| cout << "共收入" << this->income << "元停车费" << endl; |
| } |
| ~Park() { |
| delete[] spaces; |
| } |
| private: |
| int spaceNum; |
| int parkNum; |
| int income; |
| Automobile** spaces; |
| }; |
| |
| void Automobile::enter(Park* park) { |
| park->allocate(this); |
| } |
| |
| void Automobile::leave(Park* park) { |
| park->reclaim(this); |
| this->pay(park); |
| } |
| |
| |
| class Truck :public Automobile { |
| public: |
| Truck(string number, float load) :Automobile(number), load(load), fare(3) {} |
| float getLoad() { |
| return this->load; |
| } |
| int getFare() { |
| return this->fare; |
| } |
| void pay(Park* park) { |
| cout << this->getNumber() << "离开停车场,缴纳停车费" << this->fare << "元" << endl; |
| park->charge(this->fare); |
| } |
| ~Truck() {} |
| private: |
| float load; |
| int fare; |
| }; |
| |
| |
| class Car :public Automobile { |
| public: |
| Car(string number, string brand) :Automobile(number), brand(brand), fare(1) {} |
| string getBrand() { |
| return this->brand; |
| } |
| int getFare() { |
| return this->fare; |
| } |
| void pay(Park* park) { |
| cout << this->getNumber() << "离开停车场,缴纳停车费" << this->fare << "元" << endl; |
| park->charge(this->fare); |
| } |
| ~Car() {} |
| private: |
| string brand; |
| int fare; |
| }; |
| |
| |
| class Bus :public Automobile { |
| public: |
| Bus(string number, int carry) :Automobile(number), carry(carry), fare(2) {} |
| int getCarry() { |
| return this->carry; |
| } |
| int getFare() { |
| return this->fare; |
| } |
| void pay(Park* park) { |
| cout << this->getNumber() << "离开停车场,缴纳停车费" << this->fare << "元" << endl; |
| park->charge(this->fare); |
| } |
| ~Bus() {} |
| private: |
| int carry; |
| int fare; |
| }; |
| |
| int main() { |
| int N; |
| cout << "请输入停车位数量:"; |
| cin >> N; |
| Park park(N); |
| |
| Automobile* auto1 = new Car("鲁B-12345", "奥迪A6"); |
| Automobile* auto2 = new Truck("鲁B-23456", 15); |
| Automobile* auto3 = new Bus("鲁B-34567", 50); |
| Automobile* auto4 = new Car("鲁B-45678", "宝马320"); |
| |
| auto1->enter(&park); |
| auto2->enter(&park); |
| auto1->leave(&park); |
| auto3->enter(&park); |
| |
| park.showInfo(); |
| |
| auto4->enter(&park); |
| |
| auto3->leave(&park); |
| auto2->leave(&park); |
| |
| park.showInfo(); |
| |
| delete auto1; |
| delete auto2; |
| delete auto3; |
| delete auto4; |
| return 0; |
| } |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· C#/.NET/.NET Core优秀项目和框架2025年2月简报
· DeepSeek在M芯片Mac上本地化部署
· 葡萄城 AI 搜索升级:DeepSeek 加持,客户体验更智能