Function Overloading in C++
Function and Operator Overloading in C++ - Part 1
Forward: In this part of the series, I explain function overloading in C++.
By: Chrysanthus Date Published: 30 Aug 2012
Introduction
Meaning and Need for Function Overloading
More than one function defined or declared in the same scope with the same name but with different parameter lists (parameters in parentheses), is function overloading. Different parameter lists mean parameter lists with different object types, or same object types but with different numbers of parameters. Each parameter list has its own corresponding argument list for function call. When the function is called, the argument list determines the particular defined function that is executed. Overloaded function is necessary when you need similar tasks to be carried out. So, you just overload the function and give them the same name, in the same scope.
Illustration
In the following program there are 3 functions with the name add(). The first one has three parameters of int. The second one has two parameters of float. The third one has three parameters; note its types. They all have the same name, add.
#include <iostream>
using namespace std;
int add(int parA, int parB, int parC)
{
int answer = parA + parB + parC;
return answer;
}
float add(float parA, float parB)
{
float answer = parA + parB;
return answer;
}
float add(float parA, int parB, bool bl)
{
if (bl==true)
{
float answer = parA + parB;
return answer;
}
else
{
return 0;
}
}
int main()
{
int result1 = add(12,9,4);
float result2 = add(2.5,3.4);
float result3 = add(7.4,5,true);
cout << result1 << '\n';
cout << result2 << '\n';
cout << result3 << '\n';
return 0;
}
What determines the function to be called is the parameter list. The parameter list has to be different for each function. The calling functions have correspondingly, different arguments. The functions have to be in the same scope.
The following code shows the above function, but now in a class scope.
#include <iostream>
using namespace std;
class Addition
{
public:
int add(int parA, int parB, int parC)
{
int answer = parA + parB + parC;
return answer;
}
float add(float parA, float parB)
{
float answer = parA + parB;
return answer;
}
float add(float parA, int parB, bool bl)
{
if (bl==true)
{
float answer = parA + parB;
return answer;
}
else
{
return 0;
}
}
};
int main()
{
Addition summer;
int result1 = summer.add(12,9,4);
float result2 = summer.add(2.5,3.4);
float result3 = summer.add(7.4,5,true);
cout << result1 << '\n';
cout << result2 << '\n';
cout << result3 << '\n';
return 0;
}
The functions are now member functions (or methods).
A function definition begins with its interface and then the function body. If the interface is typed ending with a semicolon and without the function body (block), then it is called a function prototype or function declaration.
Overloaded functions have to be typed in the same scope. You can type overloaded function declarations, with the same name, in the same scope. In this case, overloading would still occur. The best place to type the declarations is in a namespace. Then you type the definitions in a different file or in a library. What causes overloading here, is the fact that the function declarations are in the same scope.
In the following simple illustrative code, the declarations (namespace) and the definitions are in the same file:
#include <iostream>
using namespace std;
namespace myName
{
int add(int parA, int parB, int parC);
float add(float parA, float parB);
float add(float parA, int parB, bool bl);
}
int myName::add(int parA, int parB, int parC)
{
int answer = parA + parB + parC;
return answer;
}
float myName::add(float parA, float parB)
{
float answer = parA + parB;
return answer;
}
float myName::add(float parA, int parB, bool bl)
{
if (bl==true)
{
float answer = parA + parB;
return answer;
}
else
{
return 0;
}
}
int main()
{
int result1 = myName::add(12,9,4);
float result2 = myName::add(2.5,3.4);
float result3 = myName::add(7.4,5,true);
cout << result1 << '\n';
cout << result2 << '\n';
cout << result3 << '\n';
return 0;
}
Note the use of the scope resolution operator, to access the function identifiers from outside the namespace.
A function signature is the parameter list (parameters in parentheses) of the function.
Return Type
The return type of a function does not determine which overloaded function will be called. What matters is the parameter list (function signature). The parameter list has to have different types and/or different number of parameters. The overloaded function may have the same or different return types. The return types are not important in determining which particular function is called.
That is it for this part of the series. We stop here and continue in the next part.
Chrys
Related Courses
Storage Duration in C++Scopes in C++
Function and Operator Overloading in C++
Specifiers in C++
Some Features of C++ Entities
C++ Preprocessing Directives
Writing a C++ Container
C++ Namespace
Writing a C++ Application
C++ Templates
Exception Handling in C++
Container Library Sequences in C++ Simplified
Associative Container in C++ Simplified
String in C++ Standard Library Simplified
Date and Time in C++ Simplified
C++ Course
Relational Database and Sybase
Windows User Interface
Computer Programmer – A Jack of all Trade – Poem
NEXT