3.4 FUNCTION OVERLOADING

Before C++, functions were identified by their name. To avoid ambiguity, no two functions were allowed to have same name. In C++, we can have two or more functions having same name. This is called function overloading. To resolve ambiguity, the number of parameters and their respective data types in the parameter list of each function must be different. The number of parameters and their respective data types is called the signature of the function3. So even if the names of the function are the same, it is by their signatures the functions are uniquely identified.

Consider a case that we have to define function add for adding integers as well as real numbers. We can write two different functions as follows:

int add(int ...

Get Object Oriented Programming with C++, Second Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.