#include<iostream>
using namespace std;
class Complex
{
protected:
double real; //复数的实部
double image; //复数的虚部
public:
Complex(double r = 0.0, double i = 0.0){real = r; image = i;}
void output();
Complex& operator ++(); //前置增1运算符函数
Complex operator ++(int); //后置增1运算符函数
};
void Complex::output()
{
if (image < 0.0)
cout<<"("<<real<<image<<"i)";
else
cout<<"("<<real<<"+"<<image<<"i)";
}
Complex& Complex::operator++()
{
++real;
return *this;
}
Complex Complex::operator++(int)
{
return Complex(real++, image);
}
int main()
{
Complex a(2.0, 3.0), b(3.0, 2.0);
cout<<"++a: ";
(++a).output();
cout<<"; after ++a, a is: ";
a.output();
cout<<endl;
cout<<"b++: ";
(b++).output();
cout<<"; after b++, b is: ";
b.output();
cout<<endl;
return 0;
}
陪伴而非守候
阿波罗的战车
精慕HU
郎朗坤
相关分类