我爲我的C++類編寫了一個程序,遇到了一個小問題,我不太清楚如何解決。你們中的一些人可能熟悉這本教科書練習,因爲我以前在網站上看到過有關它的問題,但我找不到任何簡單的解決方法。類和在C++中創建多個對象
我必須創建一個用於存儲關於披薩信息的類。我已經編寫了程序和功能,但是我需要類調用來循環執行基於用戶輸入的一系列迭代。我知道這可以通過使用矢量來實現,這是我們在這個學期還沒有打到的東西,但我很快就會得到足夠的信息。有沒有辦法做到這一點沒有載體?
這是班級。
class Pizza
{
private:
int type;
int size;
int numCheeseTopping;
int numPepperoniTopping;
bool cheeseTopping;
bool pepperoniTopping;
public:
Pizza();
int getType();
int getSize();
bool getCheese();
bool getPepperoni();
void setType(int t);
void setSize(int s);
void setCheese(bool choice, int temp);
void setPepperoni(bool choice, int temp);
void outputDescription();
double computePrice();
void outputPrice();
};
而構造。
Pizza::Pizza()
{
// Set initial class values
type = DEEPDISH;
size = SMALL;
cheeseTopping = false;
numCheeseTopping = 0;
pepperoniTopping = false;
numPepperoniTopping = 0;
}
主要只有兩個功能。
// Main function
int main()
{
// Call global functions
welcomeMsg();
buildPizza();
return 0;
}
我有一種感覺,我的問題在於buildPizza函數,因爲它調用其他函數以及創建對象。這是...
void buildPizza()
{
char pType, pSize, tempCheese, tempPepperoni;
int type = 0, size = 0, numCheeseTopping = 0, numPepperoniTopping = 0;
// Ask user what size pizza they would like.
cout << "What size pizza would you like?" << endl;
cout << "\tS: Small" << endl;
cout << "\tM: Medium" << endl;
cout << "\tL: Large" << endl;
cout << "Size: ";
cin >> pSize;
// Determine which size the user input and convert the
// result.
switch (pSize)
{
case 'S':
case 's':
size = SMALL;
break;
case 'M':
case 'm':
size = MEDIUM;
break;
case 'L':
case 'l':
size = LARGE;
break;
}
// Ask the user which type of pizza they would like.
cout << endl << "What type pizza would you like?" << endl;
cout << "\tD: Deepdish" << endl;
cout << "\tH: Hand-Tossed" << endl;
cout << "\tP: Pan" << endl;
cout << "Type: ";
cin >> pType;
// Determine which type the user input and convert the
// result.
switch (pType)
{
case 'D':
case 'd':
type = DEEPDISH;
break;
case 'H':
case 'h':
type = HANDTOSSED;
break;
case 'P':
case 'p':
type = PAN;
break;
}
// Call Pizza Class.
Pizza myPizza;
// Call Pizza Class functions.
myPizza.setSize(size);
myPizza.setType(type);
// Ask user whether they want cheese or not.
cout << endl << "Would you like cheese (y/n)? ";
cin >> tempCheese;
// If so call setCheese.
if (tempCheese == 'Y' || tempCheese == 'y')
{
cout << "How many cheese toppings would you like? ";
cin >> numCheeseTopping;
myPizza.setCheese(true, numCheeseTopping);
}
// Ask user whether they want pepperoni or not.
cout << endl << "Would you like pepperoni (y/n)? ";
cin >> tempPepperoni;
// If so call setPepperoni.
if (tempPepperoni == 'Y' || tempPepperoni == 'y')
{
cout << "How many pepperoni toppings would you like? ";
cin >> numPepperoniTopping;
myPizza.setPepperoni(true, numPepperoniTopping);
}
// Call outputDescription to give user an overview
// of their order.
cout << endl << endl;
myPizza.outputDescription();
cout << endl;
// Compute the cost of the pizza and display it.
myPizza.outputPrice();
}
基本上,我想程序向用戶詢問他們想要多少比薩餅評估,創建許多類迭代,然後循環「建築」或「排序」每個披薩,然後顯示一個總數並返回0.
正如我現在看代碼,我可以從buildPizza拿出最後兩個函數調用,並將調用轉移到main,但這不會解決我的問題。我只是簡單地在節目中注意到了一個疏忽。
是否有一種簡單的方法可以在運行時一次創建200個新對象。每個人都有不同的名字?我應該選擇一個數字來評估並強制用戶輸入許多對象的信息嗎?目前,該計劃評估一個比薩並退出。
我想這樣的事情發生:
- 用戶詢問程序來創建一個5披薩訂單。
- 程序創建5個披薩對象。
- 程序迭代每個對象獲取和設置每個對象的信息。
- 計劃顯示一些東西,返回0
這可能與我的代碼,或者我需要考慮重寫?社區可以給我的任何指導將非常有幫助。 謝謝。
凱爾
非常感謝您的建議。我今晚會嘗試併發布更新。這看起來正是我所需要的。 –