类和在C ++中创建多个对象

时间:2017-04-06 07:57:56

标签: c++ class

我为我的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只有两个功能。

// 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();
}

基本上,我希望程序能够询问用户他们想要评估多少个比萨饼,创建多个类迭代,然后循环“建立”#。或者&#39;订购&#39;每个披萨,然后显示总数并返回0.

当我查看代码时,我可以从buildPizza中取出最后两个函数调用并将调用移到main中,但这不会解决我的问题。我只是在程序中注意到了疏忽。

是否有一种简单的方法可以在运行时同时创建200个新对象。每个人都有不同的名字?我应该只选择一个数字来评估并强制用户输入那么多对象的信息吗?现在,该程序评估一个披萨并退出。

我喜欢这样的事情:

  1. 用户要求程序创建5个披萨订单。
  2. 程序会创建5个披萨对象。
  3. 程序遍历每个对象获取并设置每个对象的信息。
  4. 程序显示一些内容并返回0.
  5. 这可能与我的代码有关,还是我需要考虑重写?社区可以给我的任何指导都会非常有帮助。 感谢。

    凯尔

2 个答案:

答案 0 :(得分:1)

既然你要使用数组或向量,那么就足够了。

auto nrPizzas = getNumberOfPizzasFromUserInput();
for(int i = 0; i < nrPizzas; i++) {
    auto pizza = Pizza{};
    // do your stuff here.
    output to the screen here();
}

答案 1 :(得分:0)

您可以提示用户想要的比萨饼数量,然后创建一个包含多个比萨饼的动态数组,然后遍历他们的构建函数,否则您可以使用结构类型的比萨饼制作链接列表,然后重写主要逻辑。如果您不想提示用户想要订购多少披萨,建议采用第二种方法。

相关问题