1 回答
TA贡献2条经验 获得超1个赞
#include <iostream>
using namespace std;
class CComplex
{
public:
CComplex()
{
real = 0.0;
imag = 0.0;
}
CComplex(double x, double y)
{
real = x;
imag = y;
}
CComplex operator +(CComplex obj1)
{
CComplex obj2;
obj2.real = this->real + obj1.real;
obj2.imag = this->imag + obj1.imag;
return obj2;
}
CComplex operator ++()
{
this->real += 1;
this->imag +=1;
return *this;
}
CComplex operator--()
{
real -=1;
imag -=1;
return *this;
}
void print()
{
cout<<real<<"+"<<imag<<"i"<<endl;
}
private:
double real;
double imag;
};
void main()
{
CComplex obj1(2.1,3.2);
CComplex obj2(3.6,2.5);
cout<<"obj1=";
obj1.print();
cout<<"obj2=";
obj2.print();
CComplex obj3 = obj1 + obj2;
cout<<"befor++, obj3=";
obj3.print();
++obj3;
cout<<"after++, obj3=";
obj3.print();
--obj3;
cout<<"after--, obj3=";
obj3.print();
CComplex obj4 = ++obj3;
cout<<"obj4=";
obj4.print();
}
- 1 回答
- 1 关注
- 1487 浏览
添加回答
举报