C + + operator overloading

Source: Internet
Author: User

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&LT;&LT;Y.I);} Int Operator>> (const int& x,const int& y) {return (X.I&GT;&GT;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&AMP;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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.