Posted on 24th September 2009One Response
Virtual Member Function- Definition, Use and Example

Virtual Member Function
Virtual, as the name implies, is something that exists in effect but not in reality. The concept of virtual function is the same as a function, but it does not really exist although it appears in needed places in a program. Virtual member functions are declared with the keyword virtual. They allow dynamic binding of member functions. Because all virtual functions must be member functions, virtual member functions are simply called virtual functions. A virtual function allows derived classes to replace the implementation provided by the base class. If the definition of a virtual function is replaced by a pure specifier in the declaration of the function, the function is said to be declared pure. A class that has at least one pure virtual function is called an abstract class.

Why we use Virtual Function
The most prominent reason why a C++ virtual function will be used is to have a different functionality in the derived class.
For example: a Make function in a class car may have to make a car with green color. A class called Four Wheeler, derived or inherited from car, may have to use a black background and 4 tires as wheels. For this scenario, the Make function for FourWheeler should now have a different functionality from the one at the class called car. This concept is called Virtual Function.

What is the Properties of Virtual Functions
Dynamic Binding Property
Virtual Functions are resolved during run-time or dynamic binding. Virtual functions are also simple member functions. The main difference between a non-virtual C++ member function and a virtual member function is in the way they are both resolved. A non-virtual C++ member function is resolved during compile time or static binding. Virtual Functions are resolved during run-time or dynamic binding

Virtual functions are member functions of a class.
Virtual functions are declared with the keyword virtual, detailed in an example below.
Virtual function takes a different functionality in the derived class.

Example of Virtual function in C++
This example assumes a base class named Window with a virtual member function named Create. The derived class name will be Command Button, with our over ridden function Create.
class Window // Base class for C++ virtual function example
{
public:
virtual void Create() // virtual function for C++ virtual function example
{
cout <<"Base class Window"< }
};
class CommandButton : public Window
{
public:
void Create()
{
cout<<"Derived class Command Button - Overridden C++ virtual function”< }
};
void main()
{
Window *x, *y;
x = new Window();
x->Create();
y = new CommandButton();
y->Create();
}

The output of the above program will be,
Base class Window, Derived class Command Button
If the function had not been declared virtual, then the base class function would have been called all the times. Because, the function address would have been statically bound during compile time. But now, as the function is declared virtual it is a candidate for run-time linking and the derived class function is being invoked.

Popularity: 1% [?]

Share and Enjoy:
  • Print
  • Digg
  • Sphinn
  • del.icio.us
  • Facebook
  • Mixx
  • Google Bookmarks
  • BarraPunto
  • Bitacoras.com
  • BlinkList
  • blogmarks
  • BlogMemes Fr
  • BlogMemes Sp
  • Blogosphere News
  • blogtercimlap
  • co.mments
  • connotea
  • Current
  • Design Float
  • Diigo
  • DotNetKicks
  • DZone
  • eKudos
  • email
Comments
comment by ztbgxpq
Posted on December 13, 2009 at 6:43 am

what is the exact use of virtual member function

Leave a Response