| HTML Tutorials |
|
|
| XML Tutorials |
|
|
| Browser Scripting |
|
|
| Server Scripting |
|
|
| .NET (dotnet) |
|
|
| Multimedia |
|
|
| Web Building |
|
|
| Java Tutorials |
|
|
| Programming Langauges |
|
|
| Soft Skills |
|
|
|
 |
 |
|
Virtual Function
|
|
Virtual function is a function which is a member of a class, the functionality of
which can be over-ridden in the derived classes. It is declared as a virtual in the
base class using the keyword virtual. Virtual nature is been inherited in the
subsequent derived classes and there is no need to re-state virtual keyword.
Whole function body can be replaced by the new set of implementation in a
derived class.
The code given below shows how virtual function in C++ can be used
to achieve the dynamic or the runtime polymorphism.
|
#include <iostream.h>
class base
{
public:
virtual void display()
{
cout<<”\nBase”;
}
};
class derived : public base
{
public:
void display()
{
cout<<”\nDerived”;
}
};
void main()
{
base *ptr = new derived();
ptr->display();
}
|
|
|
In the example above, pointer is of the type base but it points to derived
class object. A display() method is virtual in the nature. Therefore to resolve
a virtual method call, context of a pointer is been considered, Which means that a
display method of derived class is been called and not the base class. If a method was
non virtual in nature, a display() method of base class might have been called up.
|
Share And Enjoy:These icons link to social bookmarking sites where readers can share and discover new web pages.
Keywords Virtual Functions,virtual visual, arrays functions, operator functions, virtual array,
virtual interface, public functions, virtual tutorial, methods functions, override functions,
virtual dynamic, virtual public, type functions, passing functions, variable functions,
string functions, calling functions, dll functions, functions code, functions variables,
virtual base, call functions, object functions, functions tutorial, using functions,
virtual call, library functions, functions example
|
|
| HTML Quizes |
|
|
| XML Quizes |
|
|
| Browser Scripting Quizes |
|
|
| Server Scripting Quizes |
|
|
| .NET (dotnet) Quizes |
|
|
| Multimedia Quizes |
|
|
| Web Building Quizes |
|
|
| Java Quizes |
|
|
| Programming Langauges Quizes |
|
|
| Soft Skills Quizes |
|
|
|