I'm a first year of university student , I found myself stuck on a project and sadly I can't find help anywhere so I hope I can find it here.
in my project :
I have a class Person and two classes derived from it (player and trainer)
person has the attributes : name and date of birth. player has the attributes : name and date of birth and position. trainer has the attributes : name and date of birth and licence number.
each attribute has a getter and a setter method;
in the player class I added a method called (show info) that prints out a string containing each of his attributes and did the same for the class trainer .
I have another class called club , in which I have a vector of pointers to person ( I used it because I wanted to add both players and trainers as elements of my vector) which I'm able to do using (push.back)
I have a method called ( addPerson ) that has as a parameter a pointer to person objects , but I need to use it for player and trainer objects.
what I need to do is iterate over all the elements of my vector and call the method (show info) for each of the (players or trainers)
my problem is that I dont know how to specify which of the (show info) methods is to be used , (is it the one in the player class or the trainer class ?) or how to acces the getters of the position or the licence number attributes if I'm able to know that this player is in fact a player or a trainer .
I hope that I described everything well and that I could get some help . thanks for reading.
edit:
I apreciate your answers it really helped a lot , I think I somewhat understood the concept of virtual methods but when I came to apply it it just gives me a random error.
here's my code :
std::string Club::showInfoMembers(){
stringstream text;
std::vector<Person*>::const_iterator iter;
text << "--------------------" << endl;
for (iter = m_vMembres.begin(); iter != m_vMembres.end(); iter++){
text << iter->showinfo());
text << "--------------------" << endl;
}
return text.str();
}
I get the error :
request for member ‘showInfo’ in ‘*
iter.__gnu_cxx::__normal_iterator<hockey::Person**,
std::vector<hockey::Person*> >::operator->()’, which is of pointer type
‘hockey::Person*’ (maybe you meant to use ‘->’ ?)
I don't understand what it means.