|
-
Feb 1st, 2007, 04:48 AM
#1
Thread Starter
Hyperactive Member
A vector of objects
Simulating a shopping list of some sorts is what I'm trying to do.
I have a class called item that looks like this:
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;}
};
And yes I know about the using statement but I don't use it.
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:
Code:
item milk("Milk", "A gallon of milk.");
std::vector<item> shoppingCart;
This is where I'm lost at.
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.
-
Feb 1st, 2007, 05:03 AM
#2
Re: A vector of objects
Yes and yes. Or you can use indexing.
All the buzzt
 CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.
-
Feb 1st, 2007, 05:26 AM
#3
Thread Starter
Hyperactive Member
Re: A vector of objects
Okay well how would I go about accessing the public members of the class though the instance located in the vector? Like instance milk is stored in shoppingCart[0]. How would I access the getItemName class member from the instance?
-
Feb 1st, 2007, 05:35 AM
#4
Re: A vector of objects
shoppingCart[0].getItemName()
All the buzzt
 CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.
-
Feb 1st, 2007, 06:01 AM
#5
Thread Starter
Hyperactive Member
Re: A vector of objects
Which method would be better? Indexing or iteration?
-
Feb 1st, 2007, 08:20 AM
#6
Re: A vector of objects
For getting at all of them, I'd say iterators. For accessing a specific one, indexing.
All the buzzt
 CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|