#include<iostream>
class a
{
public:
virtualvoid add()
{ std::cout << "in A"; }
void fn()
{ add(); }
};
class b: public a
{
public:
virtualvoid add() override
{ std::cout << "in b"; }
};
int main()
{
b d;
d.fn();
}
because fn is told to call a::add and not b::add. Also it is not told to try to see overridden function in derived classes (what virtual do).
Remember: you might think that both a and b contains function with the same name, but thanks to name mangling those functions have completely different names.
something like
1 2
b d;
d.fn();
is compiled into something like
1 2
b d;
__a__fn(d);
And said function is like
1 2 3 4
__a__fn(b& x)
{
__a__add(x);
}
virtual keyword changes how function is called to something like
1 2 3 4
__a__fn(b& x)
{
x.__vtable[__add](x);
}
using saved information from virtual table to select correct function to call