C++实现运算符重载
#include <iostream> using namespace std; //#define SHOW class Int { friend bool operator>(const Int& x,const Int& 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); //重载流插入运算符<< friend istream& operator>>(istream& input,const Int& x);//重载流提取运算符>> 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<<endl; #endif // SHOW } Int& operator=(const Int& x) { #ifdef SHOW cout<<"Assign,"<<this<<" : "<<&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++(); //前置++运算符 const Int operator++(int);//后置++运算符 const Int operator--(); //前置--运算符 const Int operator--(int);//后置--运算符 const Int operator+=(const Int& x); const Int operator-=(const Int& x); private: int i; }; ostream& 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(10),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; }
原文地址:http://blog.csdn.net/yangjiankang_0929/article/details/45600257