实验七 继承与派生

实验七 继承与派生

1 实验目的

学习继承与派生的相关理论,熟悉不同继承方式下对基类成员的访问方式,包括以下几个方面:

(1)学习声明和使用类的继承关系,声明派生类;

(2)熟悉不同继承方式下,对基类成员的访问控制;

2 实验内容

2.1 停车场程序

问题描述

请根据题目要求完成简单的停车场管理程序。

1.停车场(Park)有N个停车位(space),每个停车位可以停放不同类型的汽车(Automobile),包括卡车(Truck)、轿车(Car)、公交车(Bus),但同一时刻一个停车位只能停放0或1辆汽车。如果没有空余停车位,显示提示信息,但不会为车辆安排停车位。

2.程序模拟车辆停车的情况:新来车辆时如果有空位,按顺序为该车分配停车位;车辆开走时,应交纳停车费。

3.停车场可以显示当前停放的车辆的车牌号码,以及当前的全部停车费收入(income)。

4.定义汽车基类Automobile,包括车牌号码(字符串)成员数据。

5.定义派生类Truck、Car、Bus。这些车辆除了拥有车牌号码之外,还各自拥有不同的属性。Truck还包括载重量属性(浮点数,单位吨);Car还拥有品牌属性(字符串),Bus还包括核定载员数量(整型)。

此外,每个派生类中要实现leave()函数,用于显示车辆信息并交纳停车费。其中,Truck收费3元/次,Car收费1元/次,Bus收费2元/次。

问题要求

编写程序,测试上述所要求的各种功能。要求创建新的工程项目ParkManager,添加必要的源文件和头文件,并在程序适当的位置中编写注释。

class Automobile {}; // 汽车类

class Park {}; // 停车场类

int main() {

cout << “请输入停车位数量:”;

cin >> N;// 输入停车位数量,此处输入2

Park *park = new Park(N);// 创建一个停车场对象

Car car1(“鲁B-12345”,“奥迪A6”); // 创建轿车对象

car1.enter(park); // car1进入停车场,分配停车位

Truck truck(“鲁B-23456”, 15); // 创建卡车对象

truck.enter(park); // truck进入停车场,分配车位

car1.leave(park); // car1离开停车场,缴纳停车费

Bus bus(“鲁B-34567”, 50); // 创建公交车对象

bus.enter(park); // bus进入停车场,分配车位

/* 显示当前停放的车辆的车牌号码,以及当前的全部停车费收入*/

park->showInfo();

Car car2(“鲁B-45678”,“宝马320”); // 创建轿车对象

car2.enter(park);

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

bus.leave(park); // bus离开停车场,缴纳停车费

truck.leave(park); // truck离开停车场,缴纳停车费

/* 显示当前停放的车辆的车牌号码,以及当前的全部停车费收入*/

park->showInfo();

delete park;

return 0;

}

程序执行结果

程序执行结果如下:

请输入停车位数量:2

鲁B-12345进入停车场,分配停车位

鲁B-23456进入停车场,分配停车位

鲁B-12345离开停车场,缴纳停车费1元

鲁B-34567进入停车场,分配停车位

停车场目前停放了2辆汽车:鲁B-23456,鲁B-34567,共收入1元停车费

无法为鲁B-45678分配停车位

鲁B-34567离开停车场,缴纳停车费2元

鲁B-23456离开停车场,缴纳停车费3元

停车场目前停放了0辆汽车,共收入6元停车费

提示:停车场的停车位要用Automobile的指针数组表示。

Automobile **spaces;

spaces = new Automobile*[N];

spaces[i] = &car1;

delete[] spaces;

3 代码

#include <iostream>
#include <string>
#include <ctime>
#include <cstdlib>
#include <iomanip>
#include <vector>
#include <map>

using namespace std;
int N;

class Park;

class Automobile {
public:
    int cost;

    Automobile(string num) : car_num(num) {}

    virtual int pay();

    string get_car_num();

    void enter(Park *park);

    void leave(Park *park);

private:
    string car_num;
};

int Automobile::pay() { return 0; }

string Automobile::get_car_num() { return car_num; }

class Truck : public Automobile {
public:
    Truck(string name, double w) : Automobile(name), weight(w) { cost = 2; }

    int pay() override;

private:
    double weight;
};

int Truck::pay() {
    cout << get_car_num() << "离开停车场,缴纳停车费" << cost << "元\n";
    return cost;
}

class Car : public Automobile {
public:
    Car(string
        name,
        string br
    ) :

            Automobile(name), brand(br) { cost = 1; }

    int pay()

    override;

private:
    string brand;
};

int Car::pay() {
    cout << get_car_num() << "离开停车场,缴纳停车费" << cost << "元\n";
    return cost;
}

class Bus : public Automobile {
public:
    Bus(string name, int nu) : Automobile(name), num_personnel(nu) { cost = 3; }

    int pay() override;

private:
    int num_personnel;
};

int Bus::pay() {
    cout << get_car_num() << "离开停车场,缴纳停车费" << cost << "元\n";
    return cost;
}

class Park {
public:
    Park(int n) {
        N = n;
        spaces = new Automobile *[N];
        i = 0;
        income = 0;
    }

    void showInfo() {
        cout << "停车场目前停放了" << i << "辆汽车:";
        for (int j = 0; j < i; j++) { cout << spaces[j]->get_car_num() << ","; }
        cout << "共收入" << income << "元停车费\n";
    }

    void stop(Automobile *car1) {
        if (N > i) {
            spaces[i++] = car1;
            cout << car1->get_car_num() << "进入停车场,分配停车位\n";
        } else { cout << "无法为" << car1->get_car_num() << "分配停车位\n"; }
    }

    void go(Automobile *car1) {
        int j;
        for (j = 0; j < i; j++) {
            if (spaces[j]->get_car_num() == car1->get_car_num()) {
                i--;
                spaces[j] = NULL;
                for (int k = j; k <= i - 1; k++) { spaces[k] = spaces[k + 1]; }
                spaces[N - 1] = NULL;
                income += car1->pay();
                break;
            }
        }
    }

    ~Park() { delete[] spaces; }

private:
    Automobile **spaces;
    int i;
    int N;
    int income;
};

void Automobile::enter(Park *park) { park->stop(this); }

void Automobile::leave(Park *park) { park->go(this); }

int main() {
    cout << "请输入停车位数量:";
    cin >> N;
    Park *park = new Park(N);
    Car car1("鲁B-12345", "奥迪A6");
    car1.enter(park);
    Truck truck("鲁B-23456", 15);
    truck.enter(park);
    car1.leave(park);
    Bus bus("鲁B-34567", 50);
    bus.enter(park);
    park->showInfo();
    Car car2("鲁B-45678", "宝马320");
    car2.enter(park);
    bus.leave(park);
    truck.leave(park);
    park->showInfo();
    delete park;
    return 0;
}
  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值