int 运算符重载

为了更深入的理解返回引用和对象,以及运算符重载,做了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;
}
  • 3
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
在类中定义一个int变量和vector容器,并对其进行拷贝构造、+运算符重载、=运算符重载、输出流运算符重载和自增运算符重载操作的示例代码如下: ```cpp #include <iostream> #include <vector> class MyClass { private: int myInt; std::vector<int> myVector; public: // 构造函数 MyClass(int num, const std::vector<int>& vec) : myInt(num), myVector(vec) {} // 拷贝构造函数 MyClass(const MyClass& other) : myInt(other.myInt), myVector(other.myVector) {} // +运算符重载 MyClass operator+(const MyClass& other) { int sum = myInt + other.myInt; std::vector<int> resultVec = myVector; resultVec.insert(resultVec.end(), other.myVector.begin(), other.myVector.end()); return MyClass(sum, resultVec); } // =运算符重载 MyClass& operator=(const MyClass& other) { if (this != &other) { myInt = other.myInt; myVector = other.myVector; } return *this; } // 输出流运算符重载 friend std::ostream& operator<<(std::ostream& os, const MyClass& obj) { os << "myInt: " << obj.myInt << ", myVector: ["; for (int i = 0; i < obj.myVector.size(); ++i) { os << obj.myVector[i]; if (i != obj.myVector.size() - 1) { os << ", "; } } os << "]"; return os; } // 自增运算符重载 MyClass& operator++() { ++myInt; for (int i = 0; i < myVector.size(); ++i) { ++myVector[i]; } return *this; } }; int main() { std::vector<int> vec1 = {1, 2, 3}; std::vector<int> vec2 = {4, 5, 6}; MyClass obj1(10, vec1); MyClass obj2(20, vec2); // 拷贝构造 MyClass obj3 = obj1; // +运算符重载 MyClass obj4 = obj1 + obj2; // =运算符重载 obj3 = obj4; // 输出流运算符重载 std::cout << obj3 << std::endl; // 自增运算符重载 ++obj3; std::cout << obj3 << std::endl; return 0; } ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值