C++実験七類の継承と派生
4343 ワード
2.1駐車場プログラム
(1)問題の説明
テーマの要求に従って簡単な駐車場管理プログラムを完成してください.
1.駐車場(Park)にはN個の駐車スペース(space)があり、各駐車スペースにはトラック(T乗用車(Car)、バス(Bus)を含む異なるタイプの自動車(Automobile)を駐車することができるが、同じ時刻に1個の駐車スペースには0または1台の自動車しか駐車できない.空き駐車スペースがない場合は、ヒントが表示されますが、車両の駐車スペースは手配されません.
2.プログラムは車両の駐車状況をシミュレートする:新しく来た車両に空席があれば、その車に順番に駐車スペースを割り当てる.車が出発するときは、駐車料金を払わなければなりません.
3.駐車場には、現在駐車している車両のナンバープレートと、現在のすべての駐車料金が表示されます.
4.ナンバープレート(文字列)のメンバーデータを含む自動車ベースのAutomobileを定義します.
5.派生クラスTruck、Car、Busを定義します.これらの車両はナンバープレートのほか、それぞれ異なる属性を持っている.Truckはまた、荷重特性(浮動小数点数、単位トン)を含む.Carにはブランド属性(文字列)もあり、Busには承認者数(整数)も含まれています.また、各派生クラスではpay()関数を実装し、車両情報を表示し、駐車料金を支払う.このうち、Truckは3元/回、Carは1元/回、Busは2元/回です.
(1)問題の説明
テーマの要求に従って簡単な駐車場管理プログラムを完成してください.
1.駐車場(Park)にはN個の駐車スペース(space)があり、各駐車スペースにはトラック(T乗用車(Car)、バス(Bus)を含む異なるタイプの自動車(Automobile)を駐車することができるが、同じ時刻に1個の駐車スペースには0または1台の自動車しか駐車できない.空き駐車スペースがない場合は、ヒントが表示されますが、車両の駐車スペースは手配されません.
2.プログラムは車両の駐車状況をシミュレートする:新しく来た車両に空席があれば、その車に順番に駐車スペースを割り当てる.車が出発するときは、駐車料金を払わなければなりません.
3.駐車場には、現在駐車している車両のナンバープレートと、現在のすべての駐車料金が表示されます.
4.ナンバープレート(文字列)のメンバーデータを含む自動車ベースのAutomobileを定義します.
5.派生クラスTruck、Car、Busを定義します.これらの車両はナンバープレートのほか、それぞれ異なる属性を持っている.Truckはまた、荷重特性(浮動小数点数、単位トン)を含む.Carにはブランド属性(文字列)もあり、Busには承認者数(整数)も含まれています.また、各派生クラスではpay()関数を実装し、車両情報を表示し、駐車料金を支払う.このうち、Truckは3元/回、Carは1元/回、Busは2元/回です.
#include
#include
using namespace std;
class Automobile;
class Park {
public:
Park(int N);
~Park();
void showInfo();
bool assignSpace(Automobile *pa);
bool reclaimSpace(Automobile *pa, int fee);
private:
Automobile **spaces;
int income;
int N;
int numAuto;
};
class Automobile {
public:
Automobile(string plateNO);
void enter(Park &park);
void leave(Park &park);
string getPlateNO();
protected:
string plateNO;
};
Park::Park(int N) {
spaces = new Automobile*[N];
for (int i = 0; i < N; i++)
spaces[i] = NULL;
income = 0;
this->N = N;
numAuto = 0;
}
Park::~Park() {
delete[] spaces;
}
void Park::showInfo() {
if (numAuto == 0)
cout << " " << numAuto << " , " << income << " ";
else {
cout << " " << numAuto << " :";
for (int i = 0; i < N; i++)
if (spaces[i] != NULL)
cout << spaces[i]->getPlateNO() << ",";
cout << " " << income << " " << endl;
}
}
bool Park::assignSpace(Automobile *pa) {
for (int i = 0; i < N; i++) {
if (spaces[i] == NULL) {
spaces[i] = pa;
numAuto++;
cout << pa->getPlateNO() << " , " << endl;
return true;
}
}
cout << " " << pa->getPlateNO() << " " << endl;
return false;
}
bool Park::reclaimSpace(Automobile *pa, int fee) {
for (int i = 0; i < N; i++) {
if (spaces[i] == pa) {
spaces[i] = NULL;
numAuto--;
income += fee;
cout << pa->getPlateNO() << " , " << fee << " " << endl;
return true;
}
}
cout << " " << pa->getPlateNO() << " ";
return false;
}
Automobile::Automobile(string plateNO) :
plateNO(plateNO) {
}
void Automobile::enter(Park &park) {
park.assignSpace(this);
}
void Automobile::leave(Park &park) {
}
string Automobile::getPlateNO() {
return plateNO;
}
class Truck: public Automobile {
public:
Truck(string plateNO, double capacity) :
Automobile(plateNO), capacity(capacity) {
}
void leave(Park &park);
protected:
double capacity;
};
void Truck::leave(Park &park) {
park.reclaimSpace(this, 3);
}
class Car: public Automobile {
public:
Car(string plateNO, string brand) :
Automobile(plateNO), brand(brand) {
}
void leave(Park &park);
protected:
string brand;
};
void Car::leave(Park &park) {
park.reclaimSpace(this, 1);
}
class Bus: public Automobile {
public:
Bus(string plateNO, int numPassengers) :
Automobile(plateNO), numPassengers(numPassengers) {
}
void leave(Park &park);
protected:
int numPassengers;
};
void Bus::leave(Park &park) {
park.reclaimSpace(this, 2);
}
int main() {
int N = 0;
cout << " :";
cin >> N; // b , 2
Park 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();
return 0;
}