Mutant vampire bunnies

Hello im working on the last beginner program on this list http://www.cplusplus.com/forum/articles/12974/ "Graduation"

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?

Thank you!
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.

Or is that what i need to use list for?
Last edited on
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.
So i am not too familiar with how to use lists.

i did the
1
2
3
4
5
6
7
8
list<Bunny> bunny_list ;
for(int=0; in<5; i++)
{
        //what exactly do i do here so i don't have to manually name each bunny object
        
        
}


Would it be something similar to arrays?


I found some of the list commands like bunny_list.push_back(Bunny)
Last edited on
It's not too different. Use the list's push_back() or push_front() to add a bunny to the list. So, like I said before:

Inside your loop, do three things:
1. Instantiate a bunny
2. Set its properties
3. Add it to the list (push methods)

This could be shortened based on how your program is set up, but try this first.
bunny_list.push_back(Bunny(/*param list*/));

then you can access the bunnies just like you access elements in an array. EDIT - forgot it was a list not a vector, lists can't be accessed by index

Have a look at this if you want to know more about lists
http://www.cplusplus.com/reference/stl/list/

EDIT - what I just posted does exactly what Zhuge just said, assuming that you have an appropriate Bunny object constructor
Last edited on
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?
Last edited on
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(unsigned int 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.
okay i am starting to uinderstand - but how would you access one of these objects in the list though? Like arrays? mylist[1]?
If you are using a list, that doesn't work as it is a linked list. You need to iterate through each of the elements:
1
2
3
4
// Increment all the ints in the list by 2
for(std::list<int>::iterator i = mylist.begin(); i != mylist.end(); ++i) {
    (*i) += 2;
}

Basically, iterators act as a sort of pointer to the data.
okay so i should i use vectors instead?
If you like. Personally I think a linked list is a suitable structure for the purposes of this program.
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() ;
Try looking at my loop that increments the elements of my list by 2, and see if you can see how you'd do it with your Bunny class.
Last edited on
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
Last edited on
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?


Yes, that is exactly right.
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:
 
iterator i = bunny_list.front() 


is that correct?
Last edited on
but i cant do it like that with the iterator :(


You actually can, it just has pointer syntax because the iterator is a separate class:

1
2
3
4
std::list<Bunny>::iterator i = whatever;

i.foo(); // calls iterator::foo
i->bar();  // calls Bunny::bar 


So the trick is to use the -> operator instead of the . operator
OMG TY! it worked how awsome!

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
Last edited on
Topic archived. No new replies allowed.