【发布时间】:2016-07-18 23:40:49
【问题描述】:
#include <iostream>
using namespace std;
//To know the car's make, model, color, license number, and the number of minutes the car has been parked
class ParkedCar
{
string make, model, color, license_number;
int minutes;
public:
ParkedCar(string make = "...", string model = "...", string color = "...",
string license_number = "...", int minutes = 0) : make{ make }, model{model},
color{color}, license_number{license_number}, minutes{minutes} { this->make = make; }
};
//To know the number of minutes of parking time that has been purchased
class ParkingMeter
{
int purchased_minutes;
public:
ParkingMeter(int purchased_minutes) { this->purchased_minutes = purchased_minutes; }
};
//To report the make, model, color, and license number of the illegally parked car
//To report the amount of the fine, which is $25 for the first hour, or part of an
//hour that the car is illegally parked, plus $10 for every additional hour or
//part of an hour that the car is illegally parked
//To report the name and badge number of the police officer issuing the ticket
class ParkingTicket
{
ParkedCar car;
ParkingMeter meter;
PoliceOfficer officer;
public:
ParkingTicket(const ParkedCar &car, const ParkingMeter &meter, const PoliceOfficer &officer)
{
this->car = car;
this->meter = meter;
this->officer = officer;
}
};
//To know the officer's name and badge number
//To examine a ParkedCar object and a ParkingMeter object, and determine whether the car's time has expired
//To issue a prking ticket (generate a ParkingTicket object) if the car's time has expired
class PoliceOfficer
{
string name;
int badge_number;
ParkedCar car;
ParkingMeter meter;
public:
PoliceOfficer(const ParkedCar &car, const ParkingMeter &meter, string name = "...", int badge_number = 000000)
{
this->car = car;
this->meter = meter;
}
};
所以我正在为学校制作这个文件,一切都很顺利,直到我上 ParkingTicket 和 PoliceOfficer 课程。我正在尝试定义这些构造函数,它们都告诉我另一个类没有默认构造函数。我将如何解决这个问题?谢谢! :)
【问题讨论】:
-
“我该如何解决这个问题?” 提供默认构造函数,不带参数,或者将它们全部设置为默认值。
-
ParkedCar 和 ParkingMeter 真的是警官的成员吗?警官真的由姓名、徽章、汽车和仪表组成吗?
-
是的,
ParkedCar和ParkingMeter真的是警察的成员。名称和徽章是特定于 PoliceOfficer 的属性,而我使用从构造函数接收的car和meter对象的类内副本来获取它们的值和函数 [我将稍后制作] 用于警察能够报告否则无法获得的信息。
标签: c++ constructor not-exists