How do I print out a vector from a class?

I have a class named Pizza that asks all the information and displays it properly and I have to make a class named Order that has a private vector that basically outputs the order and lets the customer order multiple pizzas. How exactly do I get there? I will post my Order.cpp and Order.h. Any help would be appreciated.

Order.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
  #include "Order.h"
#include "Pizza.h"
#include <vector>
#include <string>
#include <iostream>
using std::vector;
using namespace std;

Order::Order()
{
}

void Order::addPizza(Pizza myPizza)
{
    pizza.push_back(myPizza);
}

void Order::displayOrder()
{
    string name;
    int phone;

    cout<<endl<<"What is the name on the order? ";
    getline(cin,name);
    cout<<endl<<"What is the phone number on the order? ";
    cin>>phone;

    cout<<endl<<"The name on the order is: "<<name<<".";
    cout<<endl<<"The number on the order is: "<<phone<<"."<<endl;
}


Order.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#ifndef ORDER_H
#define ORDER_H
#include "Pizza.h"
#include <vector>
using std::vector;

class Order
{
    public:
        Order();
        void addPizza(Pizza);
        void displayOrder();
    private:
        vector <Pizza> pizza;
};

#endif // ORDER_H
You already have a Order::displayOrder function. All you need to do is to iterate through the vector of pizzas displaying each pizza.

You didn't show class pizza, so don't know if that class has a similar display function to display one pizza at a time.

1
2
3
//  After line 29
  for (int i=0; i<pizza.size(); i++)
    pizza[i].displayPizza();  // Assumes class pizza has a display function 




That worked perfectly. Thanks!
Topic archived. No new replies allowed.