I have a base class which has virtual void function1( ) and that is overridden in derived class. Additionally there is one more virtual function in my derived class as below.
class Base
{
public:
virtual void function1()
{
cout<<"Base::Virtual function1"<<endl;
}
};
class Derived1:public Base
{
public:
void function1()
{
cout<<"Derived1::Function1"<<endl;
}
virtual void function2()
{
cout<<"Derived1::function2"<<endl;
}
};
int main()
{
Base *bptr = new Derived1();
Derived1 *dptr = new Derived2();
bptr->function2(); //compile time error
return 0;
}
I want to know what happens at compile time which is causing compile time error. I want an answer in an interview point of view. How does Vtable and Vptr behave in this scenario. I know there will be one vptr for Base class and that will be inherited to Derived1 class. What does compiler check at compile time?
Derived2
class – Cory Kramer