I have a strange situation over the following code. Please help me to get it clarified.
class B
{
public:
B();
virtual void print(int data=10)
{
cout << endl << "B--data=" << data;
}
};
class D:public B
{
public:
D();
void print(int data=20)
{
cout << endl << "D--data=" << data;
}
};
int main()
{
B *bp = new D();
bp->print();
return 0;
}
Regarding the output I expected
[ D--data=20 ]
But in practical it is
[ D--data=10 ]
Please help. It may seem obvious for you but I am not aware of the internal mechanism.
Question&Answers:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…