int main()
{
srand(static_cast<unsignedint>(time(0))); // set initial seed value to system clock
rand(); // If using Visual Studio, discard first random value
std::vector<Bunny> package;
package.reserve(1000); // pre-allocate memory in one go
int count = 0;
int amount = 0;
for (int i = 0; i < 5; i++)
{
Bunny bunny = BunnyGenerator::generateBunny();
package.push_back(bunny);
}
while (package.size() < 40)
{
// Increase the age of every single bunny in the farm
for (unsignedint i = 0; i < package.size(); i++)
{
package[i].increaseAge();
if (package[i].getAge() > 9)
{
package.erase(package.begin() + i);
}
}
// Create as many new rabits as there are females atleast year 2
for (unsignedint i = 0; i < package.size(); i++)
{
if (package[i].getSex() == "Female" && package[i].getAge() > 1)
{
Bunny bunny = BunnyGenerator::generateBunny();
bunny.m_color = package[i].m_color;
package.push_back(bunny);
}
}
}
for (vector<Bunny>::iterator it = package.begin(); it != package.end(); it++)
cout << *it << endl;
return 0;
}
I made m.color public, however, i know the dangers of that so i would love to hear from you if you can think of any better solution
> Is there a way for an object of the same class to inherit exactly one attribute.
¿what are you talking about? objects don't inherit, classes do (from another class)
ne555 >>> English isn't my native language so i didn't know the right word for that. Sorry if i was misleading. I meant to say that i needed one object to get the same value form another well inherit is probably still suitable word. A rabbit father has to give another rabbit kid object his color, that's what i meant by saying inherit.
If you want to inherit, make all attributes and methods you want to inherit in the base class protected, and the rest are private. To make the protected attributes and methods inherited from base class visible outside the derived class, create getter and setter functions.
> A rabbit father has to give another rabbit kid object his color
Then the `generateBunny()' function should receive as parameter the parents of the rabbit.
BunnyGenerator::generateBunny(package[i]);
only that function would need access to m_color, and perhaps only read access
> rand(); // If using Visual Studio, discard first random value
¿what for?