user16538261
user16538261

Reputation:

creating objects and naming them at runtime in C++

Actually I'm making a budget calculating app, where there is a class for the items I buy.

class Item
{
    string name;
    string date;
    int amount;
    float singlePrice;
    float totalPrice;
    // constructor
    Public Item(string name, int amount, float price)
    {
        
    }
};

But I don't want to hard code every Item, I want to add items in the app and save it to file and calculate how much money I have left.

Upvotes: 1

Views: 102

Answers (1)

Tanveer Badar
Tanveer Badar

Reputation: 5512

You need to put them in an appropriate data structure which will let you retrieve them by name later on.

Put them in a std::map<string, Bought> if there's going to be just one instance of each, otherwise, in a std::map<string, std::vector<Bought>> if needed; although, your data model really should have a notion of quantity too somewhere - i.e., separate your item catalog from a purchase itself.

Upvotes: 1

Related Questions