1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
| #include<iostream> using namespace std; class complex { private: double real; double imag; public: complex(double r=0,double i=0):real(r),imag(i){} complex operator + (complex &c) {return complex(c.real+real,c.imag+imag);} complex operator - (complex &c) {return complex(real-c.real,imag-c.imag);} void display() { cout<<"该复数为"<<imag<<"i+"<<real<<endl;
} };
int main() { complex a(1,2); complex b(2,3),c(0,0); a.display(); c=a-b; c.display(); return 0; }
|