复数类的完善版

#include<iostream>
using  namespace std;
class Complex
{
	// 构造函数(无参、带参、带缺省值、半缺省值的构造函数都去练练 )
	// 析构函数
	// 拷贝构造函数
	// 赋值运算符重载
public:
	/*Complex()
	{
	_real = 1.0;
	_image = 1.0;
	cout << "构造函数" << endl;
	}
	Complex(double real, double  image)
	{
	_real = real;
	_image = image;
	}
	Complex(double  real, double image = 1.0)
	{
	_real = real;
	_image = image;
	}*/
	Complex(double  real = 1.0, double image = 1.0)
	{
		_real = real;
		_image = image;
	}

	Complex(Complex& a)//拷贝构造函数
	{
		_image = a._image;
		_real = a._real;
		//	cout << "复制构造" << endl;
	}

	Complex& operator =(const Complex& c)//&表引用,有返回值可以实现链式访问
	{
		if (this != &c)//&表取地址
		{
			this->_real = c._real;
			this->_image = c._image;
		}
		return *this;

	}

	~Complex()
	{
		//cout << "析构函数" << endl;
	}
	Complex& operator ++();    // 前置 ++
	Complex operator ++(int);  // 后置++
	Complex& operator --();   // 前置 -
	Complex operator --(int); // 后置-
	bool operator >(const Complex& c);
	bool operator >=(const Complex& c);
	bool operator <(const Complex& c);
	bool operator <=(const Complex& c);
	bool operator ==(const Complex& c);

	Complex operator +(const Complex& c);
	Complex operator -(const Complex& c);
	Complex& operator -=(const Complex& c);
	Complex& operator +=(const Complex& c);
	Complex operator *(const Complex& c);
	Complex operator /(const Complex& c);
	void  Display();
private:
	double  _real;
	double  _image;
};
Complex& Complex:: operator++()
{
	++this->_real;
	++this->_image;
	return *this;
}
Complex  Complex::operator++(int)//int是个标记表示后置++,只作为函数重载特征
{
	Complex tmp;
	tmp._real = this->_real++;
	tmp._image = this->_image++;
	return  tmp;
}
Complex&  Complex:: operator--()
{
	--this->_real;
	--this->_image;
	return *this;
}
Complex  Complex::operator--(int)//int是个标记表示后置--,只作为函数重载特征
{
	Complex tmp;
	tmp._real = this->_real--;
	tmp._image = this->_image--;
	return  tmp;
}

bool Complex::operator >(const Complex& c)
{
	return sqrt((this->_real)*(this->_real) + (this->_image)*(this->_image)) > sqrt((c._real)*(c._real) + (c._image)*(c._image));
}
bool Complex::operator >=(const Complex& c)
{
	return  (*this > c) || (*this == c);
}
bool Complex::operator <(const Complex& c)
{
	return !(*this>c);
}
bool Complex::operator <=(const Complex& c)
{
	return  (*this < c) || (*this == c);
}
bool Complex::operator ==(const Complex& c)
{
	return sqrt((this->_real)*(this->_real) + (this->_image)*(this->_image)) == sqrt((c._real)*(c._real) + (c._image)*(c._image));
}

Complex  Complex::operator+(const Complex &c)
{
	Complex tmp;
	tmp._real=this->_real + c._real;
	tmp._image = this->_image +c._image;


	return tmp;
}
Complex  Complex::operator-(const Complex &c)
{
	Complex tmp;
	tmp._real = this->_real -c._real;
	tmp._image = this->_image - c._image;

	return tmp;
}

Complex &   Complex::operator+=(const Complex & c)
{
	this->_real += c._real;
	this->_image += c._image;
	return *this;
}
Complex &  Complex:: operator-=(const Complex & c)
{
	this->_real -= c._real;
	this->_image -= c._image;
	return *this;
}

Complex  Complex:: operator*(const Complex &c)
{
	//(a+bi)(c+di)=(ac-bd)+(bc+ad)i;
	Complex tmp;
	tmp._real = (this->_real*c._real) - (this->_image*c._image);
	tmp._image = (this->_image*c._real) + (this->_real*c._image);
	return tmp;
}
Complex  Complex:: operator/(const Complex &c)
{
	//(a+bi)/(c+di)=x+yi,x=(ac+bd)/(c*c+d*d),y=(bc-ad)/(c*c+d*d)
	Complex tmp;
	tmp._real = ((this->_real*c._real) + (this->_image*c._image)) / (c._real*c._real + c._image*c._image);
	tmp._image = ((this->_image*c._real) - (this->_real*c._image)) / (c._real*c._real + c._image*c._image);
	return tmp;
}
void  Complex::Display()
{
	cout << "real:" << _real;
	cout << " image:" << _image << endl;
}

void  Test1()
{
	Complex a, b(2,3);
	a.Display();
	b.Display();

	/*Complex ret = a + b;
	ret.Display();*/
	
	cout << (a < b) << endl;

}

int  main()
{
	Test1();
	system("pause");
	return 0;
}

注意加减运算后不能改变要运算的复数!!(不能改变this指针,应定义临时变量)

注重函数的复用,>,>=,<,<=,==减少代码的重写。

你可能感兴趣的:(复数类完整版)