运算符重载练习(一)

Code:
  1. #include <iostream>   
  2. using namespace std;   
  3. class one   
  4. {   
  5. public:   
  6.   one(){}   
  7.   one(int i):b(i){}   
  8.   const one& operator++()   
  9.   {   
  10.    ++b;   
  11.    return *this;   
  12.   }   
  13.   const one operator++(int)   
  14.   {   
  15.    one ret(b);   
  16.    ++b;   
  17.    return ret;   
  18.   }   
  19.   const one& operator--()   
  20.   {   
  21.    --b;   
  22.    return *this;   
  23.   }   
  24.   const one operator--(int)   
  25.   {   
  26.    one ret(b);   
  27.    --b;   
  28.    return ret;   
  29.   }   
  30.   friend const one operator+(const one& left,const one& right)   
  31.   {   
  32.    return one(left.b + right.b);   
  33.   }   
  34.   friend const one operator-(const one& left,const one& right)   
  35.   {   
  36.    return one(left.b - right.b);   
  37.   }   
  38.   friend ostream& operator<<(ostream& os,const one& right)   
  39.   {   
  40.      return os << right.b;    
  41.   }   
  42. private:   
  43.   int b;   
  44. };   
  45. int main()   
  46. {   
  47.  one tone(5);   
  48.  ++tone;   
  49.  cout << tone << endl;   
  50.  tone++;   
  51.  cout << tone << endl;   
  52.  --tone;   
  53.  cout << tone << endl;   
  54.  tone--;   
  55.  cout << tone << endl;   
  56.  one a(5),b(6),c(7);   
  57.  cout << a+b-c << endl;   
  58.  return 0;   
  59. }   

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值