【答案】應助回帖
★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ 感謝參與,應助指數(shù) +1 zhaowei8686(jjdg代發(fā)): 金幣+5, 辛苦了 2014-05-24 01:25:55 zhaowei8686: 金幣+25, ★★★★★ 最佳答案 2014-05-24 08:21:27
引用回帖: 3樓 : Originally posted by
zhaowei8686 at 2014-05-23 21:23:13
一家汽車公司銷售不同類型的汽車。在采購時,客戶可以選擇不同類型的發(fā)動機,車體(比如雙門跑車,轎車,房車等),車身顏色等等..
你可以創(chuàng)建一個C + +類的架構,根據(jù)我們的需求,來提供一個汽車對象。
每 ...
只能幫你到這里了,復制進一個后綴為cpp的文件運行一下,根據(jù)需要再改改,添加一些需要的函數(shù)CODE: #include <iostream>
#include <string>
using namespace std;
class Car
{
private:
int mileage;
int fuel; // fuel gauge: 0-Diesel, 1-Petrol
int seat;
double engine;
int type; // body type: 0-Coupé, 1-Sedan, 2-Estate
int color; // body color: 0-Red, 1-Orange, 2-Yellow, 3-Green, 4-Blue, 5-Purple, 6-Magenta
int direction; // directions: 0-North, 1-East, 2-South, 3-West
int status; // status: 0-OFF, 1-ON
static string types[3];
static string dirs[4];
static string colors[7];
public:
Car(double e, int t, int c, int m=0, int f=0, int n=4, int d=0, int s=0) : mileage(m), fuel(f), seat(n),
engine(e), type(t), color(c), direction(d), status(s) { }
int getMileAge(void) { return this->mileage; }
string getFuleType(void) { return this->fuel?"Petrol":"Diesel"; }
int getSeats(void) { return this->seat; }
double getEngine(void) { return this->engine; }
string getType(void) { return this->types[this->type]; }
string getColor(void) { return this->colors[this->color]; }
void setEngine(double e) { this->engine = e; }
void setType(int t) { this->type = t; }
void setColor(int c) { this->color = c; }
void turnRight(void) { cout << "Turning right..." << endl; this->direction++; this->direction %= 4; }
void turnLeft(void) { cout << "Turning left..." << endl; this->direction = this->direction==0?3:this->direction-1;}
string getDirection(void) { return this->dirs[this->direction]; }
string getStatus(void) { return this->status?"ON":"OFF"; }
void startEngine(void) { if (!this->status) { cout << "Starting engine..." << endl; } this->status = 1; }
void stopEngine(void) { if (this->status) { cout << "Stop engine..." << endl; }this->status = 0; }
void printInfo(void)
{
cout << "\nCar information: " << endl
<< "\tEngine size:\t" << this->getEngine() << " L" << endl
<< "\tType:\t\t" << this->getType() << endl
<< "\tColor:\t\t" << this->getColor() << endl
<< "\tMileage:\t" << this->getMileAge() << " mile(s)" << endl
<< "\tFuel:\t\t" << this->getFuleType() << endl
<< "\tSeats:\t\t" << this->getSeats() << endl
<< "\n\tDirection:\t" << this->getDirection() << endl
<< "\tEngine is:\t" << this->getStatus() << endl;
}
};
string Car::dirs[] = {"North","East","South","West"};
string Car::types[] = {"Coupé", "Sedan","Estate"};
string Car::colors[] = {"Red","Orange","Yellow","Green","Blue","Purple","Magenta"};
class Company
{
public:
Car* getCar(double engine, int type, int color)
{ return new Car(engine,type,color); }
};
int main()
{
Company CarCompany = Company();
Car* myCar = CarCompany.getCar(1.6,0,1);
myCar->printInfo();
myCar->startEngine();
myCar->printInfo();
myCar->turnRight();
myCar->printInfo();
myCar->turnRight();
myCar->printInfo();
myCar->turnRight();
myCar->printInfo();
myCar->turnRight();
myCar->printInfo();
myCar->turnLeft();
myCar->printInfo();
myCar->stopEngine();
myCar->printInfo();
return 0;
}
運行結果CODE:
Car information:
Engine size: 1.6 L
Type: Coupé
Color: Orange
Mileage: 0 mile(s)
Fuel: Diesel
Seats: 4
Direction: North
Engine is: OFF
Starting engine...
Car information:
Engine size: 1.6 L
Type: Coupé
Color: Orange
Mileage: 0 mile(s)
Fuel: Diesel
Seats: 4
Direction: North
Engine is: ON
Turning right...
Car information:
Engine size: 1.6 L
Type: Coupé
Color: Orange
Mileage: 0 mile(s)
Fuel: Diesel
Seats: 4
Direction: East
Engine is: ON
Turning right...
Car information:
Engine size: 1.6 L
Type: Coupé
Color: Orange
Mileage: 0 mile(s)
Fuel: Diesel
Seats: 4
Direction: South
Engine is: ON
Turning right...
Car information:
Engine size: 1.6 L
Type: Coupé
Color: Orange
Mileage: 0 mile(s)
Fuel: Diesel
Seats: 4
Direction: West
Engine is: ON
Turning right...
Car information:
Engine size: 1.6 L
Type: Coupé
Color: Orange
Mileage: 0 mile(s)
Fuel: Diesel
Seats: 4
Direction: North
Engine is: ON
Turning left...
Car information:
Engine size: 1.6 L
Type: Coupé
Color: Orange
Mileage: 0 mile(s)
Fuel: Diesel
Seats: 4
Direction: West
Engine is: ON
Stop engine...
Car information:
Engine size: 1.6 L
Type: Coupé
Color: Orange
Mileage: 0 mile(s)
Fuel: Diesel
Seats: 4
Direction: West
Engine is: OFF
Process returned 0 (0x0) execution time : 0.062 s
Press any key to continue.