Here is the code...
#include "stdafx.h"
#include<iostream>
using namespace std;
class Base
{
public:
virtual void Display(bool b = false)
{
cout<<"Base"<<"\t"<<b<<endl;
}
};
class Derived : public Base
{
public:
virtual void Display(bool b) override
{
cout<<"Derived"<<"\t"<<b<<endl;
}
};
int _tmain(int argc, _TCHAR* argv[])
{
Base* bp = new Base();
Derived* dp = new Derived();
bp->Display();
dp->Display(true);
bp = new Derived();
bp->Display();
cout<<"Done"<<endl;
return 0;
}
When the Display()
method called second time using bp
, surprisingly it hit the method in Derived
class. in Derived
class I didn't specify default argument. But it took the default base class argument. How?