Function Overloading in C++

"Overloading is the reuse of the same function name or symbol for two or more distinct functions or operations".

Function overloading is the general concept of c++. A function can be declared more than once with different operations. This is called function overloading. In “C” language, the same function name is illegal to declare more than once. But c++ is benefited with this feature. It is the compiler job which one is the right to choose. If it makes sense to you then I should say that one function name for different operations have the advantage of good readability of a program.

To understand fully what it is meant by it, consider an example shown in program funcover.cpp that demonstrates the use of this type of function.

Function Overloading Program Example:

  1. //program name: funcover.cpp
  2. #include
  3. #include
  4. Void test (int x, int y);
  5. Void test (char ch);
  6. Int main()
  7. {
  8. Int n1 = 10;
  9. Int n2 = 30;
  10. Char ch = ‘*’;
  11. Test (n1,n2);
  12. Test (ch);
  13. Return (0)k;
  14. }
  15. Void test (int x, int y)
  16. {
  17. Cout
  18. }
  19. Void test (char ch)
  20. {
  21. For(int,i=1;i
  22. Cout
  23. }

Run Output

The sum of 10 and 30 is : 40

Function overloading means that the same function is defined more than once as long as the parameters or arguments they take are different or different amount of parameters. To be more specific, function names can be overloaded. In the program funcover.cpp, the function test is called twice, one with two parameters and the other with one parameter and in both cases the data types of the parameters are different. The different data types tell you that the parameter-list must differ at least in one of its argument type.

In the example program funcover.cpp, when the compiler sees a first call to the function test(n1,n2), it is his job to search which function it should use either void test(int x,int y) or void test(char ch). Let us consider next two examples which add two numbers. The first example funcover1.cpp adds two numbers with integer data type on one call and two numbers with float data type on the second call. The program will successfully compiler and run with an incorrect result with the second call. The program will successfully compile and run with an incorrect result with the second call. The result should be 30 and 30.8, which is not the case. The reason for the output 30.8 is that the float will cast to integer.

Program Example:

  1. //program name: funcover1.cpp
  2. #include
  3. void add(int x, int y);
  4. int main()
  5. {
  6. add(10,20);
  7. add(10.4,20.4);
  8. return(0);
  9. }
  10. void add 9int x, int y)
  11. {
  12. cout
  13. }

Run Output

30

30

The second example funcover2.cpp is written using the overloading concept, which offers a predictable result.

Program Example:

  1. //program name: funcover2.cpp
  2. #include
  3. #include
  4. void add(int x, int y);
  5. void add(double x, double y);
  6. int main()
  7. {
  8. clrscr();
  9. add(10,20);
  10. add(10.4,20.4);
  11. return(0);
  12. }
  13. void add(int x, int y)
  14. {
  15. cout
  16. }
  17. void add(double x,double y)
  18. {
  19. cout
  20. }

Run output

30

30.8

In the last example, a function is called with the appropriate parameters and the floating point addition is performed successfully. This shows the neatness that you can do in C++. Now let us consider typedef, which is a synonym for an existing type. Function that contains the same name and parameters cannot be overloaded.

  1. //program name: tdefover.cpp
  2. #include
  3. typedef float real;
  4. void functest (float f);
  5. void functest(real r);
  6. int main()
  7. {
  8. float f = 10.5;
  9. real r= 20.5;
  10. functest(f);
  11. functest(0);
  12. return(0);
  13. }
  14. void functest (float f)
  15. {
  16. cout
  17. }
  18. void functest(real r)
  19. {
  20. cout

There is a compilation error in the second function call. The compiler will prompt the user with the error message something like BODY HAS ALREADY BEEN DEFINED FOR FUNCTION functest (float f). This is a kind of redefining the same function with the same type of parameter. So care must be taken when dealing with a situation like this.