C++ Overloading
Overloading allows for more than one definition of a function or operator in the same scope. Respectively, it is called function overloading and operator overloading.
Function Overloading
Function overloading begins with declaring a function with the same name as a previously declared function but with different parameters.
Note: Each declaration must have different parameters. Only changing the function’s return type will not work.
Syntax
Since a function can be overloaded multiple times, the syntax can look different case-to-case. The following is an outline of what an overloaded function could look like:
functionName(parameters1);
functionName(parameters2);
More declarations can be added as needed and a declaration has the following parts:
functionName: The name of the function that is the same for each definition.parameters1andparameters2: Parameters for each definition, these must be different for each definition.
Example
The following example overloads the multiply() function:
#include <iostream>using namespace std;void multiply(int x, int y) {cout << "The product of " << x << " and " << y << " is: " << (x*y) << endl;}void multiply(double x, double y) {cout << "The product of " << x << " and " << y << " is: " << (x*y) << endl;}int main() {multiply(20,10);multiply(80.2, 90.99);}
This will output:
The product of 20 and 10 is: 200The product of 80.2 and 90.99 is: 7297.4
Operator Overloading
Operator overloading redefines built-in operators for user-defined classes. When an overloaded operator is called, the compiler determines which definition to use based on the arguments provided. The following operators can be overloaded:
| Category | Operators | Names |
|---|---|---|
| Arithmetic | +, -, *, /, %, ++, —- |
add/positive, subtract/negative, multiply, divide, modulo, increment, decrement |
| Assignment | =, +=,*=, /=,-=, %=, &=, ^=, |=, <<=, >>= |
assign, add & assign, multiply & assign, divide & assign, subtract & assign, modulo & assign, bitwise AND & assign, bitwise exclusive OR & assign, bitwise inclusive OR & assign, shift bits left & assign, shift bits right & assign |
| Bitwise | &, |, ^, ~, <<, >> |
bitwise AND, bitwise inclusive OR, bitwise exclusive OR, bit inversion, shift bits left, shift bits right |
| Logical | &&, ||, ! |
logical AND, logical OR, logical NOT |
| Relational | ==, !=, >, <, >=, <= |
equal to, not equal to, greater than, less than, greater than or equal to, less than or equal to |
| Member Access | ->, ->* |
member of pointer, pointer to member of pointer |
| Allocation/Deallocation | new, new[], delete, delete[] |
new, new & allocate, delete, delete & allocate |
| Other | ,, (), [] |
comma, function call, subscript/array index |
The operators below cannot be overloaded:
| Category | Operators | Names |
|---|---|---|
| Conditional (or Ternary) | ? |
conditional |
| Scope | :: |
scope access |
| Member Access | ., .* |
member of object, pointer to member of object |
Syntax
class className {
public:
returnType operator symbol (arguments) {
// code goes here
}
};
The operator keyword is used along with the following:
className: Name of the class.returnType: Return type of the function.symbol: Operator in which to overload.arguments: Arguments to pass in.
Codebyte Example
The following codebyte example overloads the plus (+) operator within the Pyramid class and returns the volume of two pyramids added together:
Overloading
- Separate Overload
- When operand order changes, separate overloads are required for operators to handle each case correctly.
Contribute to Docs
- Learn more about how to get involved.
- Edit this page on GitHub to fix an error or make an improvement.
- Submit feedback to let us know how we can improve Docs.
Learn C++ on Codecademy
- Looking for an introduction to the theory behind programming? Master Python while learning data structures, algorithms, and more!
- Includes 6 Courses
- With Professional Certification
- Beginner Friendly.75 hours
- Learn C++ — a versatile programming language that’s important for developing software, games, databases, and more.
- Beginner Friendly.11 hours