- Overloading in C++ allows specifying more than one definition for a function name or an operator in the same scope is called function overloading and operator overloading respectively.
- An overloaded function can have multiple definitions for the same function name in the same scope.
- The definition of the function must differ from each other by the types and/or the number of arguments in the argument list.
- Overloading occurs when the same operator or function name is used with different signatures.
- Both operators and functions can be overloaded.
- Operator overloading refers to giving normal C++ operators such as +,* and <= and so on, an additional meaning when they applied to user-defined data types.
C++ operators that can be overloaded
Operators that cannot be overloaded
- 'returnType' may be whatever the operator returns (Including a reference to the object of the operand).
- 'Operator' symbol may be any valid operator allowed by the language compiler.
Types of operators
- Unary Operators (prefix (!,&,^,......), postfix (++, --, ......))
- Binary operators(+, <, = ......)
Operators attached to single operand
- Eg: -a, +a, --a, a--, ++a, a++,.......
Operators attached to two operands
- Eg: a-b, a+b, a*b, a/b, a%b, a>b, a>=b, a<b, a<=b, a==b
Operators that cannot be overloaded due to safety reasons
- Member selection "." operator
- Member dereference operator
- Exponential "**" Operator
- User-defined Operators
- Operator precedence rule
using namespace std;
cout<<"enter the first and the second number:";
cout<"The sum of two numbers is "<<sum;
Enter the first and second number:4 4
The sum of two numbers is 8