#include
using namespace std;
class complex
{
public:
complex(float r,float i);
complex operator++(); //前置单目运算符
complex operator++(int); //后置单目运算符
complex operator--();
complex operator--(int);
void show()
{
if(image>=0)
{
cout<<real<<"+"<<image<<"i"<<endl;
}
else
{
cout<<real<<"-"<<-image<<"i"<<endl;
}
}
private:
float real;
float image;
};
complex::complex(float r,float i)
{
real=r;
image=i;
}
complex complex::operator++() //前置
{
real++;
image++;
return *this; //this指针指向对象
}
complex complex::operator++(int) //后置
{
complex old=*this;
++(*this);
return old;
}
complex complex::operator--() //前置
{
real--;
image--;
return *this; //this指针指向对象
}
complex complex::operator--(int) //后置
{
complex old=*this;
++(*this);
return old;
}
int main()
{
complex c1(2,3)
(++c1).show();
complex c2(5,5)
(--c2).show();
}
应该是重载的自增自减运算符有点问题,别写成后置++和--,你可以在搜一搜它的运行机制,后置--也写错了,写成++了