// 实现运算符的重载 #include <iostream> using namespace std; class Int { public: Int(int i = 0) :m(i) { cout << "constructed function" << endl; } ~Int() { cout << "destructor" << endl; } public: Int operator+(const Int &d); Int operator-(const Int &d); Int operator*(const Int &d); Int operator/(const Int &d); Int operator|(const Int &d); Int operator&(const Int &d); Int operator^(const Int &d); Int operator~(); Int operator++(); Int operator++(int); Int operator--(); Int operator--(int); void print(); private: int m; }; Int Int ::operator+(const Int &d) { return Int(m + d.m); } Int Int::operator-(const Int &d) { return Int(m - d.m); } Int Int::operator*(const Int &d) { return Int(m * d.m); } Int Int::operator/(const Int &d) { return Int(m / d.m); } Int Int::operator|(const Int &d) { return Int(m | d.m); } Int Int::operator&(const Int &d) { return Int(m & d.m); } Int Int::operator^(const Int &d) { return Int(m ^ d.m); } Int Int::operator~() { return Int(~m); } Int Int::operator++() { return Int(++m); } //后置++ Int Int::operator++(int) { return Int(m++); } Int Int::operator--() { return Int(--m); } //后置-- Int Int::operator--(int) { return Int(m--); } void Int::print() { cout << m << endl; } int main() { Int i(2); i = i + 1; i.print(); i = i - 1; i.print(); Int a, b(3), c(1); a = b + c; a.print(); a = b - c; a.print(); a = b * c; a.print(); a = b / c; a.print(); a = b | c; a.print(); a = b & c; a.print(); a = b ^ c; a.print(); a = ~b; a.print(); a = ++b; a.print();//4 b.print();//4 a = b++; a.print();//4 b.print();//5 a = --b; a.print();//4 b.print();//4 a = b--; a.print();//4 b.print();//3 return 0; }// 实现运算符的重载 #include <iostream> using namespace std; class Int { public: Int(int i = 0) :m(i) { cout << "constructed function" << endl; } ~Int() { cout << "destructor" << endl; } public: Int operator+(const Int &d); Int operator-(const Int &d); Int operator*(const Int &d); Int operator/(const Int &d); Int operator|(const Int &d); Int operator&(const Int &d); Int operator^(const Int &d); Int operator~(); Int operator++(); Int operator++(int); Int operator--(); Int operator--(int); void print(); private: int m; }; Int Int ::operator+(const Int &d) { return Int(m + d.m); } Int Int::operator-(const Int &d) { return Int(m - d.m); } Int Int::operator*(const Int &d) { return Int(m * d.m); } Int Int::operator/(const Int &d) { return Int(m / d.m); } Int Int::operator|(const Int &d) { return Int(m | d.m); } Int Int::operator&(const Int &d) { return Int(m & d.m); } Int Int::operator^(const Int &d) { return Int(m ^ d.m); } Int Int::operator~() { return Int(~m); } Int Int::operator++() { return Int(++m); } //后置++ Int Int::operator++(int) { return Int(m++); } Int Int::operator--() { return Int(--m); } //后置-- Int Int::operator--(int) { return Int(m--); } void Int::print() { cout << m << endl; } int main() { Int i(2); i = i + 1; i.print(); i = i - 1; i.print(); Int a, b(3), c(1); a = b + c; a.print(); a = b - c; a.print(); a = b * c; a.print(); a = b / c; a.print(); a = b | c; a.print(); a = b & c; a.print(); a = b ^ c; a.print(); a = ~b; a.print(); a = ++b; a.print();//4 b.print();//4 a = b++; a.print();//4 b.print();//5 a = --b; a.print();//4 b.print();//4 a = b--; a.print();//4 b.print();//3 return 0; } <img src="http://img.blog.csdn.net/20150508161939603?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvemhhb3lhcWlhbjU1Mg==/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" alt="" /> <img src="http://img.blog.csdn.net/20150508161752132?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvemhhb3lhcWlhbjU1Mg==/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" alt="" />
时间: 2024-10-29 12:27:04