Results 1 to 6 of 6

Thread: A vector of objects

  1. #1

    Thread Starter
    Hyperactive Member GamerMax5's Avatar
    Join Date
    Nov 2004
    Location
    United States
    Posts
    388

    Question 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.

  2. #2
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594

    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.

  3. #3

    Thread Starter
    Hyperactive Member GamerMax5's Avatar
    Join Date
    Nov 2004
    Location
    United States
    Posts
    388

    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?

  4. #4
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594

    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.

  5. #5

    Thread Starter
    Hyperactive Member GamerMax5's Avatar
    Join Date
    Nov 2004
    Location
    United States
    Posts
    388

    Re: A vector of objects

    Which method would be better? Indexing or iteration?

  6. #6
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594

    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
  •  



Click Here to Expand Forum to Full Width