What is ad hoc polymorphism?
Ad hoc polymorphism refers to polymorphic functions that can be applied to different types of arguments, known by the same name in a programming language. Ad hoc polymorphism is also known as function overload or operator overload because a polymorphic function can represent a number of unique and potentially heterogeneous implementations, depending on the type of argument it is applied to.
Ad hoc polymorphism defines operators that can be used for different types of arguments. What follows is a dispatch mechanism in which the control moving from a named function is sent to several other functions without specifying the called function. This overloading of functions allows multiple functions, with different argument types being known by the same name, as the compiler and the interpreter call the correct function.
For example in the following code:
int a, b;
float x, y;
printf ('% d% f', a + b, x + y);
The '+' symbol is used in two different ways. In the expression a + b, it stands for the function that adds two whole numbers. In the expression x + y, it stands for the function that adds two floating point numbers. Thus, ad hoc polymorphism refers to the use of a single function name to indicate two or more unique functions. The compiler decides which function to call depending on the type of arguments.
Ad hoc polymorphism is supported by almost all programming languages for built-in operations such as '+', '-', '*' etc.