C++ allows a function and operator in the same scope to specify multiple definitions, called function overloading and Operator overloading.
An overloaded declaration is a declaration that has the same name as a function or method that has been previously declared within the scope, but their parameter list and definition (implementation) are not the same.
When you call a overloaded function or overloaded operator, the compiler compares the type of parameter you are using with the type of parameter in the definition. Decided to choose the most appropriate definition. The process of choosing the most appropriate overloaded function or overloaded operator is called overload decision.
Function overloading in C++
In the same scope, several functions with the same name can be declared, but the formal parameters of the same-named function (the number, type, or order of parameters) must be different. You cannot override a function by simply returning a different type.
In the example below, the same name function print() is used to output different data types:
When the above code is compiled and executed, it produces the following results:
Integer is: 5 The floating point number is: 500.263 The string is: Hello C++
Operator overloading in C++
You can redefine or override most of the C++ built-in operators. This way, you can use operators of custom types.
The overloaded operator is a function with a special name, and the function name consists of the keyword operator and the operator symbol to be overloaded later. Like other functions, overloaded operators have a return type and a list of arguments.
Box operator+(const Box&);
The declaration addition operator is used to add two Box objects and return the final Box object. Most overloaded operators can be defined as ordinary non-member functions or as class member functions. If we define the above function as a non-member function of the class, then we need to pass two parameters for each operation, as follows:
Box operator+(const Box&, const Box&);
The following example demonstrates the concept of operator overloading using member functions. Here, the object is passed as a parameter, and the object's properties are accessed using the this operator, as shown below:
When the above code is compiled and executed, it will produce the following results:
Volume of Box1 : 210 Volume of Box2 : 1560 Volume of Box3 : 5400
Overloadable operator/non-overloadable operator
The following is a list of overloadable operators:
|Binocular arithmetic operator||+ (plus), - (minus), * (multiply), / (division)，% (modulo)|
|Relational operator||==(equal),!= (not equal)，< (Less than)，> (more than the >，<=(Less than or equal to)，>=(greater or equal to)|
|Logical operator||||(Logical OR)，&&(Logic and),! (Logical)|
|Monocular operator||+ (positive), - (negative), * (pointer)，&(Take the address)|
|Self-increasing decrement operator||++ (self-increase), -- (self-reduction)|
|Bit operator||| (Bitwise or)，& (by bit and ), ~ (inverted by bit), ^ (bitwise XOR), << (Shift left)，>>(Right shift)|
|Assignment operator||=, +=, -=, *=, /= , % = , &=, |=, ^=, <<=, >>=|
|Space application and release||New, delete, new[ ] , delete|
|Other operators||() (function call), ->(Member visit), , (comma), (subscript)|
The following is a list of operators that cannot be overloaded:
- .: member access operator
- .*, ->*: member pointer access operator
- ::: domain operator
- sizeof:length operator
- ?:: Conditional Operators
- #: Preprocessing symbols
operator overload example
The following examples of various operator overloads are provided to help you better understand the concept of overloading.