Thread: Code to dump a classes objects into an array?

  1. #1
    Registered User
    Join Date
    May 2010
    Posts
    18

    Code to dump a classes objects into an array?

    Code:
    class Fruit
    {
    public:
    string name;
    int quantity;
    }apple = {"Apple", 5}, pear = {"Pear", 3}, peach = {"Peach", 2};
    
    Fruit list[100];
    Is there a way to store each Fruit object into list[100] without typing out "list[0] = apple, list[1] = pear....etc"?

    And, is there a way to use sizeof on a class, to count the number of objects it has? So I can make list[] hold the same number elements as Fruit has objects.

    I'm trying to code in a way so when I add more objects later down the road, I will only have to initialize them in their class. Rather than have to do that, and change array sizes, and values and all that.

    Much appreciated,

  2. #2
    Kiss the monkey. CodeMonkey's Avatar
    Join Date
    Sep 2001
    Posts
    937
    is there a way to store each Fruit object into list[100] without typing out "list[0] = apple, list[1] = pear....etc"?
    Not if each fruit is a distinct instance declared somewhere -- in that case you'd have to add them one-by-one. You could make it easier by adding them when your declare them, or by keeping a map<string,Fruit*> handy throughout the program.

    And, is there a way to use sizeof on a class, to count the number of objects it has? So I can make list[] hold the same number elements as Fruit has objects.
    Library containers like std::vector have size() and can be resized dynamically. Otherwise, you'd have to keep track with an int size, or better yet with your own custom class. Or use vector

    I'm trying to code in a way so when I add more objects later down the road, I will only have to initialize them in their class. Rather than have to do that, and change array sizes, and values and all that.
    You can add fruit to your array (let's say vector) anytime you like, and they'll be copied right in. If you mean that you want the fruit to add itself to the list, then you might find tricky constructor stuff heading your way (I say from ignorance). You could also
    Code:
    class fruit
    {
      //...
    };
    
    fruit apple;
    fruit pear;
    fruit TomCruise;
    
    fruit & SpawnFruit(const std::string & name, int quant, container & list = ThatOneList)
    {
       fruit temp_fruit;
       //initialize temp_fruit with name and quant;
       list.push_back(temp_fruit);
       return list.back();   //or list[most_recently_added] or whatever
    }
    
    int main()
    {
       fruit & Mango  = SpawnFruit("Mango",9001);
       //now ThatOneList (wherever it is) knows about our Mangos (over 9000 of them)
    }
    "If you tell the truth, you don't have to remember anything"
    -Mark Twain

  3. #3
    Registered User
    Join Date
    May 2010
    Posts
    18
    Thank you very much, this basically answered all of my questions. My Fruit list[100] is just a normal array. I didn't even think of actually using a list.


    I'm pretty inexperienced, so I don't understand how you are using the '&' sign in your code;I haven't used vectors or lists yet. push_back seems to do exactly what I'm looking for though, so I will learn more about them

    Thanks again.

  4. #4
    Kiss the monkey. CodeMonkey's Avatar
    Join Date
    Sep 2001
    Posts
    937
    The & indicates a reference. You can look it up on this website and have a full understanding of it in a minute. It's just an alias -- a way of having another name for something in scope, without having to declare and dereference a pointer.
    "If you tell the truth, you don't have to remember anything"
    -Mark Twain

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Array of classes?
    By dream_noir in forum C++ Programming
    Replies: 2
    Last Post: 03-22-2009, 11:43 AM
  2. Creating array of objects w/o default constructor
    By QuestionC in forum C++ Programming
    Replies: 19
    Last Post: 05-02-2007, 08:03 PM
  3. array of objects?
    By *~*~*~* in forum C++ Programming
    Replies: 4
    Last Post: 05-31-2003, 05:57 PM
  4. Replies: 5
    Last Post: 12-05-2002, 02:27 AM
  5. Help with an Array
    By omalleys in forum C Programming
    Replies: 1
    Last Post: 07-01-2002, 08:31 AM