What is a virtual function? Why do we need virtual function? When we make virtual function “Pure”. OR how can one achieve run time polymorphism in C++? Explain with suitable example.

Virtual function: – Polymorphism in biology means ability of an organism to assume a variety of forms. In C++, it indicates the form of a member function that can be changed at run time. Such member function are called virtual member function and the corresponding class is called polymorphic class. The object of the polymorphic class, addressed by pointer, change at run time and respond differently for the same message. Such a mechanism requires postponement of binding of a function call to the member function until run time.

When object of different class in a class hierarchy, react to the same message in their own unique ways, they are said to be exhibit polymorphic behavior. The C++ provide a solution to invoke the exact version of the member function, which has to be indicated at run time using virtual function. They are the means by which function of the base class can be overridden by function of derived class. The keyword virtual provides a mechanism for defining the base class member function, the keyword virtual is used with those function, which are to be bound dynamically. The syntax of defining virtual function is-

Class <Class name>
                _ _ _ _ _
                _ _ _ _ _
                Virtual <Return Type> <Function Name>(Argument)
                                Body of Virtual Function
                _ _ _ _ _

Virtual function should be define in the public section of a class to realize its full potential benefits. When such a declaration is made, it allows to decide which function to be use at run time, based on the time of object, pointed to by the base pointed, rather than the type of the pointer.

Pure Virtual function: – Virtual function define inside the base class normally serve as a frame work for future design of the hierarchy, these function can be overridden by the method in the derived classes. In most of the cases, these virtual function are defined with null body, it has no definition. Such function in the base class are similar to do-nothing or dummy function and in C++ they are called pure virtual function. Pure virtual function declared as a virtual function with its declaration followed by =0.

A pure virtual function declared in a base class has no implementation as for as the base class is conserved. The classes derived from a base class having a pure virtual function have to define such a function or re-declare it as a pure virtual function. It must be noted that, a class containing pure virtual function cannot be used to define any objects of its own and hence such classes are called pure abstract class or simply abstract class. Whereas all other classes without pure virtual function and which are initiated are called concrete class. A pure virtual function is an undefined place holder that the derived class is accepted to complete. The following are the properties of pure virtual function-

  1. A pure virtual function has no implementation in the base class hence, a class with pure virtual function cannot be initiated.
  2. It acts as an empty bucket that the derived class is supposed to fill.
  3. A pure virtual member function cannot be invoked by its derived class.

Syntax of the pure virtual function is-

 pure virtual function

Needs for virtual function: – The following rule hold good with respect to virtual function.

  1. When a virtual function in a base class is a created, there must be definition of a virtual function in the base class even if base class version of the function is never actually called. However pure virtual function are exception.
  2. They cannot be static member.
  3. They can be friend function to another class.
  4. They are accessed using object pointer.
  5. A base pointer can serve as a pointer to derived object since it is type compatible where as a derived object pointer variable to base object.
  6. Its prototype in a base class and derived class must be identical for the virtual function to work properly.
  7. The class cannot have virtual constructor, but can content virtual destructor. In fact, virtual destructor are essential to the solution of some problem. It is also possible to have virtual operator overloading.
  8. More importantly to realize the potential benefit of virtual function supporting run time polymorphism, they should be declared in the public section of class.

Comments are closed.

Create a website or blog at WordPress.com

Up ↑

%d bloggers like this: