I am trying to figure out how to pass a group of classes with the same base class to a vector. I figured out how to pass them through their BASE class, but I can't get them to access the derived class functions.
This is hard to explain, the example below shows what I'm trying to do. What I want the output to be:
A dinosaur
A lizard
A dinosaur
What I'm actually getting as output:
A generic reptile
A generic reptile
A generic reptile
The topic you are talking about is polymorphism.
You want to invoke the correct function of the derived class from a vector of base-class elements.
The problem is, that your vector holds the Objects themselves in the vector.
When adding an element of a derived class the rest of the class is just cut-off so all your elements are actually reptiles then.
To use polymorphsim you have to use pointers and virtual functions, so to solve your problem you have to do these things:
1. use pointers to your objects.
2. declare the toString() function in reptile virtual
3. ???
4. profit.
To learn more about polymorphism you should look around somewhere else in the internet or in a book of your choise, it would be to much to go in depth here.