Hey everyone.

I need to write program that builds an object of Pizza class and then pass that object to Order class.

here's my Pizza class:

class Pizza
{
public:
    void setCheeseToppings ( int ) ;
    void setPepperoniToppings ( int ) ;
    void setSize ( string ) ;
    void setType ( string ) ;
private:
    int cheeseTop ;
    int pepperoniTop ;
    string size [ 3 ] = { "small" , "medium" , "large" } ;
    string type [ 3 ] = { "hand tossed" , "pan" , "deep dish" } ;
} ;

I know this is a general question, but how should my order.addPizza function look? Do I need to put the same variables that were in pizza into the orders class to store them or do i have access to those variables since the object has been passed?

I'm stuck here:

class Order
{
public:
    void addPizza ( Pizza ) ;
    void outOrder ( ) ;
private:
} ;

If Order can contain one or many pizzas then it should have a vector of pizza objects, and pass the Pizza object by referenct so prevent unnecessary duplication of the object.

class Order
{
public:
    void addPizza ( Pizza& order) ;
    void outOrder ( ) ;
private:
    vector<Pizza> theOrders;
} ;

a vector is a c++ array class, so just use a loop to access each Pizza object

int n = theOrders.size(); // get number of elements in the array
for(int i = 0; i < n; i++)
{
   outOrder(theOrders[i]);
}

You will want to add a parameter to outOrder function, similar to the parameter to addPizza().

>So how would I access the Pizza data to output it?
Probably define a member function or overloaded << operator that performs the output. In the code you posted, the Pizza class doesn't provide any way to get at the data. For example (keeping it simple):

class Pizza
{
public:
    void setCheeseToppings ( int ) ;
    void setPepperoniToppings ( int ) ;
    void setSize ( string ) ;
    void setType ( string ) ;

    template <typename CharT>
    friend std::basic_ostream<CharT>& operator<< (
      std::basic_ostream<CharT>& out, const Pizza& pie );
private:
    int cheeseTop ;
    int pepperoniTop ;
    string size;
    string type;
} ;

template <typename CharT>
std::basic_ostream<CharT>& operator<< (
  std::basic_ostream<CharT>& out, const Pizza& pie )
{
  return out<<"Cheese: "<< ( cheeseTop ? "Yes" : "No" );
}

Ok, so in my outOrders function would I be able to do something like:

cout << MyPizza.size << endl :

?

>cout << MyPizza.size << endl
No, size is a private data member. Unless the outOrders function is a friend of the Pizza class or a member function of a class which is a friend of the Pizza class, you don't have access to the private data. However, if you define the operator as I showed, you can do this:

cout<< MyPizza <<'\n';

And all of the private data you choose to output in the overloaded operator will be sent to cout in the way you format it, followed by a newline. Another example, even simpler:

#include <iostream>
#include <ostream>

class Time {
  int _minutes;
  int _seconds;
  bool _is_am;
public:
  Time ( int minutes, int seconds, int is_am )
    : _minutes ( minutes ), _seconds ( seconds ), _is_am ( is_am )
  {}

  friend std::ostream& operator<< ( std::ostream& out, const Time& t );
};

std::ostream& operator<< ( std::ostream& out, const Time& t )
{
  return out<< t._minutes <<':'<< t._seconds <<' '
    << ( t._is_am ? "AM" : "PM" );
}

int main()
{
  Time now ( 9, 55, true );

  std::cout<<"The time is "<< now <<". Isn't that nifty.\n";
}

is this

friend std::ostream& operator<< ( std::ostream& out, const Time& t );

the same as this

friend cout << ( ostream & out , const Time & t )

? Or am I utterly confused

template <typename CharT>
std::basic_ostream<CharT>& operator<< ( std::basic_ostream<CharT>& out, const Pizza& pie )

none of that^ makes since to me. =(

here's my updated .h file if that will help:

#pragma once

#include <string>
#include <iostream>
#include <iomanip>
using namespace std ;

class Pizza
{
public:
    void setCheeseToppings ( int ) ;
    void setPepperoniToppings ( int ) ;
    void setSize ( istream & is ) ;
    void setType ( istream & is ) ;
private:
    int cheeseTop ;
    int pepperoniTop ;
    string size ;
    string type ;
} ;

class Order
{
public:
    void addPizza ( Pizza ) ;
    void outOrder ( ) ;
private:
} ;

example main:

int main()
{
    Order myOrder;                    // variable declarations
    Pizza cheesy;
    Pizza pepperoni;

    cout << "Cheese size type:  " << endl ;
    cheesy.setCheeseToppings(3);    // specify pizza
    cheesy.setSize( cin );
    cheesy.setType( cin );

    cout << "pep size type:  " << endl ;
    pepperoni.setSize( cin );        // specify pizza
    pepperoni.setPepperoniToppings(2);
    pepperoni.setType( cin );
    
    cout << pepperoni.size << endl << pepperoni.type << endl ;
    cout << cheesy.size << endl << cheesy.type << endl ;

    myOrder.addPizza(cheesy);        // add to order
    myOrder.addPizza(pepperoni);

    myOrder.outputOrder();            // ouput order
    cout << "\n -- Done! --\n";
    return 0;
}

>Or am I utterly confused
You're utterly confused. All of the things that cout can print have an operator<< defined for them. When you want to use this syntax to print your own custom classes:

cout<< my_object <<endl;

You have to overload the operator<< function for your class. The operator<< function should look like this for any given class and output stream:

[I]<output stream>[/I]& operator<< ( [I]<output stream>[/I]& out, const [I]<your class>[/I]& obj );

However, if you want the operator to have access to any private or protected members of the class, it has to be a friend of the class as well.

>friend cout << ( ostream & out , const Time & t )
I don't even know what this is supposed to be.

>none of that^ makes since to me. =(
It's the same thing, just more flexible because you're not restricted to std::ostream objects. For example, you can use both cout and wcout where the example that uses std::ostream can only use cout.

oh Ok, but how do classes access variables in other classes?

If my question isn't clear, maybe look at my example main() and see what I'm trying to do. I am completely lost on how to write the Orders class so that I can output the Pizza information. :(

I need to use a vector in the Orders class to store the pizza objects and then output them with the output func

>but how do classes access variables in other classes?
There are

1) Make the data members public, then anyone can access them:

class test {
public:
  int foo;
};

int main()
{
  test t;
  t.foo = 123;
}

2) Make the data members protected, then derived classes can access them:

class test {
protected:
  int foo;
};

class derived: public test {
public:
  void bar() { foo = 123; }
};

3) Make the function or class that accesses a data member a friend of the class:

class test {
  friend void bar ( test& t );
  friend class baz;
private:
  int foo;
};

void bar ( test& t )
{
  t.foo = 123;
}

class baz {
public:
  void blah ( test& t ) { t.foo = 123; }
};

4) Provide a public or protected member function that accesses the data member:

class test {
private:
  int foo;
public:
  int get_foo() { return foo; }
  void set_foo ( int value ) { foo = value; }
};

int main()
{
  test t;

  t.set_foo ( 123 );
  int bar = t.get_foo();
}

>I am completely lost on how to write the Orders class so that I can output the Pizza information.
Okay, a pizza contains all of its information, right? And an order is just a collection of pizzas. So what you want to do is have the Pizza class provide a way to print or return its information so that the Orders class can use that information. Like this (I'll even eschew the operator overloading as that seems to confuse you):

#include <iostream>
#include <string>
#include <vector>

class Pizza {
  std::string _name;
public:
  Pizza ( std::string name )
    : _name ( name )
  {}

  void display() const
  {
    std::cout<< _name <<'\n';
  }
};

class Orders {
  std::vector<Pizza> _pizzas;
public:
  void add_pizza ( const Pizza& pie )
  {
    _pizzas.push_back ( pie );
  }

  void display() const
  {
    std::vector<Pizza>::const_iterator it = _pizzas.begin();
    std::vector<Pizza>::const_iterator end = _pizzas.end();

    while ( it != end ) {
      it->display();
      ++it;
    }
  }
};

int main()
{
  Orders my_order;

  my_order.add_pizza ( Pizza ( "Chef Special" ) );
  my_order.add_pizza ( Pizza ( "Meat Lovers" ) );
  my_order.add_pizza ( Pizza ( "Veggie Lovers" ) );

  my_order.display();
}

It's just that easy.

Comments
great explanation

Oh ok! that makes perfect sense! The only part I'm consfused about is this

it->display();

what is 'it->'

>That's a pointer?
It's a pointer-like object.

>I don't get it..... we haven't went over pointers yet
Then do this:

void display() const
{
  std::vector<Pizza>::size_type i;

  for ( i = 0; i < _pizzas.size(); i++ )
    _pizzas[i].display();
}

thanks for all the help Narue! My program is working (almost) great!

I've just got a logic error somewhere that I'm trying to figure out. for some reason, when I output the Pepperoni pizza it outputs 1 cheese topping every time.

This question has already been answered. Start a new discussion instead.