The operator overload is another advanced function of C . It enables users to use the operand to use the operand to use the same convenience, simplicity, image, and increase program readability. The overload operator can be used either user function or a member function. It is recommended to use member functions to overload because the friend function destroys C encapsulation. Self-write procedure:
// overloading operator # include
Class Coor {Friend Istream & Operator >> (ISTREAM &, COOR &); Friend Ostream & Operator << (Ostream &, Coor &);
Public: Coor (int = 0, int = 0); COOR OPERATOR ; COOR Operator- (Coor &); Bool Operator == (COOR &); BOOL Operator! = (Coor &);
PRIVATE: INT X; INT Y;
iStream & Operator >> (ISTREAM & INPUT, COL & D) {INPUT.IGNORE (); Input >> D.X; Input.Ignore (); Input >> D.Y; Input.Ignore ();
Return INPUT;
Ostream & Operator << (Ostream & Output, Coor & D) {OUTPUT << '(' << D.x << ',' << D.Y << ')' << Endl;
Return Output;
COOR :: Coor (int x, int y) {x = x; y = y;}
COOR COOR :: Operator (COOR & A) {COOR C; C.X = this-> x a.x; c.y = this-> y a.y;
Return C;
COOR COOR :: Operator - (COOR & A) {COOR C; C.x = this-> x - a.x; c.y = this-> y - a.y;
Return C;
Bool Coor :: Operator == (COOR & A) {IF (this-> x == a.x && this-> y == a.y) Return True; Else Return False;}
Bool Coor :: Operator! = (COOR & A) {Return! (* this == a);}
Int main () {COOR NUM1 (5, 9), Num2 (5, 9), SUM; COUT << "Num1 =" << Num1 << Endl; cout << "Num2 = << Num2 << ENDL; Cout << "SUM =" << Sum << Endl;
IF (Num1 == Num2) cout << "NUM1 IS Equal to Num2 << ENDL; IF (Num1! = Num2) cout <<" Num1 IS Not Equal to Num2 << ENDL;
Cout << "Type the Num2:" << Endl; CIN >> Num2; cout << "NUM2 = << Num2 << endl;
IF (Num1 == Num2) cout << "NUM1 IS Equal to Num2 << ENDL;
IF (Num1! = Num2) cout << "Num1 IS Not Equal to Num2 << ENDL;
SUM = Num1 Num2; COUT << "Num1 Num2 = SUM =" << Sum << Endl;
Return 0;}