// File name : complex.h
#ifndef _COMPLEX_H_
#define _COMPLEX_H_
#include <iostream>
using namespace std;
class complex {
public:
complex(double x = 0, double y = 0) {
real = x;
imag = y;
}
complex(complex &src) {
real = src.real;
imag = src.imag;
}
~complex() {}
complex operator+(const complex& src) const{
return complex(real + src.real, imag + src.imag);
}
complex& operator=(const complex& src) {
real = src.real;
imag = src.imag;
return (*this);
}
void operator++(int) {
cout << "call post++"<<endl;
imag = imag + 1;
}
void operator++() {
cout << "call pre++"<<endl;
real = real + 1;
}
void print(ostream& out) {
if(real != 0) out<<real;
if(imag > 0) out<<"+"<<imag<<"j";
else if(imag < 0) out<<imag<<"j";
if(real == 0 && imag == 0) out<<"0";
}
private:
double real;
double imag;
};
ostream& operator<<(ostream& out, complex& obj) {
obj.print(out);
return out;
}
#endif
The complex class
最新推荐文章于 2021-11-02 11:54:13 发布