Skip to content

Ad hoc polymorphism

MarJC5 edited this page Nov 15, 2022 · 1 revision

The Ad-Hoc polymorphism is called as overloading. This allows function with same name to act in different manner for different types. The function and the operator both can be overloaded.

#include <iostream>

int add(int a, int b) {
   return a + b;
}

string add(string a, string b) {
   return a + b; //concatenate
}

int main() {
   cout << "Addition of numbers: " << add(2, 7) << endl;
   cout << "Addition of Strings: " << add("hello", "World") << endl;
}

Output:

Addition of numbers: 9
Addition of Strings: helloWorld
Clone this wiki locally