Hi ,
What is the need of virtual function in C++? Here is my program:
#include<iostream.h>
class parent
{
public:
virtual void display()
{
cout<<"Inside class parent"<<endl;
}
};
class child1 : public parent
{
public:
void display()
{
cout<<"Inside child1 "<<endl;
}
};
class child2 : public parent
{
public:
void display()
{
cout<<"Inside child2 "<<endl;
}
};
void main()
{
child1 c1;
child2 c2;
parent* ptr;
ptr = &c1;
ptr->display();
ptr = &c2;
ptr->display()
}
In this program, if I take out the keyword "virtual" ,the output will be:
Inside class parent
Inside class parent
But, by using c1,c2...I can access the child class method(ie display())...Then why we use the virtual function here??????
Anybody can explain me???
Thanks in advance
Kokila.



LinkBack URL
About LinkBacks



