Creating objects in a loop

Im writing a program that predicts population rise and fall using a class Person. I want a loop that represents a year which calculates newborns and deaths. I need to be able to create a new object each "year" and delete objects that have died, im guessing i will need to allocate the objects dynamically so i can use the delete keyword on a person that has died. But how would i create a new object each turn that can last for more than one turn? then delete the object when it reaches its maximum age? Here is an example to hopefully make my question clealer

The constructor of the object assigns a random sex, color and name using rand().
1
2
3
4
//Loop runs
     //a baby is born! a new object is created(how?)
     //one of the population has passed the age limit, he has died!(delete the obejct)
//run while the population is not zero 


Thank you if you can help!
You want a to make a list - http://www.cplusplus.com/reference/stl/list/ (which requires you to #include <list>).

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
// Make a list
list<Person> people;

//add a new person:
while () { // whatever condition you might have
  Person temp;  // not dynamically allocated
   // construct as needed.
   people.push_back(temp);
}

// remove person
for (list<Person>::iterator iter = people.begin(); iter != people.end(); iter++){
  if (iter->age > lifeExpenctancy){ // iter is a pointer
    people.erase(iter);
    iter--; // erasing the iterator puts you at the next one, which you will skip otherwise
  }
}


Last edited on
use vectors:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
#include <vector>
#include <iostream>
using std::vector; using std::cout; using std::endl;

class person
{
    int _age;
public:
    person()      : _age(0) {}        // A person is born (age 0)
    void age()    { ++_age; }         // Ages people by 1 year
    bool isDead() { return _age>75; } // People die after 75 years. You could add variation to this number and create a probability of accidents.
};

int main()
{
    vector<person> population( 10, person() ); // create the initial population of 10 people.

    while( !population.empty() )               // Loop through the years
    {
        // Births
        int nb_births = rand()%10;             // Between 0 and 9 births per year
        for (int j = 0; j < nb_births; ++j)
            population.push_back( person() );  // Calls default constructor for person and adds it to the population

        // Age each person
        for (vector<person>::iterator it = population.begin(); it != population.end(); ++it)
            it->age();

        // Deaths
        for (vector<person>::iterator it = population.begin(); it != population.end(); ++it)
            if ( it->isDead() )
                population.erase(it--);

        std::cout << "there are " << population.size() << " people at the end of the year" << std::endl;
    }
}


Of course you couldshould put the age and death function in the same for loop, but I displayed like this for clarity.

If you want to get fancy, start by adding a few people to the population, then simulate births as a result of whoopee.

Edit: lists work too. Just replace the word "vector" with "list" everywhere in my code. It would probably be a little more efficient.
Last edited on
Thanks for the help guys! i plan on using the stl lists, then eventually learning linked lists without stl. Does that seem like a good plan of attack?
Yes. I like to work with available higher-level libraries when learning something new. Then when I want to dig deeper I'll start breaking apart the library to learn the methods used.

I used the same method when learning WIN32. I started with SFML to see what could be done, then I went through the source code and made changes, and now I don't bother with SFML anymore.
Topic archived. No new replies allowed.