虚函数——c++停车场问题

请根据题目要求完成简单的停车场管理程序。
1.停车场(Park)有N个停车位(Space),每个停车位可以停放不同类型的(Automobile),包括卡车(Truck)、轿车(Car)、公交车(Bus),但同一时刻一个停车位只能停放0或1辆汽车。如果没有空余停车位,显示提示信息,但不会为车辆安排停车位。
2.程序模拟车辆停车的情况:新来车辆时如果有空位,按顺序为该车分配停车位;车辆开走时,应交纳停车费。
3.停车场可以显示当前停放的车辆的车牌号码,以及当前的全部停车费收入(income)。
4.定义汽车基类Automobile,包括车牌号码(字符串)成员数据。
5.定义派生类Truck、Car、Bus。这些车辆除了拥有车牌号码之外,还各自拥有不同的属性。Truck还包括载重量属性(浮点数,单位吨);Car还拥有品牌属性(字符串),Bus还包括核定载员数量(整型)。
此外,每个派生类中要实现pay()函数,用于显示车辆信息并交纳停车费。其中,Truck收费3元/次,Car收费1元/次,Bus收费2元/次。
#include

using namespace std;
class Automobile;
class Park { // 停车场类
public:
Park(int n);
void assignSpace(Automobile x);
void reclaimSpace(Automobile x);
void getPaid(double x){income+=x;}
void showInfo();
private:
double income;
int sum;
Automobile
spaces;
};
class Automobile { // 汽车类
public:
Automobile(string _id):id(_id){}
void enter(Park *park);
void leave(Park *park);
string getid(){return id;}
protected:
virtual void pay(Park *park) = 0; // 向停车场支付停车费,由派生类实现
private:
string id;
};
void Automobile::enter(Park *park)
{
park->assignSpace(this);
}

void Automobile::leave(Park *park) {
park->reclaimSpace(this); // 让停车场收回停车位
pay(park); // 向支付支付停车费,由派生类实现本方法
}
Park::Park(int n){
sum = n;
spaces = new Automobile * [n];
income=0;
for (int i = 0; i < n; i++)
spaces[i]=NULL;
}
void Park::assignSpace(Automobile *x)
{ int flag = 0;
for (int i = 0; i < sum && flag == 0; i++)
{
if (spaces[i] == NULL)
{
spaces[i] = x;
flag = 1;
}
}
if (flag == 1)
cout << x->getid() << “进入停车场,分配停车位” << endl;
else
cout << “无法为” << x->getid() << “分配停车位” << endl;
}
void Park::reclaimSpace(Automobile *x)
{
int flag = 0;
for (int i = 0; i < sum && flag == 0; i++)
{
if (spaces[i]->getid() == x->getid())
{
spaces[i] = NULL;
flag = 1;
}
}
}
void Park::showInfo()
{
int num1 = 0;
int num2[2];
for (int i = 0; i < sum; i++)
{
if (spaces[i] != NULL)
{
num2[num1] = i;
num1++;
}
}
cout << “停车场目前停放了” << num1 << “辆汽车:”;
for (int j = 0,k=0, i = num2[j]; i < num1; k++)
{
cout << spaces[i]->getid() << “,”;
j++;
i=num2[j];
}
cout << “共收入了” << income << “元停车费”<<endl;
}
class Car: public Automobile {
public:
Car(string _id, string _brand):Automobile(_id)
{
brand = _brand;
}
protected:
void pay(Park *park);
private:
string brand;
};
void Car::pay(Park *park) {
cout <<getid()<< “离开停车场,缴纳停车费1元” << endl;
park->getPaid(1);// getPaid函数使park里面的income增加1
}
class Truck :public Automobile
{
public:
Truck(string _id, float _tons):Automobile(_id)
{
tons = _tons;
}
void pay(Park park);
private:
float tons;
};
void Truck::pay(Park park) {
cout <<getid()<< “离开停车场,缴纳停车费3元” << endl;
park->getPaid(3);// getPaid函数使park里面的income增加3
}
class Bus :public Automobile
{
public:
Bus(string _id, int _people):Automobile(_id)
{
people = _people;
}
void pay(Park
park);
private:
int people;
};
void Bus::pay(Park
park){
cout <<getid()<< “离开停车场,缴纳停车费2元” << endl;
park->getPaid(2);// getPaid函数使park里面的income增加2
}
int main() {
int N;
cout << “请输入停车位数量:”;
cin >> N;// 输入停车位数量,此处输入2

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);   // car进入停车场,分配停车位
auto2->enter(&park);   // truck进入停车场,分配车位
auto1->leave(&park);   // car离开停车场,缴纳停车费
auto3->enter(&park);   // bus进入停车场,分配车位

/* 显示当前停放的车辆的车牌号码,以及当前的全部停车费收入*/
park.showInfo();

auto4->enter(&park);      // car进入停车场,分配停车位

// car进入停车场,分配停车位。因为没有空余停车位,所以无法分配

//这个顺序会使程序崩掉,交换顺序以后就可以正常运行
auto3->leave(&park); // bus离开停车场,缴纳停车费
auto2->leave(&park); // truck离开停车场,缴纳停车费

**auto2->leave(&park);  // truck离开停车场,缴纳停车费
auto3->leave(&park);  // bus离开停车场,缴纳停车费**

/* 显示当前停放的车辆的车牌号码,以及当前的全部停车费收入*/
park.showInfo();

return 0;
}

  • 1
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
简单的车位管理程序 随着家庭购买汽车的增加,停车场车位紧张的问题越来越突出。请根据题目要求完成简单的车位管理程序。 1.停车场有若干停车位(为说明问题,假定为3个),每个位置可以存放不同种类的的汽车,包括卡车Truck,客车Carriage和小轿车Car,但同一时刻一个位置只能存放0或1辆汽车。 2.管理系统模拟实际车辆停车的情况:新来车辆时如果有空位,按顺序为该车分配停车位; 车辆开走时,交纳相应停车费;统计各类车辆的数量。 3.定义描述停车场的类Park,其中有3个位置用于存放各类车辆。 4.定义基类Automobile,至少包括纯虚函数Pay用于显示车辆信息并交纳相应停车费。 5.定义派生类Truck,Carriage和Car,这些车辆除了拥有车牌号(字符串)、车辆已使用年数(整数)之外。 Truck还拥有载重量(浮点数,单位吨)属性,Carriage还拥有乘坐人数(整数,单位人)属性,Car还拥有排气量(浮点数,单位L)属性。具体实现上述纯虚函数Pay,显示每类车辆的相应信息,并给出计价提示,其中Truck收费2元/小时,Carriage收费1.5元/小时,Car收费1元/小时。 6.重载输入“>>”操作符,使得可以通过cin直接读入每类车辆的相应信息。 7.编写main函数,测试上述所要求的各种功能,即根据菜单命令为新来车辆分配停车位、开走车辆(输入车位编号)时付费、显示停车场中各类车辆的数量。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值