What is
virtual Function ?
some time base class function and child class function has
same name.
it means function override.
then output type of outputs.
1.
static binding
2.
dynamic
binding
Static
binding:
Compiler time selected output is called static
binding .
The compiler selected the output according
the argument.
Dynamic
binding:
it is nice to
dynamically select at runtime the appropriate member
function from among
base- and derived-class functions. The keyword virtual,
a
function specifier
that provides such a mechanism, may be used only to modify member
function
declarations. The combination of virtual functions and
public inheritance is
our most general
and flexible way to build a piece of software. This is a form of pure
polymorphism.
Example:
#include<iostream>
#include<conio.h>
using namespace std;
class base
{
public:
virtual void print()
{
cout<<
"call base class function";
}
};
class child: public base
{
public:
void print()
{
cout << "call drived class function";
}
};
int main()
{
base b;
child c;
base * ptr=&b;
ptr->print();
ptr=&c;
ptr->print();
getch();
}