Google

Virtual function – Difference between a Virtual function and an Overloaded function

Written on:May 8, 2013
Comments are closed




Virtual function

Polymorphism is the property that allows objects of different types to behave differently to the same message. In C++, polymorphism is achieved through virtual functions. Whenever we define a function with the same name in the Base class as well as in the Derived class, the base class pointer, irrespective of pointing to the desired class object, always call the base class function. If the function in the base class is defined as virtual then the function to be invoked is determined at runtime depending upon the case as to what object is being pointed to by the pointer of the base class. This is also known as runtime polymorphism or late binding or dynamic binding.




Virtual function vs. Overloaded function

A Virtual function is different from an Overloaded function. A Virtual function must have similar definitions everywhere i.e. the function prototype defined in the base class must be identical to that of the function defined in the desired class. But if two functions bear same name but different parameter list or different prototype, then they are considered as overloaded functions in place of virtual functions.

 

 

Sorry, the comment form is closed at this time.