Your code is actually seen by the compiler like this:
(The display()
method is not actually there, but the resolving works in similar way)
class A
{
public:
virtual void display(int i) { cout<< "Base::" << i << endl; }
void display() { display(5); }
};
class B : public A
{
public:
void display(int i) override { cout<< "Derived::" << i << endl; }
void display() { display(9); }
};
Now you should understand what happens. You are calling the non-virtual display()
which calls the virtual function. In more strict words: the default argument is resolved just like if the no-arg non-virtual method was there - by the type of the variable (not by the actual type of the object), but the code gets executed according to real object type, because it is virtual method:
int main()
{
A * a = new B(); // type of a is A* real type is B
a->display(); // calls A::display() which calls B::display(5)
A* aa = new A(); // type of aa is A* real type is A
aa->display(); // calls A::display() which calls A::display(5)
B* bb = new B(); // type of bb is B* real type is B
bb->display(); // calls B::display() which calls B::display(9)
}