IF you really know how virtual functions work,it is really fun. Here is such an example(for fun i wrote a function fun )
class base
{
public :
virtual void fun()
{
cout <"base fun";
}
~base(){}
};
class deriv: public base
{
void fun()
{
cout << "deriv fun";
}
~ deriv(){}
};
very simple two classes..
We know virtual funtions are initlaized in constructor and removed to their old base versions in the destructor.
Here is One more proof .
base *p = new deiv();
p->fun();p->~base();
p->fun();
the outputs will be first
deriv fun and
base fun
We can't call constructor explicity as we did with destructor.
Ok , I am stopping now.
No comments:
Post a Comment