Ad Hoc Polymorphism

Why Trust Techopedia

What Does Ad Hoc Polymorphism Mean?

Ad hoc polymorphism refers to polymorphic functions that can be applied to different argument types known by the same name in a programming language. Ad hoc polymorphism is also known as function overloading or operator overloading because a polymorphic function can represent a number of unique and potentially heterogeneous implementations depending on the type of argument it is applied to.

Advertisements

Techopedia Explains Ad Hoc Polymorphism

Ad hoc polymorphism defines operators that can be used for different argument types. It follows a dispatch mechanism in which the control moving from one named function is dispatched to several other functions without specifying the function being called. This function overloading permits multiple functions taking different argument types to be known by the same name as the compiler and interpreter calls the right 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 integers. In the expression x+y, it stands for the function that adds two floats. 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.

Advertisements

Related Terms

Margaret Rouse
Editor

Margaret jest nagradzaną technical writerką, nauczycielką i wykładowczynią. Jest znana z tego, że potrafi w prostych słowach pzybliżyć złożone pojęcia techniczne słuchaczom ze świata biznesu. Od dwudziestu lat jej definicje pojęć z dziedziny IT są publikowane przez Que w encyklopedii terminów technologicznych, a także cytowane w artykułach ukazujących się w New York Times, w magazynie Time, USA Today, ZDNet, a także w magazynach PC i Discovery. Margaret dołączyła do zespołu Techopedii w roku 2011. Margaret lubi pomagać znaleźć wspólny język specjalistom ze świata biznesu i IT. W swojej pracy, jak sama mówi, buduje mosty między tymi dwiema domenami, w ten…