Hi, everyone!
So from main(), V[i].display() first calls B::display() or C::display (depending on what an user types in main()). And then they call A::display(). And from there, I'm trying to go back to B::display() or C::display() (to whichever one that had called A::display())...... WITHOUT ADDING OR CHANGING ANYTHING IN MAIN().
The line 12, where I put //////////
is the line I don't know what to write in order to call back B::display() or C::display(). I know I can't just write B::display() or C::display() there because the base class doesn't know about the derived class (from what I was taught).
Then how can I possibly do that!?!?
Thanks so much in advance.... :)
p.s. Sorry if the code I wrote is too "noob" ...xD
> And from there, I'm trying to go back to B::display() or C::display()
> (to whichever one that had called A::display()). how can I possibly do that!?!?
You do not have to do anything to achieve that. There is an implicit return statement at the end of A::display(); return is a jump statement which unconditionally transfers control back to the caller of the function - if B::display called A::display, control goes back to B::display; if C::display called A::display, control goes back to C::display etc.
(When control reaches the end of a function with the return type void, return ; is automatically executed.)
ohhhh i seeeeeee!
Um sorry but can i ask you one more thing then..? If i were to get rid of the word "virtual" in class A, A::display() would get called first right? Is there anyway i can call either B::display() or C::display() from there directly?? Do i have to make class B (or C) object to do that?
In your code, the vector contains values of type 'A'; A::display() would be called for each object of type 'A' in the vector.
To get polymorphic behaviour, we need to operate on either values of type 'pointer to A' or variables of type 'reference to A'
Run this program, first as it is, observe the behaviour.
Then comment out the virtual specifier in A::display(), recompile and rerun,
and observe the difference in behaviour.