Simulating a shopping list of some sorts is what I'm trying to do.
I have a class called item that looks like this:
And yes I know about the using statement but I don't use it.Code:class item{ private: std::string itemName; std::string itemDesc; public: item(); ~item(); item(std::string newItemName, std::string newItemDesc); std::string getItemName(){return itemName;} std::string getItemDesc(){return itemDesc;} void setItemName(std::string newItemName){itemName = newItemName;} void setItemDesc(std::string newItemDesc){itemDesc = newItemDesc;} };
When creating an instance of the item class, I'll mostly be using the constructor with the 2 argument signature.
Now in my main function I want to declare a vector that will hold any instance of the item class I pass to it. Then I want to be able to access any object inside the vector so that I can get it's attributes. This is what I got so far:
This is where I'm lost at.Code:item milk("Milk", "A gallon of milk."); std::vector<item> shoppingCart;
I'm guessing that when I want to add an object to the vector, I use the push_back function passing the instance? And for accessing the elements of the vector, would I use an iterator?
Thanks in advance for any help. It's greatly needed.




Reply With Quote