运算符的重载

#include<iostream>
using namespace std;

class Int;
ostream& operator<<(ostream &out,const Int &I);

class Int
{
public:
	Int(int i = 0):m_i(i)
	{}
	~Int()
	{}
public:
	friend ostream& operator<<(ostream &out,const Int &I);
	Int& operator++()//前置++
	{
		++m_i;
		return *this;
	}
	Int operator++(int)//后置++
	{
		//Int tmp =*this;
		//m_i++;
		//return *this;
		return ++*this;//调用前置++
	}
	Int& operator--()//前置--
	{
		--m_i;
		return *this;
	}
	Int operator--(int)//后置--
	{
		//Int tmp=*this;
		//m_i--;
		//return *this;
		return --*this;//调用后置--
	}
	Int operator+(const Int &I)//加
	{
		return m_i+I.m_i;
	}
	Int operator-(const Int &I)//减
	{
		return m_i-I.m_i;
	}
	Int operator*(const Int &I)//乘
	{
		return m_i * I.m_i;
	}
	Int operator/(const Int &I)//除
	{
		return m_i /I.m_i;
	}
	Int operator&(const Int &I)//与
	{
		return m_i & I.m_i;
	}
	Int operator|(const Int &I)//或
	{
		return m_i | I.m_i;
	}
	Int operator^(const Int &I)//异或
	{
		return m_i ^I.m_i;
	}
	Int operator~()//按位取反
	{
		return ~m_i;
	}
	Int operator!()//非
	{
		return !m_i;
	}
	Int operator+()//正号
	{
		return m_i;
	}
	Int operator-()//负号
	{
		return -m_i;
	}
private:
	int m_i;
};

ostream& operator<<(ostream &out,const Int &I)
{
	out<<I.m_i;
	return out;
}
void main()
{
	Int i(1);
	i = i+1;
	cout<<i<<endl;
	i = i-1;
	cout<<i<<endl;
	
	Int x = ++i;
	cout<<x<<endl;
	 x =i++;
	 cout<<x<<endl;
     x = --i;
	 cout<<x<<endl;
	 x =i--;
	 cout<<x<<endl;

	 Int a,b,c;
	 b =10;
	 c=20;
	 a = b+c; 
	 cout<<a<<endl;
	 a = b-c;
	 cout<<a<<endl;
	 a = b*c;
	 cout<<a<<endl;
	 a = b/c;
	 cout<<a<<endl;

	 a = b & c;
	 cout<<a<<endl;
	 a = b | c;
	 a = b ^ c;
	 a = ~b;
	 a = !b;
	 cout<<a<<endl;
	 a = 6;
	 a = +a;
     a = -a;
	 cout<<a<<endl;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值