Chinaunix首页 | 论坛 | 博客
  • 博客访问: 291556
  • 博文数量: 43
  • 博客积分: 628
  • 博客等级: 上士
  • 技术积分: 392
  • 用 户 组: 普通用户
  • 注册时间: 2011-08-30 18:11
文章分类

全部博文(43)

文章存档

2014年(1)

2013年(8)

2012年(11)

2011年(23)

分类: C/C++

2011-10-14 17:15:36

#include

using namespace std;

class INT

{

friend ostream & operator << (ostream &, const INT &);

public:

INT(){ val = 0; }

INT(const INT & c){ val = c.val; }

INT(const int & t){ val = t; }

INT operator + (const INT &);

INT & operator ++ (); //prefix ++

INT & operator -- (); //prefix --

const INT operator ++(int); //postfix ++

const INT operator --(int); //postfix --

double getVal() const { return val; }

void setVal(const int &d) { val = d; };

private:

int val;

};


ostream & operator << (ostream & output, const INT & c)

{

output<

return output;

}

INT INT :: operator + (const INT & c)

{

return INT(c.val + this->val);

}

INT & INT :: operator ++ ()

{

++(this->val);

return *this;

}

const INT INT :: operator ++(int)

{

INT temp(*this);

++(this->val);

return temp;

}

INT & INT :: operator -- ()

{

--(this->val);

return *this;

}

const INT INT :: operator --(int)

{

INT temp(*this);

--(this->val);

return temp;

}


int main(void)

{

INT c1;

cout<

c1.setVal(2);

INT c2;

cout<

c2.setVal(3);

cout<

c1++;

cout<

++c1;

cout<


return 0;

}

阅读(1763) | 评论(0) | 转发(0) |
0

上一篇:可重入与不可重入

下一篇:条件编译

给主人留下些什么吧!~~