Operator Overloading:
Some of the built-in operators in C + + can only be used to operate on variables or constants of certain primitive types, not in objects
Operation. Sometimes we want to be able to apply these operators to objects, making the program more concise and understandable. While the C + + delivery
operator overloading mechanism, which gives operators new functions to solve these problems.
The essence of operator overloading is to write functions that are named as operators. It is defined in the following format:
return value type operator operator (parameter table) {
......
}
An expression that contains an overloaded operator is compiled into a call to an operator function, and the operand of the operator becomes the function
argument, the result of the operation is the return value of the function. Operators can be overloaded multiple times and can be overloaded into member functions or full-
Local functions are all possible.
When an operator is overloaded with a global function, the number of arguments equals the number of operator operands, and when operator overloading is a member function, the parameter
The number of numbers equals the number of operator operands minus one.
General principles for operator overloading:
assignment = subscript [] Call () and member access arrows--operators must be defined as members, and these operators are defined as non-members
The function will be marked as error at compile time.
Like assignments, a composite operator should typically be defined as a member of a class. Unlike assignments, it is not necessarily necessary to do so, if
Non-member compound assignment operator, no error occurs.
Some other operators, such as self-increment, self-decrement, and reference, that change the state of an object or are closely related to a given type, should typically be defined as
Class member.
The operands, such as arithmetic operators, equality operators, relational operators, and bitwise operators, are best defined as ordinary non-members
Function.
#include <iostream>using namespacestd;classcomplex{ Public: DoubleReal, imag; Complex (DoubleR =0.0,Doublei =0.0): Real (R), Imag (i) {} Complexoperator-(ConstComplex &c);//overloading as member functions}; Complexoperator+(ConstComplex &a,ConstComplex &b) {//overloading as global function returnComplex (A.real + b.real, A.imag + b.imag);//returns a temporary object}complex Complex::operator-(ConstComplex &c) { returnComplex (Real-c.real, Imag-c.imag);//returns a temporary object}intMain () {Complex A (4,4), B (1,1), C; C= a + b;//equivalent to C = operator+ (A + b);cout << c.real <<" , "<< C.imag <<Endl; cout<< (A-B). Real <<" , "<< (A-B). Imag <<Endl; return 0;}
Overloaded assignment Operator "="
An assignment operation requires that the type of the left and right two operands be at least matched, or at least compatible.
C + + Specifies that the assignment operator "=" can only be overloaded as a member function.
When overloading operators, a good style should try to preserve the original attributes of the operators.
#include <iostream>using namespacestd;classccar{ Public: intPrice ; Ccar (void) { price=0; } ccar (intp):p Rice (p) {} Ccar&operator=(ConstCCAR *car);}; Ccar& Ccar::operator=(ConstCCAR *car) { Price= car->Price ; return* This; } intMain () {Ccar C1 (Ten), C2; C2=C1; cout<< C1.price <<" , "<< C2.price <<Endl; return 0;}
Shallow copy and deep copy:
Peer objects can be assigned to each other through the assignment operator "=". If not overloaded, the "=" function is to put the left
Each member variable of an object becomes equal to the object on the right, which is the work of a byte-by-bit copy called a shallow
Copy. Sometimes two objects are equal, from the meaning of the actual application, it should not mean that each byte of two objects
Are the same, but there are other explanations, then the "=" needs to be overloaded.
stringS1, S2;s1=" This"; S2=" that"; S2= S1;//causes the S2.str and s1.str to point to the same address, performing a shallow copy, causing the memory area that the S2.STR originally pointed to not be freed//To solve these problems, we need to overload the "=". String& String::operator=(ConstString &1) { if(str = =s.str)return* This; if(str)Delete[] str; if(s.str) {str=New Char[Strlen (S.STR) +1]; strcpy (str, S.STR); } ElseStr=NULL; return* This;}
Operator overloading is a friend function:
In general, overloading an operator as a member function is a good choice, but sometimes overloading is a member function that does not meet the required
, it is necessary to overload the global function, but the private member of the object cannot be accessed as a member function, so this is required
Reload it as a friend.
classcomplex{Private: DoubleReal; DoubleImag; Public: Complex (DoubleR =0.0,Doublei =0.0): Real (R), Imag (i) {} Complex (ConstComplex &c) {Real=C.real; Imag=C.imag; } //Overloaded addition operatorsFriend Complexoperator+(ConstComplex &c1,ConstComplex &C2); //Overloaded subtraction operatorFriend Complexoperator-(ConstComplex &c1,ConstComplex &C2); //The compound assignment operator is typically defined while the arithmetic operator is defined, and the compound assignment operator is implemented by the arithmetic operator//Compound assignment operatorComplex &operator+=(ConstComplex &C2); Complex&operator-=(ConstComplex &C2); //overloaded stream insert operators and stream extraction operatorsFriend std::istream&operator>> (std::istream& is, Complex &c); Friend Std::ostream&operator<< (std::ostream& OS,ConstComplex &c); //The self-increment decrement operator, which is divided into pre-and post-form, is generally post-form by accepting an additional int parameter, which the compiler provides 0 as the parameterComplex &operator++(); Complex&operator--(); Complexoperator++(inti); Complexoperator--(inti); }; Complexoperator+(ConstComplex &c1,ConstComplex &C2) { returnComplex (C1.real + c2.real, C1.imag +c2.imag);} Complexoperator-(ConstComplex &c1,ConstComplex &C2) { returnComplex (C1.real-c2.real, C1.imag-c2.imag);} Complex& Complex::operator+=(ConstComplex &C2) {Complex C; C= * This+C2; * This=C; return* This;} Complex& Complex::operator-=(ConstComplex &C2) {Complex C; C= * This-C2; * This=C; return* This;} Std::istream&operator>> (std::istream& is, Complex &c) { is>> C.real >>C.imag; //error handling mechanism ... return is;} Std::ostream&operator<< (std::ostream& OS,ConstComplex &c) {OS<< C.real <<" , "<< C.imag <<Endl; returnOS;} Complex& Complex::operator++(){ //Error Handling ...++Real; ++Imag; return* This; } Complex& Complex::operator--(){ //Error Handling ...--Real; --Imag; return* This; } Complex Complex::operator++(inti) {Complex C (* This); ++* This; returnC;} Complex Complex::operator--(inti) {Complex C (* This); --* This; returnC;}
C + + operator overloading