#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;
}
运算符的重载
最新推荐文章于 2018-11-10 15:48:18 发布