An operator overload is essentially a function overload that overloads the member function, and it is free to access the data members of this class, and when actually used, accesses the overloaded operator through an object of that class. If the binocular operator, the left operand is the data of the object itself, as indicated by the this pointer, no parameters are required. These two cases are described below.
For binocular operator B, if you are overloading a member function of a class so that it can implement an expression Oprd1 b oprd2, where Oprd1 is an object of Class A, you should overload B with the member function of Class A, which has only one formal parameter and the type of the formal parameter that the oprd2 belongs to. After overloading, an expression is equivalent to a function call Orpd.operator B (OPRD2).
For the pre-order operator U, such as "-" (minus), if you want to overload the member function of the class to implement the expression U OPRD, where OPRD is an object of Class A, you should overload the member function of Class A, the function has no formal parameter, after overloading, the expression u OPRD is equivalent to a function call Oprd.operator U ().
Then take a look at the post operator "+ +" and "–" If you are overloading a member function of a class to implement an expression oprd++ or oprd–, where OPRD is an object of Class A, then the operator should be overloaded with a member function of Class A, when the function takes an integral type (int) parameter. After overloading, the expression OPRD is equivalent to calling oprd.oprator++ (0) and oprd.oprator– (0). The int type parameter here does not play any role in the operation, except that it differs from the front-facing.
Complex class addition and subtraction operation overloading as member function form
#include <iostream> #include <cassert>using namespace STD;class Complex{ Public:Complex(DoubleR=0.0,DoubleI=0.0): Real (R), Imag (i) {}Complex operator+(Const Complex&C2)Const;//operator + overloaded member function Complex operator-(Const Complex&C2)Const;//Operator-overloaded member function voidDisplay ()Const;Private:DoubleRealDoubleImag;};Complex Complex::operator+(Const Complex&C2)Const{return Complex(REAL+C2.REAL,IMAG+C2.IMAG);//Create a temporary nameless object as the return value}Complex Complex::operator-(Const Complex&C2)Const{return Complex(Real-c2.real,imag-c2.imag);}void Complex::d Isplay ()Const{cout<<"("<<real<<","<<imag<<")"<<endl;}intMain () {ComplexC1 (5,4), C2 (2,Ten), C3;cout<<"c1="; C1.display ();cout<<"c2="; C2.display (); C3=C1-C2;//Use overloaded operator to complete complex subtraction cout<<"c3=c1-c2="; C3.display (); C3=C1+C2;cout<<"c3=c1+c2="; C3.display ();return 0;} Overloads of the single-mesh operator "+ +" are in the form of member functions.#include <iostream> #include <cassert>using namespace STD;classclock{ Public: Clock (intHour=0,intMinute=0,intSecond=0);voidShowtime ()Const; clock&operator++(); Clockoperator++(int);Private:intHour,minute,second;}; Clock::clock (intHourintMinuteintSecond) {if(0<=hour&&hour< -&&0<=minute&&minute< -&&0<=second&&second< -) { This->hour=hour; This->minute=minute; This->second=second; }Else{cout<<"Time error!"<<endl; }}voidClock::showtime ()Const{cout<":"<<minute<<":"<<second<<endl;} Clock &clock::operator++()//Pre-order operator overloading function{second++;if(second>= -) {second-= -; minute++;if(minute>= -) {minute-= -; Hour= (hour+1)% -; } }return* This;} Clock clock::operator++(int)//post-order operator overloading{Clock old=* This; ++(* This);//Call the predecessor "+ +" operator returnOld;}intMain () {clock Myclock ( at, -, -);cout<<"First time output:"; Myclock.showtime ();cout<<"Show myclock++"; (myclock++). Showtime ();cout<<"Show ++myclock"; (++myclock). Showtime ();return 0;}
Operator overloading is a member function