我为我的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
这可能与我的代码,或者我需要考虑重写?社区可以给我的任何指导将非常有帮助。 谢谢。
凯尔
非常感谢您的建议。我今晚会尝试并发布更新。这看起来正是我所需要的。 –