while reading through vptr and vtable concept i got this wonderful piece of code, but i am not able to make out the concept involved here:
#include <iostream>
using namespace std;
class A
{
public:
virtual void foo(int x = 10)
{
cout << "base x : " << x << "\n";
}
virtual void bar()
{
cout << "base bar\n";
}
};
class B : public A
{
public:
virtual void foo(int x = 20)
{
cout << "derived x : " << x << "\n";
}
private:
virtual void bar()
{
cout << "derived bar\n";
}
};
class C : public B
{
};
int main()
{
A x; x.foo(); // x.foo(10);
B y; y.foo(); // x.foo(20);
A *p(&y);
p->foo();
}
now the output i get here is:
base x : 10
derived x : 20
derived x : 10
how is it possible that even when derived x(i.e B::foo()) is being printed then the default argument is that of base function(i.e A::foo())?