Free Training


C Language  |  CSS  |  MainFrame  |  VBScript  |  PHP  |  XML  |  C++ Tutorials  |  Ajax  |  JavaScript  |  CSS3  |  UML  |  jQuery  |  Microsoft AJAX

C++ Tutorials

 
Home Tutorials C++
 

C++ Virtual Functions

 

C++ Virtual Functions

What are Virtual Functions?

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. The object-oriented programming language C++ implements the concept of virtual function as a simple member function, like all member functions of the class.



The functionality of virtual functions can be over-ridden in its derived classes. The programmer must pay attention not to confuse this concept with function overloading. Function overloading is a different concept and will be explained in later sections of this tutorial. Virtual function is a mechanism to implement the concept of polymorphism (the ability to give different meanings to one function).


Need for Virtual Function:

The vital reason for having a virtual function is to implement a different functionality in the derived class.


For example: a Make function in a class Vehicle may have to make a Vehicle with red color. A class called FourWheeler, derived or inherited from Vehicle, may have to use a blue 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 Vehicle. This concept is called Virtual Function.


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.

Declaration of Virtual Function:

Virtual functions are member functions declared with the keyword virtual.


For example, the general syntax to declare a Virtual Function uses:



class classname //This denotes the base class of C++ virtual function
{
public:
virtual void memberfunctionname()
//This denotes the C++ virtual function
{
.............
............
}
};



Referring back to the Vehicle example, the declaration of Virtual function would take the shape below:



class Vehicle //This denotes the base class of C++ virtual function
{
public:
virtual void Make()
//This denotes the C++ virtual function
{
cout <<"Member function of Base Class Vehicle Accessed"<<endl;
}
};



After the virtual function is declared, the derived class is defined. In this derived class, the new definition of the virtual function takes place.


When the class FourWheeler is derived or inherited from Vehicle and defined by the virtual function in the class FourWheeler, it is written as:



 


class Vehicle   //This denotes the base class of C++ virtual function
{
public:
virtual void Make()   //This denotes the C++ virtual function
{
cout <<"Member function of Base Class Vehicle Accessed"<<endl;
}
};


class FourWheeler : public Vehicle
{
public:
void Make()
{
cout<<"Virtual Member function of Derived class FourWheeler Accessed"<<endl;
}
};


void main()
{
Vehicle *a, *b;
a = new Vehicle();
a->Make();
b = new FourWheeler();
b->Make();
}



In the above example, it is evidenced that after declaring the member functions Make() as virtual inside the base class Vehicle, class FourWheeler is derived from the base class Vehicle. In this derived class, the new implementation for virtual function Make() is placed.


The programmer might be surprised to see the function call differs and the output is then printed as above. If the member function has not been declared as virtual, the base class member function is always called because linking takes place during compile time and is therefore static.



In this example, the member function is declared virtual and the address is bounded only during run time, making it dynamic binding and thus the derived class member function is called.


To achieve the concept of dynamic binding in C++, the compiler creates a v-table each time a virtual function is declared. This v-table contains classes and pointers to the functions from each of the objects of the derived class. This is used by the compiler whenever a virtual function is needed.



Read Next: C++ Pure Virtual Function and Base Class



 

 

Comments


Raghunath said:

  Very Nice informatoin
June 10, 2007, 9:02 pm

syed firoj basha said:

  very goog idea. such a nice infomation
January 10, 2008, 11:54 pm

sanyukta G said:

  the best piece of information i cud ever get.......
too gud!!!!!!!!!

January 26, 2008, 10:36 pm

diptesh mohan said:

  program equipped with output would have been better
April 15, 2008, 4:21 am

said:

  The compiler creates a v-table for each *class* with virtual functions and one entry in that table for each virtual function, not a v-table for each virtual function.
June 28, 2008, 9:51 am

Srivant said:

  Nice Explanation with example.
August 13, 2008, 3:10 am

tom said:

  giv an eg without pointer.
November 15, 2008, 4:08 am

anup said:

  here is the best definition of virtual class that is benefecial for beginers.
November 21, 2008, 1:35 am

Post Your Comment:

Members Please Login
Your Name:*
e-mail ID:(required for notification)*
Image Verification: 
 
 Subscribe    

Weekly Offers

Sponsored Links