Functions Overloading in c++ with example code
Functions Overloading:
Functions Overloading-Declaring more than one function with the same name but with a different set of arguments and return data types is called function overloading.
For example, three functions with the same name “sum” and having different parameters are declared as:
Int sum (int, int);
Int sum(int, int, int);
Double sum(double, double, double );
The first “sum” has two parameter s both of int type and returned data type is also int type.
The second function “sum” has three parameters all of the int data types the return data type is also of int type.
The third function “sum” has three parameters, all of the double type and its return data type is also double.
When the program that has an overloaded function is compiled, the C++ compiler checks the number of parameters, their order, and data type and marks a proper function name for each function. At function call, it executes that function whose return type or parameters matched with the return type or parameter given in the function call.
Amazon Purchase Links:
*Please Note: These are affiliate links. I may make a commission if you buy the components through these links. I would appreciate your support in this way!
Example how to use the functions overloading concept in c++ programming:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
#include <iostream> using namespace std; int main() { int sum(int, int); int sum(int,int,int); double sum(double, double,double); cout<<"sum of 3 integer value ="<<sum(4,5,6)<<endl; cout<<"sum of 3 float value ="<<sum(4.4,5.5,6.8)<<endl; cout<<"sum of 2 integer value ="<<sum(4,6)<<endl; cout<<"ok"; return 0; } int sum(int x, int y) { return x+y; } int sum(int x, int y, int z) { return x+y+z; } double sum(double x, double y, double z) { return x+y+z; } |
Output
1 2 3 4 |
sum of 3 integer value =15 sum of 3 float value =16.7 sum of 2 integer value =10 ok |
In the above functions overloading program, three functions are defined with the same name “sum”. When the function “sum” is called by passing three integer values parameters, the control will shift to the 2nd function that has three integer type arguments. Similarly, when the function is called by passing three float data type values, control shifts to the 3rd function that matches the parameters.
Consider the following Functions overloading example
For example, doTask() and doTask(object O) are overloaded functions. To call the latter, an object must be passed as a parameter, whereas the former does not require a parameter, and is called with an empty parameter field. A common error would be to assign a default value to the object in the second function, which would result in an ambiguous call error, as the compiler wouldn’t know which of the two methods to use.
Another example is a Print(object O) function that executes different actions based on whether it’s printing text or photos. The two different functions may be overloaded as Print(text_object T); Print(image_object P). If we write the overloaded print functions for all objects our program will “print”, we never have to worry about the type of the object, and the correct function call again, the call is always: Print(something)
Rules in Functions Overloading
- The same function name is used for more than one function definition
- The functions must differ either by the arity or types of their parameters
It is a classification of static polymorphism in which a function call is resolved using some “best match” algorithm, where the particular function to call is resolved by finding the best match of the formal parameter types with the actual parameter types. The details of this algorithm vary from language to language.
Functions overloading is usually associated with statically-typed programming languages that enforce type checking in function calls. An overloaded function is really just a set of different functions that happen to have the same name. The determination of which functions to use for a particular call is resolved at compile time.
In Java, functions overloading is also known as compile-time polymorphism and static polymorphism.
Functions overloading should not be confused with forms of polymorphism where the choice is made at runtime, e.g. through virtual functions, instead of statically.