/************************************************************************/
/*
注意实现前缀++ 与后缀++的区别
下面分别用两种方法实现 ,一个是友员函数,一个是面员函数;
*/
/************************************************************************/
#include <iostream>
using namespace std;
class complex
{
int a, b;
public:
complex(int a,int b):a(a),b(b){}
void operator ++(int) { a++;b++; }
void operator ++();
friend complex & operator--(complex &com);
friend complex & operator--(complex &com,int)
{
com.a--;
com.b--;
return com ;
}
void Show()
{
cout << "("<<a<<","<<b<<")" <<endl;
}
};
void complex::operator++() { a+=10;b+=10 ; }
complex &operator--(complex &com)
{
com.a-=10;
com.b-=10;
return com;
}
void main()
{
complex com1(1,5),com2(com1);
com1++ ;
++com2 ;
com1.Show();
com2.Show();
--com1; //在VC6.0中可以当做 postfix(后缀)用使,只有一个warning 没有error;在g++ 编译器中是不能通过。
com2--;
com1.Show();
com2.Show() ;
}
运行结果:
(2,6)
(11,15)
(-8,-4)
(10,14)
Press any key to continue