Define a test class complex, where the method of the member function is overloaded with the + 、-、 front + +, the front--、 post + +, and the back--these 6 operators, of course, these 6 operators can also be overloaded with the method of the meta-function, but it is customary that these are directly overloaded with member functions.
Demo
#include <iostream>using namespace Std;class complex{public:complex (int a = 0, int b = 0) {This->a = A;this->b = b;} ~complex ();//print complex void printcom () {cout << a << "+" << b << "i" << Endl;} Implement the + operator overload Complex operator+ (Complex &c2) {Complex tmp (This->a + c2.a, this->b + c2.b); return tmp;} Implementation-Operator overloading Complex operator-(Complex &c2) {Complex tmp (this->a-c2.a, this->b-c2.b); return tmp;} Implements the predecessor + + operator overload complex& operator++ () {A++;b++;return *this;} Implement post + + operator overloading//This involves the use of the placeholder parameter, because the overloaded function and the pre-+ + + parameter without the placeholder//post + + are the same, so the overload has a syntax error complex& operator++ (int) {Complex TMP = *this;this->a++;this->b++;return tmp;} Implement predecessor-operator overloading complex& operator--() {A--;b--;return *this;} Implement post-operator overloading complex& operator--(int) {Complex tmp = *this;this->a--;this->b--;return tmp;} Private:int a;int b;}; int main () {return 0;}
Note that when both the front + + and the Post + + are implemented, the member functions overload when the parameters are the same and cannot implement the function overloading, only one of them should be added with a placeholder parameter.
1) Friend function and member function selection method
Use global functions to overload when the class of the left operand cannot be modified
=, [], () and operator can only be overloaded by member functions
2) UF meta function overloading << >> operator
IStream and Ostream are pre-defined stream classes for C + +
CIN is the object of IStream, cout is the object of Ostream
Operator << by Ostream overload for insert operation for output of basic type data
Operator >> by IStream overload for extraction operation, for entering basic type data
UF function overloading << and >>, output and input user-defined data types
Overloads the left and right shift operators for the complex class in the first demo.
Demo
<pre name= "code" class= "CPP" >//friend function method implementation << operator overloading ostream& operator<< (ostream& out, Complex & AMP;C1) {<span style= "white-space:pre" ></span>out << c1.a << "+" << c1.b << "I" <& Lt Endl;<span style= "White-space:pre" ></span>return out;} Friend function method implementation >> operator overloading istream& operator>> (istream& in, Complex &c1) {<span style= "White-space: Pre "></span>in >> c1.a >> c1.b;<span style=" White-space:pre "></span>return in;}
Remember to add the complex in the class
Friend ostream& operator<< (ostream& out, Complex &c1); friend istream& Operator>> (IStream & In, Complex &c1);
This can only be used to do the operator overloading, because if you use a member function, you have to add code to the declaration of the data type on the left side of the operator, but cout or CIN can get it, even if it is obtained, it is not desirable, because it is best not to bring the library files to modify. So the best way to do this is to do the meta-function.
3) Friend function overloaded operator use note point
A) the friend function overloading operator is often used in cases where operators have different types of left and right operands.
b) Other
In the case where the first argument requires an implicit conversion, making the UF function overload operator is the correct choice.
The friend function does not have this pointer, and the required operands must be explicitly declared in the parameter table, which is easy to implement the implicit conversion of the type.
The operators in C + + that cannot be overloaded with a member function are:
= () [],
Application scenario of operator overloading with friend function--friend function implements left shift right shift operator overloading