int 运算符重载
来源:互联网 发布:android 数据库加密 编辑:程序博客网 时间:2024/05/13 00:49
为了更深入的理解返回引用和对象,以及运算符重载,做了int的运算符重载:
#include <iostream>using namespace std;class Int;ostream& operator<<(ostream &out, const Int &i);class Int{ friend ostream& operator<<(ostream &out, const Int &i);public: Int(int i = 0) : m_i(i) {} ~Int() {}public: Int operator+(const Int &i); Int operator-(const Int &i); Int operator*(const Int &i); Int operator/(const Int &i); Int operator+=(const Int &i); Int operator-=(const Int &i); Int operator*=(const Int &i); Int operator/=(const Int &i); Int operator%(const Int &i); Int operator^(const Int &i); Int operator%=(const Int &i); Int operator^=(const Int &i); Int& operator++(); //前置++ Int operator++(int); //后置++ Int& operator--(); Int operator--(int); Int& operator+(); //正a Int& operator-(); //负a Int& operator~(); //取反 Int& operator!(); //取非 Int& operator>>(int n); //右移 Int& operator<<(int n); //左移 Int& operator>>=(int n); //右移等 Int& operator<<=(int n); //左移等 bool operator>(const Int &i); bool operator<(const Int &i); bool operator>=(const Int &i); bool operator<=(const Int &i); bool operator==(const Int &i); bool operator!=(const Int &i); Int operator&(const Int &i); //与运算(位) Int operator|(const Int &i); //或运算(位) bool operator&&(const Int &i); bool operator||(const Int &i);private: int m_i;};ostream& operator<<(ostream &out, const Int &i){ out << (i.m_i); return out;}Int Int::operator+(const Int &i){ return Int(m_i + i.m_i);}Int Int::operator-(const Int &i){ return Int(m_i - i.m_i);}Int Int::operator*(const Int &i){ return Int(m_i * i.m_i);}Int Int::operator/(const Int &i){ return Int(m_i / i.m_i);}Int Int::operator+=(const Int &i){ return Int(m_i += i.m_i);}Int Int::operator-=(const Int &i){ return Int(m_i -= i.m_i);}Int Int::operator*=(const Int &i){ return Int(m_i *= i.m_i);}Int Int::operator/=(const Int &i){ return Int(m_i /= i.m_i);}Int Int::operator%(const Int &i){ return Int(m_i % i.m_i);}Int Int::operator^(const Int &i){ return Int(m_i ^ i.m_i);}Int Int::operator%=(const Int &i){ return Int(m_i %= i.m_i);}Int Int::operator^=(const Int &i){ return Int(m_i ^= i.m_i);}Int& Int::operator++() //前置++{ m_i++; return *this;}Int Int::operator++(int) //后置++{ Int tmp(m_i); m_i += 1; return tmp;}Int& Int::operator--() //前置--{ m_i--; return *this;}Int Int::operator--(int) //后置--{ Int tmp(*this); m_i -= 1; return tmp;}Int& Int::operator+() //正a{ m_i = +m_i; return *this;}Int& Int::operator-() //负a{ m_i = -m_i; return *this;}Int& Int::operator~() //取反{ m_i = ~m_i; return *this;}Int& Int::operator!() //取非{ m_i = !m_i; return *this;}Int& Int::operator>>(int n) //右移{ m_i = (m_i >> n); return *this;}Int& Int::operator<<(int n) //左移{ m_i = (m_i << n); return *this;}Int& Int::operator>>=(int n) //右移等{ m_i >>= n; return *this;}Int& Int::operator<<=(int n) //左移等{ m_i <<= n; return *this;}bool Int::operator>(const Int &i){ return m_i > i.m_i;}bool Int::operator<(const Int &i){ return m_i < i.m_i;}bool Int::operator>=(const Int &i){ return m_i >= i.m_i;}bool Int::operator<=(const Int &i){ return m_i <= i.m_i;}bool Int::operator==(const Int &i){ return m_i == i.m_i;}bool Int::operator!=(const Int &i){ return m_i != i.m_i;}Int Int::operator&(const Int &i) //与运算(位){ Int tmp; tmp.m_i = (m_i & i.m_i); return tmp;}Int Int::operator|(const Int &i) //或运算(位){ Int tmp; tmp.m_i = m_i | i.m_i; return tmp;}bool Int::operator&&(const Int &i){ return m_i && i.m_i;}bool Int::operator||(const Int &i){ return m_i || i.m_i;}int main(){ Int i; Int i1(1); Int i2(2); cout << "i1 + i2 = " << i1 + i2 << endl; cout << "i1 - i2 = " << i1 - i2 << endl; cout << "i1 * i2 = " << i1 * i2 << endl; cout << "i1 / i2 = " << i1 / i2 << endl; cout << "i1 += 2 = " << (i1 += 2) << endl; cout << "i1 -= i2 = " << (i1 -= i2) << endl; cout << "i1 *= i2 = " << (i1 *= i2) << endl; cout << "i1 /= i2 = " << (i1 /= i2) << endl; cout << "i1 % i2 = " << (i1 % i2) << endl; cout << "i1 ^ i2 = " << (i1 ^ i2) << endl; cout << "i1 %= 1 = " << (i1 %= 1) << endl; cout << "i2 ^= 3 = " << (i2 ^= 3) << endl; Int i3(1); Int i4(1); cout << "++i3 = " << ++i3 << endl; cout << "i4++ = " << i4++ << endl; cout << "--i3 = " << --i3 << endl; cout << "i4-- = " << i4-- << endl; Int i5(2); Int i6(3); cout << (i5 == i6) << endl; cout << (i5 != i6) << endl; cout<< "(i5 <<= 1) = " << (i5 <<= 1) << endl; cout<< "(i5 <<= 1) = " << (i5 <<= 1) << endl; cout<< "(i6 || 1) = " << (i6 || 1) << endl; cout<< "(i6 && 0) = " << (i6 && 0) << endl; cout<< "(i6 | 1) = " << (i6 | 1) << endl; cout<< "(i6 & 1) = " << (i6 & 1) << endl; cout<< "-i6 = " << -i6 <<endl; cout<< "~i6 = " << ~i6 <<endl; return 0;}
2 0
- int 运算符重载
- Int函数运算符重载
- 运算符重载 [++,--,+,<<; &&,||(不可重载)]
- 重载之运算符重载
- 重载赋值运算符
- C++运算符重载
- 运算符重载
- 运算符重载总结
- 运算符重载例子
- 运算符重载
- 运算符重载
- 基础:运算符重载
- 运算符重载
- 运算符重载
- 重载赋值运算符
- C#运算符重载
- C++-运算符重载
- 重载赋值运算符
- LeetCode 36 Valid Sudoku
- 聊聊Oracle 11g中的char类型使用
- libxml/tree.h file not found
- android 开发的7/365 设计模式复习
- android最新的选择图片及上传
- int 运算符重载
- js与jQuery的DOM操作比较(二)
- R packages: dplyr拾遗
- C/C++中tm结构
- java设计模式简要
- tar打包压缩指定目录文件
- JSONObject.toBean时多个类转换
- listview和imageview实现条目单选效果
- 小白笔记-------------------课程作业(sfind)