Virtual Function:
Before going through virtual function we need to understand
few of the basic things.
In inheritance, let us assume there is a Base class and a
Derived class. Now we create a pointer of base class in function main().
Considering this situation dive in for depth knowledge, read the following two
points:
1) Base
class pointer can store address of base class object or can store address of
Derived class object.
2) If
Base pointer contains address of Derived class object, we can access only those
members of Derived class using Base pointer which are inherited from Base
class.
Example:
#include<iostream.h>
#include<conio.h>
class Base
{
public:
void fun1()
{
cout<<”Base
Class, fun1()”;
}
void fun2()
{
cout<<”Base
Class, fun2()”;
}
};
class Derived : public Base
{
public:
void fun1()
{
cout<<”Derived
Class, fun1()”;
}
void fun2()
{
cout<<”Derived
Class, fun2()”;
}
};
void main()
{
clrscr();
Base *p;
p=
new Base;
p->fun1(); //Base class version is called
p->fun2(); //Base class version is
called
delete p;
p= new Derived;
p->fun1(); //Base class version is called
p->fun2(); //Base class version is
called
delete p;
getch();
}
If we call fun1() or fun2() using Object of Derived class
using dot (.) operator, Derived
class version of functions would be called. This is called function overriding
(see chapter 9)
We focus our discussion to access member using object
pointers only.
In the above example if we made base class version of
function virtual by prefixing a keyword virtual then Base pointer calls version
of function depends on address contained by Base pointer. If Base pointer
contains address of Base class object then function of Base class version would
be run and if Base pointer contains address of Derived class object then
function of Derived class version would run.
Example:
#include<iostream.h>
#include<conio.h>
class Base
{
public:
virtual void fun1()
{
cout<<”Base
Class, fun1()”;
}
void fun2()
{
cout<<”Base
Class, fun2()”;
}
};
class Derived : public Base
{
public:
void fun1()
{
cout<<”Derived
Class, fun1()”;
}
void fun2()
{
cout<<”Derived
Class, fun2()”;
}
};
void main()
{
clrscr();
Base *p;
p=
new Base;
p->fun1(); //Base class version is called
p->fun2(); //Base class version is
called
delete p;
p= new Derived;
p->fun1(); //Derived class version is called
p->fun2(); //Base class version is
called
delete p;
getch();
}
Explanation:
A
virtual function is always a member of a class.
A
function can be made virtual by using keyword virtual (see fun1() in Base
class)
It
usually has a different functionality in Derived class
A
function call is resolved at run time
See
in above example p->fun1() is written two times but they call different
versions, former is a call to Base class version of fun1() and later is call to
Derived class version.
Since both the calls look alike it is polymorphism.
It
is worth mentioning here that virtual function mechanism is valid for functions
sharing same prototype but different definitions, one in Base class and other
in Derived class.
If functions have same name but vary in arguments, virtual
keyword losses its effect.
If
Base class contain any virtual function, it is not mandatory to redefine in
Derived class.
No comments:
Post a Comment