C + + implements operator overloading
#include <iostream>using namespace std;//#define Showclass int{friend bool operator> (const int& X,const I nt& y); Friend int operator> (const int& x,const int& y); friend bool operator< (const int& x,const int& y); friend bool operator== (const int& x,const int& y); Friend Int operator<< (const int& x,const int& y); Friend Int operator>> (const int& x,const int& y); Friend int& operator+= (int& x,const int& y); Friend int& operator-= (int& x,const int& y); Friend int& operator*= (int& x,const int& y); Friend int& operator/= (int& x,const int& y); Friend ostream& operator<< (ostream& out,const int& x); Overloaded stream insertion operator << friend istream& operator>> (istream& input,const int& x);//overloaded stream extraction operator >> Public:int (int x=0): I (x) {#ifdef SHOW cout<< "creat Int Obj:" <<this<<Endl #endif//SHOW} Int (const int& x) {#ifdef SHOW cout<< "Copy Int Obj:" <<this<& Lt;endl; #endif//Show} int& operator= (const int& x) {#ifdef SHOW cout<< "Assign," <<th is<< ":" <<&x<<endl; #endif//SHOW if (this! = &x) {i = x.i; } return *this; } ~int () {#ifdef SHOW cout<< "Free Int Obj:" <<this<<endl; #endif//SHOW} const INT operator+ (const int& x); Const INT operator-(const int& x); Const INT operator* (const int& x); Const INT operator/(const int& x); Const INT operator% (const int& x); Const INT operator& (const int& x); Const INT operator| (const int& x); Const INT operator^ (const int& x); Const INT operator~ (); Const INT operator! (); Const INT operator++ (); Predecessor + + operator Const INT Operator++ (int);//post + + operator const int operator--(); Predecessor--Operator const int operator--(int);//post--Operator const int operator+= (const int& x); Const INT operator-= (const int& x);p rivate:int I;};o stream& operator<< (ostream& out,const int& x) {out<<x.i; return out;} istream& operator>> (istream& input,const int& x) {input>>x.i; return input;} BOOL Operator> (const int& x,const int& y) {if (x.i>y.i) {return true; } else {return false; }}int operator> (const int& x, const int& y) {return x.i>y.i;} BOOL operator< (const int& x,const int& y) {if (x.i<y.i) {return true; } else {return false; }}bool operator== (const int& x,const int& y) {if (x.i==y.i) {return true; } else {return false; }}int operator<< (const int& x,const int& y) {return (X.I<<Y.I);} Int Operator>> (const int& x,const int& y) {return (X.I>>Y.I);} int& operator+= (int& x,const int& y) {x.i + = y.i; return x;} int& operator-= (int& x,const int& y) {x.i-= y.i; return x;} int& operator*= (int& x,const int& y) {x.i *= y.i; return x;} int& operator/= (int& x,const int& y) {x.i/= y.i; return x;} Const INT int::operator+ (const int& x) {return (I+X.I);} Const INT int::operator-(const int& x) {return (I-X.I);} Const INT int::operator* (const int& x) {return (I*X.I);} Const INT int::operator/(const int& x) {return (I/X.I);} Const INT int::operator% (const int& x) {return (I%X.I);} Const INT int::operator& (const int& x) {return (I&X.I);} Const INT int::operator| (const int& x) {return (I|X.I);} Const INT int::operator^ (const int& x) {return (I^X.I);} Const INT int::operator~ () {return ~i;} Const INT int::operator! () {return!i;} Const INT INT::OPerator++ () {+ + I; return *this;} const int int::operator++ (int) {int tmp (*this); i + +; return TMP;} Const INT int::operator--() {---I; return *this;} const int int::operator--(int) {int tmp (*this); I--; return TMP;} int main () {int T (ten), T1 (9), T2 (3); t = t1 + t2; cout<<t<<endl; t = t1-t2; cout<<t<<endl; t = t1 * T2; cout<<t<<endl; t = t1/t2; cout<<t<<endl; t = t1% T2; cout<<t<<endl; + + t; cout<<t<<endl; T + +; cout<<t<<endl; --T; cout<<t<<endl; T--; cout<<t<<endl; t = t1>t2; cout<<t<<endl; t = t1<t2; cout<<t<<endl; t = t1==t2; cout<<t<<endl; t = t1&t2; cout<<t<<endl; t = t1|t2; cout<<t<<endl; t = t1^t2; cout<<t<<endl; t = ~t1; cout<<t<<endl; T=!t1; Cout<<t<<endl; t = t1<<t2; cout<<t<<endl; t = t1>>t2; cout<<t<<endl; T + = T1; cout<<t<<endl; T-= t1; cout<<t<<endl; t *= T1; cout<<t<<endl; t/= T1; cout<<t<<endl; return 0;}
C + + operator overloading