class A
{
public:
virtual void Print()
{
cout<<"A"<<endl;
Print(1);
}
void Print(int n)
{
cout<<"A - "<<n<<endl;
}
};
class B: public A
{
public:
void Print()
{
cout<<"B"<<endl;
Print(1);
}
void Print(int n)
{
cout<<"B - "<<n<<endl;
}
};
class C:public B
{
public:
void Print()
{
cout<<"C"<<endl;
Print(1);
}
void Print(int n)
{
cout<<"C - "<<n<<endl;
}
};
int main()
{
A *p=new B;
p->Print();
((C*)p)->Print(9);
}
Above is my code. The result I got is :
B
B - 1
C - 9
My question is: why the object B
can call a function member of class C
(its child)? How can object B
contain code of class C
? I just think it will call class B
member function. So the result should be:
B
B - 1
B - 9
Please help me understand what is going on.