I have constructed my bunny class - they get randomly chosen names and colors and sex. Now i am stuck about how to make them procreate. Do i create another function or class? And also how to hold all the bunnies i create? Do i have to create my own linked list data structure?
Every turn, you can iterate through your list and see if you find a male who meets the breeding requirements. If you find one, then loop through the list and create new bunnies for each of the females (who also meet the breeding requirements) you find. If would probably just place this in the main loop.
You can use one of the STL containers, like list or deque. Look them over and see which one you think is best for this situation.
okay thank you. So also i want to have a for loop at beginning creating 5 bunnies.
When i call my bunny class how do i loop this?
What i mean is after i call my bunny class like: Bunny bunny_object_name_here; is there any way to loop this type of thing like create bunny 1 - 5 with a single loop? I hope im not being too confusing.
You'll need to instantiate (that's the term for what you are doing) a bunny, set up its properties, then add it to your list. Do all of that in your initial for loop to create the first five.
Okay and how do i Instantiate my class objects? The basic way i know about creating an object is you have to write the objects name yourself. like bunny1, bunny2, bunny3 - But is there not some way i could just do bunny(i) so that it automate the name creation process? Otherwise i dont see why i would need a loop at all.
EDIT: Ah now wait a second are you guys saying i dont need to create a name at all?
It doesn't matter what you name the object. You could even have it nameless as quirkyusername did above if you have good constructor. Since the object is destroyed and recreated each iteration of the loop, the name is irrelevant.
1 2 3 4 5 6
std::list<int> mylist;
for(unsignedint i = 0; i < 20; ++i) {
int blah = 0;
//...do complex calculations to get blah based on w/e
mylist.push_back(blah);
}
This will add 20 ints to the list. The name of the variable is unimportant, and not used when accessing elements of the list.
i mean id love to. I got the list working and created within the loop 5 bunnies. now (For example in my class i wrote a Details function which prints to the screen the bunnies name,sex,age, and fur color among other things. So i want to use this function on one of the Bunnies in my list just to start things off. That should be simple but i'm not sure how i would do it. typically it would be just: bunny1.Details() ;
I am trying this again and i am abit confused about your scoping. I havent really seen iterators either before (These things are never explained in the tutorials - only in the defenitions :( )
So i have to call iterators like this? list<Bunny>::iterator i ;
and i use this iterator to point to a object in the list and then can do stuff with said object?
I hope i dont have to use pointers TOO much here though because although ive went over them numerous times they leave me very confuzzled
An iterator is just a way to step through a sequence of data.
Here, you have a list of bunnies. Your iterator 'i' represents one bunny in that list. By incrementing the iterator, you move it to the next bunny in the list, and so on until you've stepped over all the bunnies.
So i have to call iterators like this? list<Bunny>::iterator i ;
That's how you define them, yes (although, don't say "call". the only things you "call" in C++ are functions)
and i use this iterator to point to a object in the list and then can do stuff with said object?
So then how do i use my class functions with this iterator thats what i have been confused about the whole time. If i just create a object of class bunny on my own its easy
1 2 3 4
Bunny test_bunny ;
test_bunny.DoStuff();
but i cant do it like that with the iterator :(
1 2
i.DoStuff();
EDIT: I understand i can point the iterator something like:
Now where should i go to learn more about this? This is all pointer stuff right? Or this is just specific to list iterator class?
EDIT: Ah okay i understand the importance now.. since its a class you might want to use some of the important class functions from there along with your own ones.. Hey thank you very much guys