More on Virtual Functions in C++

This tutorial will actually be a continuation of the topics covered in the last tutorial of Virtual Functions but this will be a fuller explanation of what virtual functions are and how they can be used in a program. We will present a simple database program with a virtual function to show how it can be used, then we will go on to illustrate a more complex use of the virtual function in a manner that finally illustrates its utility and reason for existence.

So what is a Virtual Function?

A Virtual function is a function which is declared in base class using the keyword virtual. We write the body of virtual function in the derived classes. Its purpose is to tell the compiler that what function we would like to call on the basis of the object of derived class. C++ determines which function to call at run time on the type of object pointer to.

Declaration of a virtual function

3 Comments

  1. Hi everybody! I am not satisfied with the explanations in this webpage about the “virtual Functions”.
    Even if i dont use this virtual keyword also i am getting the same output.. the…. why should i Use it ???????..
    what hell is this of making use of virtual functions?….plz send answer any body.. please…..

    • HI
      here is just an illustration about virtual functions but it is not avery strong example .
      Virtual functions are very useful in Inheritance as it enables you to write adifferent implementation (bodies) in the child classes without suffering from the parent’s implementation _it is akind of interface in java_

      ——– Original Message ——–
      Hi everybody! I am not satisfied with the explanations in this webpage about the “virtual Functions”.
      Even if i dont use this virtual keyword also i am getting the same output.. the…. why should i Use it ???????..
      what hell is this of making use of virtual functions?….plz send answer any body.. please…..

Leave a Reply