// 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;
}
#endifThe complex class
最新推荐文章于 2023-04-15 23:54:39 发布
本文介绍了一个简单的复数类的实现,包括复数的基本运算如加法,并重载了运算符以支持复数间的操作。同时实现了复数的前缀和后缀递增操作,并提供了打印复数的方法。
3018

被折叠的 条评论
为什么被折叠?



