VOOZH about

URL: https://www.geeksforgeeks.org/dsa/ad-hoc-inclusion-parametric-coercion-polymorphisms/

⇱ Ad-hoc, Inclusion, Parametric & Coercion Polymorphisms - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Ad-hoc, Inclusion, Parametric & Coercion Polymorphisms

Last Updated : 28 Feb, 2024

When we talk about Polymorphism in C++, we come to hear the following four types: 👁 Image
Discussing these in details:

  1. Ad-hoc Polymorphism, also called as OverloadingAd-hoc Polymorphism allows functions having same name to act differently for different types. For example: The + operator adds two integers and concatenates two strings. 👁 Image
    Above example could be better illustrated by invoking the function "sum()" in under-mentioned code: 

Output
70 :- Integer addition Output
Polymorphism achieved :- String Concatenation Output
  1. Hence, by calling two different functions(which differ in the type of arguments) having the same names, to execute multiple operations, we have successfully achieved Ad-hoc Polymorphism.
  2. Inclusion Polymorphism, also called as SubtypingInclusion Polymorphism is the ability to use derived classes through base class pointers and references. It is also known as Run-time polymorphism because the address of the function is not located by the Compiler at compile-time, rather, the right pointer from the virtual table is dereferenced to invoke the function at run-time. The concept of Virtual Function, also known as Dynamic Linkage, is employed to achieve Inclusion Polymorphism. The usage of Virtual Function allows the selection of that function which is to be invoked based on the kind of object for which it is called. For example: To implement such a Polymorphism technique, let us take different files under consideration such as .jpg, .gif, .png files. All these files fall under the category of Image Files. 👁 Image
    So, they can be represented as Classes Derived from Image Base Class and overriding the display() pure virtual function. Above example could be better understood by the following illustration: 

Output
JPG Image File
PNG Image File
  1. Hence, in the above code, we have two different Classes with a function having the same name and not differing by Parameters, but with different implementations.
  2. Coercion Polymorphism, also called as CastingCoersion Polymorphism occurs when an object or primitive is cast into some other type. It could be either Implicit or Explicit. Implicit casting happens as a responsibility of Compiler itself. For example: float f=100 (integer implicitly gets promoted to float) Explicit casting makes use of some type-casting expressions such as const_cast, dynamic_cast, etc. For example: When a class defines conversion operator for some type, say "int", then, it could be employed anywhere in the program where integer type data is expected. Illustration Below could make it more easier to understand: 👁 Image
    This above illustration could be more clarified with help of code below: 

Output
746
100
  1. The IntClass reference is used in place of integer type argument, and hence, the concept of Casting is well understood.
  2. Parametric Polymorphism, also called as Early BindingParametric Polymorphism opens a way to use the same piece of code for different types. It is implemented by the use of Templates. For example: To develop an understanding of this sort of polymorphism, let us execute a program for finding greater of two Integers or two Strings, 

Output
55
Early
  1. Using Templates, the same function can be parameterized with different types of data, but this needs to be decided at compile-time itself, and hence, this polymorphism is named so. If we wish to achieve such polymorphism for pointers, it turns into Ad-hoc Polymorphism.
Comment