Thread: homework assistance wanted: using a class as a vector

  1. #1
    Registered User
    Join Date
    Apr 2010
    Posts
    37

    homework assistance wanted: using a class as a vector

    I'm trying to create a pizza ordering system that consists of 2 classes. The first one contains all the information for a pizza, while the second consists of an order system for multiple pizzas. For the second one, class order, I am supposed to have a private vector of type pizza. And in this is where I am confused. I am unsure as how to actually use the vector as a class. Here is what I have so far.

    Code:
    #include <iostream>
    #include <vector>
    using namespace std;
    
    class pizza
    {
      private:
        int crust;
        int size;
        int cheese;
        int pepperoni;
    
      public:
        int getCrust();
        void setCrust(int newCrust);
        int getSize();
        void setSize(int newSize);
        int getPepperoni();
        void setPepperoni(int newPepperoni);
        int getCheese();
        void setCheese(int newCheese);
        void outputDescription();
        double computePrice();
        void takeOrder();
    };
    
    class order
    {
      private: 
        vector<pizza> customer;
    
      public:
        void customerOrder();
        void customerTotal();
    };
    
    int main () 
    {
      order test;
      test.customerOrder();
      test.customerTotal();
    }
    
    int pizza::getCrust()
    {
      return crust;
    }
    
    int pizza::getSize()
    {
      return size;
    }
    
    int pizza::getPepperoni()
    {
      return pepperoni;
    }
    
    int pizza::getCheese()
    {
      return cheese;
    }
    
    void pizza::setCrust(int newCrust)
    {
      crust = newCrust;
    }
    
    void pizza::setSize(int newSize)
    {
      size = newSize;
    }
    
    void pizza::setCheese(int newCheese)
    {
      cheese = newCheese;
    }
    
    void pizza::setPepperoni(int newPepperoni)
    {
      pepperoni = newPepperoni;
    }
    
    void pizza::outputDescription()
    { 
      cout << "\nThe pizza you ordered is a "; 
        if(size == 1)
          cout << "small ";
        else if(size == 2)
          cout << "medium ";
        else if(size == 3)
          cout << "large ";
      
        if(crust == 1)
          cout << "deep dish ";
        else if(crust ==2)
          cout << "hand tossed ";
        else if(crust ==3)
          cout << "pan ";
    
      cout << "\nwith";
        if(cheese == 0 && pepperoni ==0)
          cout << " plain cheese.";
        else if(cheese == 1 && pepperoni == 0)
          cout << " extra cheese.";
        else if(cheese == 1 && pepperoni == 1)
          cout << " extra cheese and pepperoni.";
        else if(cheese == 0 && pepperoni == 1)
          cout << " pepperoni.";
        else if(cheese == 0 && pepperoni == 2)
          cout << " double pepperoni.";
        else if(cheese == 1&& pepperoni == 2)
          cout << " extra cheese and double pepperoni.";
    }
    
    double pizza::computePrice()
    {
      double sizePrice, toppingsPrice = 0;
      double toppings = cheese + pepperoni;
      
      if(size == 1)
        sizePrice = 10.00;
      else if(size == 2)
        sizePrice = 14.00;
      else if(size == 3)
        sizePrice = 17.00;
    
      if(toppings == 0)
        toppingsPrice = 0.00;
      else if(toppings == 1)
        toppingsPrice = 2.00;
      else if(toppings == 2)
        toppingsPrice = 4.00;
      else if(toppings == 3)
        toppingsPrice = 6.00;
    
      double finalPrice = sizePrice + toppingsPrice;
      cout << "\n\nThe total price of the order is $" << finalPrice << endl;
      return finalPrice;
    }
    
    void order::customerOrder()
    { 
      int numberOfPizzas = 0;
      char morePizzas;
      do
      {
        ++numberOfPizzas;
      
        int i, newSize, newCrust = 0;
        int newCheese = 0;
        int newPepperoni = 0;
        char moreToppings, addToppings;
    
        cout << "What size of pizza would you like? " << endl;
        cout << "Type 1 for a small, 2 for a medium, or 3 for a large --> ";   
        cin >> newSize;
        setSize(newSize);
    	
        i = 0;
        cout << "\nWhat kind of crust would you like? " << endl;
        cout << "Type 1 for Deep Dish, 2 for Hand Tossed, or 3 for Pan --> ";
        cin >> newCrust;
        setCrust(newCrust);
    
        cout << "\nAll pizzas come plain with cheese ";
        cout << "do you wish to add extra cheese or pepperoni? ";
        cout << "Type 'Y' to add toppings to your pizza or type no to skip --> ";
        cin >> moreToppings;
        if(moreToppings == 'Y' || moreToppings == 'y')
        {
          cout << "All toppings are an extra $2.00 charge.";
          cout << "  Type 'Y' to add the toppings." << endl;
          cout << "Do you want EXTRA cheese? ";
          cin >> addToppings;
          if(addToppings == 'Y' || addToppings == 'y')
            newCheese++;
    
            cout << "Do you want pepperoni? ";
            cin >> addToppings;
            if(addToppings == 'Y' || addToppings == 'y')
            { 			
              newPepperoni++;
    				
               cout << "Do you want add double pepperoni? ";
               cin >> addToppings;
               if(addToppings == 'Y' || addToppings == 'y')
                 newPepperoni++;
             }
           }
    	
        setCheese(newCheese);
        setPepperoni(newPepperoni);
    
        cout << "\nDo you want to add another pizza to your order? Type 'Y' for yes ";
        cin >> morePizzas;
        }
    
      while(morePizzas == 'Y' || morePizzas == 'y');
    }
    
    void pizza::takeOrder()
    { 
      int i, newSize, newCrust = 0;
      int newCheese = 0;
      int newPepperoni = 0;
      char moreToppings, addToppings;
    
      cout << "What size of pizza would you like? " << endl;
      cout << "Type 1 for a small, 2 for a medium, or 3 for a large --> ";   
      cin >> newSize;
      setSize(newSize);
    	
      i = 0;
      cout << "\nWhat kind of crust would you like? " << endl;
      cout << "Type 1 for Deep Dish, 2 for Hand Tossed, or 3 for Pan --> ";
      cin >> newCrust;
      setCrust(newCrust);
    
      cout << "\nAll pizzas come plain with cheese ";
      cout << "do you wish to add extra cheese or pepperoni? ";
      cout << "Type 'Y' to add toppings to your pizza or type no to skip --> ";
      cin >> moreToppings;
      if(moreToppings == 'Y' || moreToppings == 'y')
      {
        cout << "All toppings are an extra $2.00 charge.";
        cout << "  Type 'Y' to add the toppings." << endl;
        cout << "Do you want EXTRA cheese? ";
        cin >> addToppings;
        if(addToppings == 'Y' || addToppings == 'y')
          newCheese++;
    
        cout << "Do you want pepperoni? ";
        cin >> addToppings;
        if(addToppings == 'Y' || addToppings == 'y')
        {			
          newPepperoni++;
    				
          cout << "Do you want add double pepperoni? ";
          cin >> addToppings;
          if(addToppings == 'Y' || addToppings == 'y')
            newPepperoni++;
    			}
          }
      setCheese(newCheese);
      setPepperoni(newPepperoni);
    }
    
    void order::customerTotal()
    {
      double finalTotal = finalTotal + computePrice();
    }
    Again, what I am unsure of is how to actually use the vector. I know that I am not actually plugging it in anywhere, but thats what I am trying to figure out how and where to use it. Thanks in advance for any advice.

  2. #2
    Registered User
    Join Date
    Mar 2009
    Posts
    344
    in CustomerOrder, you have to access a specific pizza in the customer vector (which I'd rename to something like pizza_list to better reflect how it's used, btw).

    Something like customer[numberOfPizzas].SetCrust() in place of just SetCrust(). And since vectors start with index 0, you'd want to increment numberOfPizzas last in the loop instead of first like you have it now.

  3. #3
    Registered User
    Join Date
    Apr 2010
    Posts
    37
    In other words change my function calls to versions of this...

    customer[numberOfPizzas].setSize(newSize);

    If I do that I get an error at that line saying that the vector subscript is out of range. I moved my ++numberOfPizzas line to the end, and moved the declaration down to where its inside the loop.

  4. #4
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    The first thing I would do is rearrange your code so that all of the "pizza" member functions are together. Then add the "order" member functions, and finally your main function. This will help when you put the classes into separate files and to make it easier to follow the program flow.

    You would use your vector<pizza> inside you order class member functions. When you start to take a customer order you would add a pizza to the order class.

    Here is a simplified customerOrder() to give you an idea.

    Your void pizza::takeOrder() should probably be part of the order class not the pizza class.

    Code:
    void order::customerOrder()
    {
       // Make a simple instance of the pizza class.
       pizza newOrder;
    
       // get the pizza's information.
       newOrder.setSize();
    
       // now push the pizza into the vector.
       order.customer.push_back(newOrder);
    }
    Note I did not try compiling the code.

    Jim

  5. #5
    Registered User
    Join Date
    Apr 2010
    Posts
    37
    Yeah, the layout of everything is a bit of a mess, I appologize. The assignment is in two parts, where the first one is just the basic pizza class and I meant to leave the pizza::takeOrder out, but over looked it in the preview.

    The pushback stuff makes sense and was a lot of what I was looking for in what I was missing. Debugging through the order::takeOrder function everything works and makes sense but how do I use the pizza functions outputDescription and calculateTotal without just adding them in the order::customerOrder function?

    As it stands now I have everything working and functioning as planned. I would like to be able to use pizza:utputDescription at the end as to list the description of all of the pizzas ordered and then list the final price. I would prefer to do this in a seperate function and not just inside my order::customerOrder.
    Last edited by DHart07; 11-02-2010 at 01:18 PM.

  6. #6
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    You would use pizza.outputDescription() from each pizza in the order.

    Because the pizza vector is private you will need to create a method to access and print out the orders.

    JIm

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Getting an error with OpenGL: collect2: ld returned 1 exit status
    By Lorgon Jortle in forum C++ Programming
    Replies: 6
    Last Post: 05-08-2009, 08:18 PM
  2. Vector Addition Using OOP?
    By everydaybh in forum C++ Programming
    Replies: 3
    Last Post: 04-09-2009, 05:09 AM
  3. Problems defining classes
    By esmeco in forum C++ Programming
    Replies: 47
    Last Post: 10-24-2007, 01:13 PM
  4. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  5. deriving classes
    By l2u in forum C++ Programming
    Replies: 12
    Last Post: 01-15-2007, 05:01 PM