In my C++ program:
#include<iostream.h>
class A
{
public:
virtual void func()
{
cout<<"In A"<<endl;
}
};
class B:public A
{
public:
void func()
{
cout<<"In B"<<endl;
}
};
class C:public B
{
public:
void func()
{
cout<<"In C"<<endl;
}
};
int main()
{
B *ptr=new C;
ptr->func();
}
the statement should call B::func()
. However, the function, C::func()
is called. Please throw some light on this. Once the virtual keyword is removed in 'class A', this does not happen anymore.