我要为一家只有 50 个座位的电影院设计订票系统(5 排,每排 10 个座位)
下面给了我Cinema类,这个不应该修改
class Cinema{
private:
Ticket ticket[50]; // not sure what it is going on
public:
Cinema(); //constructor
double purchaseTicket(int); // ticket ID as parameter, check if it is available, if so update it as unavailable. If not, return 0.
void listAll();
};
这是 Ticket 类
class Ticket{
private:
int ID[50]; //ticket ID (correct to store data in array?)
int price; // ticket price
// have to provide set and get function for ID and price, have no idea even I've googled on this topic
bool available[50]; // availability of ticket
public:
Ticket(); //constructor
bool status(int); // return availability of ticket
void setAvailable(int); //update status of ticket as available
void buy(int); //update status of ticket as unavailable
};
这是模拟购票功能的主要功能(给定的)
int main(){
Cinema myCinema;
myCinema.listAll(); // available seats print "O", otherwise print "X"
//simulate to purchse ticket 15, 16, 17
double price = 0;
price += myCinema.purchaseTicket(15);
price += myCinema.purchaseTicket(16);
price += myCinema.purchaseTicket(17);
cout <<"\nTotal Price: $" << price << endl << endl;
//print the current status
myCinema.listAll();
return 0;
}
谁能告诉我如何使用“Ticket ticket[50];” ?这让我很烦恼,很多步骤无法完成
如果 Ticket 类中有任何问题,也请告诉我
最佳答案
每个座位都有一张票。因此要购买五十张票。这种看待事物的方式有点奇怪,但我认为您可以将“票”视为“座位”的同义词。
您的机票等级有误。请记住,票对象代表一张单独的票(代表一个单独的座位)。因此,一张票有一个 ID 和一个可用性。因此票类中的数组是错误的。
class Ticket{
private:
int ID; //ticket ID
int price; // ticket price
bool available; // availability of ticket
public:
...
};
setter 和 getter 很简单
int Ticket::getPrice() const { return price; }
void Ticket::setPrice(int p) { price = p; }
可用性类似。
刚注意到 Cinema::purchaseTicket
返回一个 double
,我不知道为什么。
关于C++ 类 - 对象数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20420591/