#include
using namespace std;
class complex
{
double real,imag;
public:
complex(double r=0,double i=0)
{
real=r;
imag=i;
}
const complex operator--(int)
{
complex temp(*this);
--(*this);
return temp;
}
const complex operator--()
{
--real;--imag;
return *this;
}
void display();
};
void complex::display()
{
if(imag>=0)
{
cout<<"("<<real<<"+"<<imag<<"i)"<<endl;
}
else
cout<<"("<<real<<imag<<"i)"<<endl;
}
int main()
{
complex c1(1,1);
cout<<"c1=";c1.display();
--c1;
cout<<"--c1,c1=";c1.display();
c1--;
cout<<"c1--,c1=";c1.display();
return 0;
}
运行结果:
c1=(1+1i)
--c1,c1=(0+0i)
c1--,c1=(-1-1i)
我预想的结果应该是c1--,c1=(0+0i),不知道错在哪里?求解