C++ Tutorial
C++ Function Overloading
Same name, different parameter lists. The compiler picks the match from the arguments you pass.
One name, several versions
Overloading means you declare more than one function with the same name. Each version must take a different list of parameters: a different type, a different number of arguments, or both. When you call the name, the compiler looks at the arguments and picks the matching version.
That is useful when the work is the same idea — add two values, print a value, compute an area — but the inputs are not the same type. You keep one name in your head instead of addInt andaddDouble.
Different types
These two functions are both named add. One takes two int values. The other takes two double values. The call add(2, 3) matches the first. The calladd(1.5, 2.5) matches the second.
Example
#include <iostream>
using namespace std;
int add(int a, int b) {
return a + b;
}
double add(double a, double b) {
return a + b;
}
int main() {
cout << add(2, 3) << endl;
cout << add(1.5, 2.5) << endl;
return 0;
}Click Try it in C++ under the example. That opens /cpp/try. Change the numbers and compile again.
Different number of parameters
You can also overload by count. area with one argument is a square. area with two arguments is a rectangle. The compiler does not guess; it counts the arguments you passed.
Example
#include <iostream>
using namespace std;
int area(int side) {
return side * side;
}
int area(int width, int height) {
return width * height;
}
int main() {
cout << area(4) << endl;
cout << area(4, 6) << endl;
return 0;
}How the compiler picks
Matching uses the parameter list, not the function body. The list of types and the number of parameters is the signature. Two functions with the same name are overloads only when the signatures differ.
| Call | Matches |
|---|---|
add(2, 3) | add(int, int) |
add(1.5, 2.5) | add(double, double) |
area(4) | area(int) |
area(4, 6) | area(int, int) |
If two overloads could both work, g++ reports an ambiguous call. Fix it by passing a value whose type is obvious, or by naming a different function.
Return type is not enough
You cannot overload two functions that take the same parameters and differ only in return type. The compiler decides from the arguments at the call site, and a return type is not an argument. This does not compile:
Example
int value();
double value();Default arguments can also clash with overloads. If greet() and greet(string name = "Ada")both exist, a call with no arguments has two matches. Keep default arguments or overloads, not both for the same call shape.
Keep the meaning the same
Overload when the versions do the same kind of work. Do not reuse a name for unrelated jobs. A reader who seesadd should get a sum, not a file write. Next: a function that calls itself.
Worked examples
The short listings above are there so you can see the grammar. The programs here use the same statements on quantities that already have units: a speed, a pH, a count of bases. They are classroom numbers. Air resistance is ignored. g is 9.81 m/s² unless a line says otherwise.
Open them in the C++ editor at /cpp/try. Change one measurement and check whether the result still has the right unit.
Physics
Energy of a mass, two ways
Near Earth, PE is m g h. Translational KE is ½ m v². Same name energy, different parameter lists. The compiler picks from the arguments: three doubles versus two.
2 kg at 3 m height is about 58.9 J of PE. 2 kg at 3 m/s is 9 J of KE. The names look alike; the physics is not.
PE = m g h, KE = ½ m v²
Example
#include <iostream>
using namespace std;
double energy(double mass, double g, double height) {
return mass * g * height;
}
double energy(double mass, double speed) {
return 0.5 * mass * speed * speed;
}
int main() {
cout << "PE = " << energy(2.0, 9.81, 3.0) << " J" << endl;
cout << "KE = " << energy(2.0, 3.0) << " J" << endl;
return 0;
}Maths
Add ints or doubles
2 + 3 is 5. 2.5 + 3.5 is 6. Integer add would truncate the doubles if you had only one function. Overloads keep both exact.
Example
#include <iostream>
using namespace std;
int add(int a, int b) { return a + b; }
double add(double a, double b) { return a + b; }
int main() {
cout << add(2, 3) << endl;
cout << add(2.5, 3.5) << endl;
return 0;
}