hi,
I have doubt about virtual function behaviour in below code snippet. As per my understanding class derive2 didn't override the virtual function fun() , so it is obvious that its object can't be assigned to the Base class pointer *p and even individually we can't instanciate the object of derive2 (please see the commented section) unless and untill we override the fun function in derive2 class due to having abstract base class.
#include<iostream.h>
using namespace std;
class Base
{
public:
virtual void fun()=0;
};
class derive:public Base
{
public:
void fun()
{
cout<<"fun:derive";
}
};
class derive1:public Base
{
public:
void fun()
{
cout<<"fun:derive1";
}
};
class derive2 : public Base
{
public:
void show()
{
cout<<"show:derive2";
}
};
int main()
{
Base * p=new derive;
p->fun();
p= new derive1;
p->fun();
//derive2 *a = new derive2;
//a->show();
//derive2 a;
//a.show();
derive2 *a;
a->show();
return 1;
}
but my question is ,why still i am able to instanciate derive2 object as per below code.
derive2 *a;
a->show();