Melky
Melky

Reputation: 167

Objects having their own Linked List

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?

Upvotes: 1

Views: 113

Answers (3)

Aman
Aman

Reputation: 8995

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 :

Customer()
{
//other construction 
 myTray = new LinkList<Tray>();
}

Note that you must now declare it like LinkList<Tray> * myTray; if you want to allocate the list dynamically.

now you can use myTray as per your requirements. Eg. you may like to call myTray.addToList(MyNewItem) and the likes.

Assumption:

When every object shares the same value, you declare them as static. But you mentioned within a Customer class their food tray so i am assuming this is not the case here.

Upvotes: 0

juanchopanza
juanchopanza

Reputation: 227518

It sounds like you want your customer to have the ability to hold many food items in a food tray. So it would make sense to hold some kind of container of food items (not necessarily a linked list) and give the Customer type methods to add or remove food items. This container would represent the tray you're talking about:

class Customer
{
private:
    std::string firstName;
    std::string lastName;
    LinkList<FoodItem> myTray;
public:
    AddFoodItemToTray(const FoodItem& item) { myTray.push(item);}
    RemoveFoodItemFromTray(const FoodItem& item) { myTray.remove(item=; } 
};

If you want your Customer class to be initializable from a list of elements, then you can just add a constructor for that:

explicit Customer(const LinkList<FoodItem>& tray) : myTray(tray) {}

It is probably best to leave the table number out of the customer, and let some kind of table class know which curtomers it holds.

Upvotes: 1

Debobroto Das
Debobroto Das

Reputation: 862

write another constructor which takes linkedlist of tray type object as parameter.

Upvotes: 0

Related Questions