COMP 2012 Object-Oriented Programming and Data Structures

Lab 4 Inheritance: Effect of Substitution and Slicing

Model events in a parking lot

In this lab, you are going to use the concept of inheritance to simulate the events happening in a parking lot. Specifically, you need to model the relationship between different types of car as well as the relationship between the cars and the parking lot. The source files are available HERE.

Description

In order to manage the parking lot more clearly by documenting the entering and leaving information, you are required to construct a parking lot management system.

For simplicity, we assume that the parking lot has only 2 parking spots, and each vehicle will only occupy 1 parking spot. The fee only depends on the types of vehicle and the fee rate of the parking lot. The parking time is ignored in this lab.

Overview

There are 5 classes involved: ParkingLot, Vehicle, Bus, Truck, and PrivateCar. The relationships between them are shown in the following figures. Detailed descriptions for each class can also be found below.

Note: '+': public; '-': private; '#': protected

  1. ParkingLot
    This class contains the parking space to store the vehicles, the total income, the size of the parking space, the fee rate, and the current number of parking vehicles. The constructor takes in an int (as constant reference) as the size, and it is fixed to 2 in this lab. The destructor deletes the space allocated for the parking spaces. The function assign_space checks whether there is still a space for the new vehicle and assign the space for the vehicle. It will take a pointer of Vehicle as input and return a bool value to indicate the status. If there is a space, output plate_no enter the parking lot and get a parking spot. and return true, otherwise output Failed to assign space for plate_no. and return false. The function reclaim_space reclaims spaces, calculates and charges the fee from the vehicles according to their types, and add it to the income of the parking lot. The types are listed in a enumeration, namely, DEFAULT, PRIVATE_CAR, BUS, TRUCK, which are given. Bus, Truck, and PrivateCar objects will call this function with their corresponding type and the fee they should paid is calculated as type*fee_rate. If succeed in reclaiming a space, the function output plate_no leave the parking lot and pay fee for the space and return the fee, otherwise it output There is no a vehicle with the plate No. of plate_no. and return -1 to indicate the failure. The function print shows the information of the parking lot, including the parking vehicles and the total income.
  2. Vehicle
    This class contains the plate number, the total fee that has been charged, the status of parking, and the accessor function of the plate number. The constructor takes in a string (as constant reference) as the plate number. The function enter triggers the assign_space function of the parking lot and changes the status of itself to true. The function leave triggers the reclaim_space function of the parking lot, calculates the fee, add it to the total fee, and changes the status of itself to false. Remember to check the status of the vehicle before triggering the reclaim_space function. The function print shows the information of the vehicle, including the plate number and the total fee paid for parking.
  3. Bus
    This class is derived from the Vehicle class. This class contains the number of passengers. The constructor takes in a string and an int as the plate number and the number of passengers respectively. It has 2 member functions: leave and print. The leavefunction triggers the reclaim_space function of the parking lot, calculates the fee, add it to the total fee, and changes the status of the bus to false. Remember to check the status of the vehicle before triggering the reclaim_space function. If the vehicle is not parking, output plate_no is not parking so it cannot leave!. The print function prints the plate number, the number of passengers, and the total fee paid for parking.
  4. Truck
    This class is derived from the Vehicle class. This class contains capacity. The constructor takes in a string and an int as the plate number and the capacity respectively. It has 2 member functions: leave and print. The leave triggers the reclaim_space function of the parking lot, calculates the fee, add it to the total fee, and changes the status of the truck to false. Remember to check the status of the vehicle before triggering the reclaim_space function. If the vehicle is not parking, output plate_no is not parking so it cannot leave!. The print function prints the plate number, the capacity, and the total fee paid for parking.
  5. PrivateCar
    This class is derived from the Vehicle class. This class contains brand. The constructor takes in 2 strings as the plate number and the brand respectively. It has 2 member functions: leave and print. The leave triggers the reclaim_space function of the parking lot, calculates the fee, add it to the total fee, and changes the status of the private car to false. Remember to check the status of the vehicle before triggering the reclaim_space function. If the vehicle is not parking, output plate_no is not parking so it cannot leave!. The print function prints the plate number, the brand, and the total fee paid for parking.

Lab tasks

  1. Finish the TODOs in ParkingLot.h/.cpp, Truck.cpp/.h, Bus.cpp/.hand PrivateCar.cpp/.h
    • The class definition of ParkingLot is given to you in ParkingLot.h and the constructor, destructor and assign_space member function has already been implemeted in ParkingLot.cpp. You will need to implement the other member functions.
    • The class definition and implementations of Vehicle is given to you in Vehicle.h and Vehicle.cpp for your reference.
    • You need to define and implement the derived classes, PrivateCar, Bus and Truck.
  2. Compile the project.
  3. Run the executable to check with the expected output below:
    Currently 0 vehicles in this parking lot, the total income is 0.
    The private car is AS134 with the brand of M-Benz. It has paid totally 0.
    AS134 enter the parking lot and get a parking spot.
    Currently 1 vehicles in this parking lot: AS134, the total income is 0.
    AS134 has been parking so it cannot leave!
    Currently 1 vehicles in this parking lot: AS134, the total income is 0.
    AS134 leave the parking lot and pay 2.4 for the space.
    Currently 0 vehicles in this parking lot, the total income is 2.4.
    The private car is AS134 with the brand of M-Benz. It has paid totally 2.4.
    The truck is XY778 with the capacity of 15. It has paid totally 0.
    The bus is QW907 with 12 passengers. It has paid totally 0.
    XY778 is not parking so it cannot leave!
    XY778 enter the parking lot and get a parking spot.
    QW907 enter the parking lot and get a parking spot.
    Currently 2 vehicles in this parking lot: XY778, QW907, the total income is 2.4.
    Failed to assign space for CY890.
    QW907 leave the parking lot and pay 4.8 for the space.
    XY778 leave the parking lot and pay 7.2 for the space.
    The bus is QW907 with 12 passengers. It has paid totally 4.8.
    The truck is XY778 with the capacity of 15. It has paid totally 7.2.
    Currently 0 vehicles in this parking lot, the total income is 14.4.
                             

Hint

  1. Remember to initialize data members when their values are NOT provided in constructor parameter list.
  2. Reading the relationship graph provided above will help you implement inheritance across classes and re-use functions when needed.
  3. Remember to use the correct data type to achieve the expected output.

Submission Deadline and Guidelines:

The lab assignment is due on 10 minutes after the end of your lab session.
We will use an online grading system ZINC to grade your lab work. Therefore, you are supposed to upload the following files in a zip file to ZINC:
- Bus.h
- Bus.cpp
- ParkingLot.h
- ParkingLot.cpp
- PrivateCar.h
- PrivateCar.cpp
- Truck.h
- Truck.cpp
You can submit your codes multiple times to ZINC before the deadline. Only the last submission will be graded.

Page maintained by
  • Yuxuan ZHOU
  • Last Modified:
Homepage