Operator Overloading is an important part of C ++. It makes the program easier to understand, and the use of simple operators makes the understanding of complex functions more intuitive.
For common objects, we naturally Use Arithmetic Operators frequently to involve them in computing. However, for custom objects, in any case, we cannot prevent writing programs like the following code.
Example:
C ++ code
Class Test { // Process omitted }
Int main () { Test a, c; C = a +; }
|
|
Of course, such Code cannot be compiled. c ++ retains the arithmetic operations of the custom class to the programmer, which also conforms to the flexible features of c ++.
To implement such operations in c ++, you must customize them.
Operator overload FunctionTo complete the specific work.
It is important to remind readers that the operator overload function of a custom class is also a function. All operators that you overload will not change the priority of their operations because they are defined by yourself, the operation Priority of custom operators is the same as that of internal operators.
In addition, c ++ also specifies that some operators cannot customize the overload, such as.,:, and so on.
420) {this. width = 420} ">
The preceding table lists operators that can be overloaded in C ++.
Next we will learn how to overload operators. The form of operator overload functions is:
Return operator symbol (parameter description)
{
// Internal implementation of the function body
}
There are two main methods to use the operator overload function,
One is used as a friend function of the class.,
The other is used as a member function of the class..
Next, let's take a look at the examples used as the class's friend functions:
C ++ code
// Program Author: Guan Ning // Site: www.cndev-lab.com // All the manuscripts are copyrighted. If you want to reprint them, be sure to use the famous source and author.
# Include <iostream> Using namespace std;
Class Test { Public: Test (int a = 0) { Test: a =; } Friend Test operator + (Test &, Test &); Friend Test & operator ++ (Test &); Public: Int; }; Test operator + (Test & temp1, Test & temp2) // + operator overload Function { // Cout <temp1.a <"|" <temp2.a <endl; // you can observe the member components of the passed referenced object. Test result (temp1.a + temp2.a ); Return result; } Test & operator ++ (Test & temp) // ++ operator overload Function { Temp. a ++; Return temp; } Int main () { Test a (100 ); Test c = a +; Cout <c. a <endl; C ++; Cout <c. a <endl; System ("pause "); }
|
|
In this example, for the user-defined class Test, the addition operator and the auto-increment operator are overloaded. The overload operator completes the addition and increment operations of the same type of objects.
The return type and format parameters of the heavy-duty operator function are adjusted according to the requirement. Next let's take a look at the modified plus operator heavy-duty function.
The Code is as follows:
C ++ code
// Program Author: Guan Ning // Site: www.cndev-lab.com // All the manuscripts are copyrighted. If you want to reprint them, be sure to use the famous source and author.
# Include <iostream> Using namespace std;
Class Test { Public: Test (int a = 0) { Test: a =; } Friend Test operator + (Test &, const int &); Public: Int; }; Test operator + (Test & temp1, const int & temp2) // + operator overload Function { Test result (temp1.a * temp2 ); Return result; } Int main () { Test a (100 ); Test c = a + 10; Cout <c. a <endl; Syste |
|