I’m in the process of learning C++. I’ve come across a rather interesting issue where I wish to store within a Customer class their food tray. The basic idea would be that one customer can have a tray which consists of drinks and foods.
My original thought was to use following class.
class Customer
{
private:
std::string firstName;
std::string lastName;
int tablenumber;
//LinkList<Tray> myTray = new LinkList<Tray>();
//or
//LinkList<Tray> myTray;
public:
Customer();
Customer(std::string sFirstName, std::string sLastName,
int sTableNumber);
~Customer(void);
What would be the correct way for dealing with having an object store a Linklist within itself? So upon calling the customer constructor, they can have orders added to it?
Let me address a more fundamental issue before going to the details.
It is not good to think about
LinkList<Tray> myTray = new LinkList<Tray>();as a solution.Think about it this way, Every Customer will have their own Tray. So you need a new tray for each customer.
Remember that a class is just a blueprint.
So go with
LinkList<Tray> myTray;and then in constructor of your object, allot a new tray each time a customer is created.it would look like :
Note that you must now declare it like
LinkList<Tray> * myTray;if you want to allocate the list dynamically.now you can use
myTrayas per your requirements. Eg. you may like to callmyTray.addToList(MyNewItem)and the likes.Assumption:
When every object shares the same value, you declare them as
static. But you mentionedwithin a Customer classtheirfood trayso i am assuming this is not the case here.