Hi
I am very new to this.......
somebody can explain well the use of virtual member function....???
and
The difference between how virtual and non-virtual member functions are called???
thanks....
|
TechCake
|
|
| 8May2008,19:44 | #2 |
|
You can search on google for detail Or ask some point where you are unable to understand.
In short: In c++ two types of polymorphism One is Compile time : By function overloading,operator overloading and template also. and second is Run Time polymorphism: by Virtual function >>> By the use of virtual function, we can override the function ( b/w base and derived class) Code:
class base
{
public:
void Fun()
{
cout<<" I am from base"<<endl;
}
}
class derived: public Base
{
public:
void fun()
{
cout<<" I am from derived"<<endl;
}
}
int main()
{
Base *bptr;
Derived dObj;
bPtr=&dObj;
bPtr-> fun()
}
I am from Base Because It will never call to derived because No run time polymorphism support here. If you write virtual keyword before function then Code:
class base
{
public:
virtual void Fun()
{
cout<<" I am from base"<<endl;
}
}
class derived: public Base
{
public:
void fun()
{
cout<<" I am from derived"<<endl;
}
}
int main()
{
Base *bptr;
Derived dObj;
bPtr=&dObj;
bPtr-> fun()
}
I am from Derived Because It will call to derived because run time polymorphism support because of writing virtual. For detail you can see this thread. http://www.go4expert.com/showthread.php?t=8403 |
