OOAD Functions Q/A #2


Question:What is virtual function? Why it is required? What are the basic rules to create a virtual function? write down a program in C++ for virtual function.

Answer:

virtual function

A virtual function is a member function of a class, whose functionality can be overridden in its derived classes. It is one that is declared as virtual in the base class using the virtual key word. The function body of base class can be completely replaced with a new set of implementation in the derived class. Virtual , as the name implies , is something that exists in effects but not in reality . The object oriented programming language C++ implements the concept of virtual function as a simple member function , like all member functions of the class. Virtual function is a mechanism to implement the concept of polymorphism i.e. the ability to give different meanings to one function.

Declaration of virtual function

Virtual functions are member functions declared with the keyword virtual.

classname   // base class of C++ virtual function 
{
 public:
   virtual void memberfuctionname ( )// This denotes the C++  virtual function
{

}
};

Properties of virtual function

  • Dynamic binding: virtual functions are resolved during run time or dynamic binding. 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 and hence it is a static binding. Virtual functions are resolved during run time resolved during run time hence it is a dynamic binding.

  • Virtual functions are member functions of a class.

  • Virtual functions are declared with the keyword virtual.

  • Virtual function takes a different functionality in the derived class.

Need for virtual function

Virtual function is needed to implement different functionalities in different derived classes of a base class. For Exaple, make( ) function in a class vehicle may have to make a vehicle with red colour. A class called fourwheeler, derived or inherited from class vehicle , may have to use a blue background and 4 tyres as wheels. For this the make( ) function for fourwheeler class should now have a different functionality from the one defined at the class called vehicle. This concept is called virtual function.

The virtual functions can be inherited. when a virtual function is inherited, its virtual nature is also inherited. This means that when a derived class that has inherited a virtual function is itself used as a base class for another derived class ,the virtual function can still be overridden. One can inherited a virtual function any number of times.

Program to explain virtual function

#include <iostream>
using namespace std;

class vehicle// base class of C++ virtual function
{
   public:
      virtual void make ( )//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;
     }
};
int main ( )
{
   vehicle *a, *b;
   a = new vehicle ( );
   a->make ( ) ;
   b= new fourwheeler ( );
   b->make ( );
}

Output

member function of base  class vehicle accessed
virtual member function of derived  class fourwheeler accessed

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 the derived class, the new implementation for virtual function make ( ) is created. Here 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. 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 the pointers to the functions from each of the ojects of the derived class. This is used by the compiler whenever a virtual function is accessed.

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements