Google

Difference between a Virtual function and Overloaded function in C++

Written on:May 28, 2013
Comments are closed




Difference between a Virtual function and Overloaded function in C++

C++ supports dynamic linking through virtual functions. Whenever both the base class and derived class have function with the same name, then the base class function always invokes the base class function irrespective of the fact that it is pointing to the derived class function. When a function is defined as virtual, it is determined at runtime, which function to use depending upon the type of object pointed to by the base class pointer. Thus, if a base class pointer points to different objects of different classes, by defining base member function as virtual, we can invoke different versions of different function.

Function Overloading is the phenomenon of defining different functions with the same name but different function signatures i.e. we can use same function name to perform a wide variety of tasks on different data types. The function to be invoked is determined by the number and type of arguments. Hence in function overloading the function prototypes differ and are not identical.

If the prototypes of the virtual function defined in the base class, and all other functions defined in derived classes is identical then they are considered as virtual functions. If the two functions with the same name have different prototype, then they are considered as overloaded functions and the virtual function phenomenon is ignored irrespective of the virtual function.

Sorry, the comment form is closed at this time.