Hi, I'm having trouble calling the right method when looping in my vector. It should call the most specialized method, but it's only calling the parent class one. (Sorry if this is a little code heavy, but I want to show up everything that is related to these classes)
Basically, this is my testing unit and the fonctions / methods i'm having trouble with are theses ones :
code removed
I have the method reqReferenceFormate (Which is a getter method for formatted attributes of the class) implemented in the parent class (Reference), and in the 2 daughter classes (Journal and Ouvrage). `
So when looping in my vector that holds these instances of the daughter classes, I would expect for the getter to call the one of the corresponding daughter class, but it's always calling the parent's one. It's probably a beginner error since it's my first time having to work with vectors and class inheritance in C++ (I started programming with python).
This is school assignment, so please don't spoil direct answers if you can, that would be appreciated for my learning, as I like to fix my code by myself !
And sorry for the wall of text, and if you think you can help me and you need more of my code, please leave a message and I'll try to follow this every hour or so while searching up. Thanks alot in advance !
I believe you've lied to us. The only place a method needs to be declared virtual is in the base class, and it is clearly not declared virtual in your last code snippet.
cire, the last code shows Bibliographie::reqBibliographieFormate(). The function that should be declared virtual is Reference::reqReferenceFormate(). He says it's virtual so I see no reason why we should doubt that (except that it's hard to think of another explanation).
Viiarge, make sure you have spelled the function names correctly.
If you mark the functions with override when overriding the functions, the compiler will give you an error if you fail to override a function in the base class, which could easily happen if you misspell the function name or forget to use virtual.
So then. in another class (Bibliography), there is a private attribute that is a vector, I 'for loop' into it with the getter method 'reqBibliographieFormat'. There is my problem :
it's always calling the method of the parent class, and I'm expecting for it to call the 'Ouvrage' method when the object is a 'Ouvrage' instance, and same for the 'Journal' class.