您的位置:首页 > 其它

The complex class

2013-04-21 16:44 155 查看
// 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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: