0

I'm doing a restaurant management program. Right now I'm trying to output private vector data and am getting stuck.

So I have a Menu.h

private:
vector<Category> categories;
vector<Menu_Item> menu_items;
vector<Recipe> recipes;
vector<Ingredient> ingredients;
vector<Order> orders;
vector<Order_Item> order_items;

And Menu.cpp

Menu.read()
Menu.show()

The read function reads from a file like this

1010    Appetizers                                      
1901    Entrees                                     
1576    Desserts                                        
1320    Drinks  

And stores those values into the appropriate vector, for example this one would be vector categories.

I also have a .h file for all the different types of things like, Menu_Item.h, Recipe.h, etc. And I store values into the vector like such:

menu_items.push_back(Menu_Item(meniID, catID, rID....

However in Menu_Item.h the values are

private:
int menu_item_id;
int cat_id;
int recipe_id;
string menu_item_name;
double price;   

The show() function queries the user what he/she wants to see. Let's say the user wants to see a specific menu item like Onion rings. What I can't do is

if(menu_items[0].menu_item_name == "Onion Rings")

because it says that menu_item_name value is private within Menu_Item.h. How can I access the private data?

Richard
  • 5,840
  • 36
  • 123
  • 208

2 Answers2

1

You have to make menu_items public or make a public getter function like the following.

public: 
   vector<Menu_Item> get_menu_items(){ return menu_items;}

Then say if you had a Menu object of this type called Menu you can do this:

if(Menu.get_menu_items()[0].menu_item_name == "Onion Rings")

The other possible option is that you make a friend class if another specific class needs access, though usually this won't be the best design decision.

In response to the comment you could do this:

for(size_t n=0, n<menu_items.size()-1, ++n){    
     if(Menu.get_menu_items()[n].menu_item_name == "Onion rings")
          cout << "something";
}
shuttle87
  • 15,466
  • 11
  • 77
  • 106
  • Now my idea was to do something like this `code for(int n=0, n – Richard Oct 15 '11 at 18:18
  • it says menu_item_name is inaccessible – Richard Oct 15 '11 at 18:41
  • Make menu_item_name public. Or make a get_menu_item_name() getter function. – shuttle87 Oct 15 '11 at 18:47
  • Sorry I'm getting very lost. I'm trying to use the for loop and I can't make any of those private variables public. So I made this `code string get_mi_name(){ return menu_item_name;}` then i have the `vector get_menu_items()`. However I have no idea how to get anything. Can you please clear this up for me. – Richard Oct 15 '11 at 18:56
  • @Richard: the `Menu_Item` class should have a `public: const string& get_name() const {return menu_item_name;}` Then in your loop instead of using `menu_items[i].menu_item_name` you type `menu_items[i].get_name()` instead. – Mooing Duck Oct 17 '11 at 17:10
1

Two options:

  1. Declare your fields in Menu_item.h as public, not private.
  2. Keep your fields as private, but create public getters (and setters) to access the fields.
Community
  • 1
  • 1
kevinfahy
  • 1,544
  • 1
  • 11
  • 7