【C++】复数类

#include
using namespace std;

class Complex
{
public:
	void Display()
	{
		cout<<"Real:"<<_real<<"Image:"<<_image<_real = d._real;
			this->_image = d._image;
		}
		return *this;
	}

	Complex operator +(Complex& c)		//声明成员函数重载运算符"+"
	{
		cout<<"operator +(Complex& c)"<_real + c._real;
		temp._image = this->_image + c._image;

		return temp;
	}
	Complex operator -(Complex& c)
	{
		cout<<"operator -(Complex& c)"<_real - c._real;
		temp._image = this->_image - c._image;
		return temp;
	}

	Complex operator *(Complex& c)
	{
		cout<<"operator *(Complex& c)"<_real*c._real - this->_image * c._image;
		temp._image = this->_real *c._image + this->_image * c._real;

		return temp;
	}

	Complex operator /(Complex& c)
	{
		cout<<"operator -(Complex& c)"<_real*c._real + this->_image * c._image)/
			(c._real*c._real + c._image * c._image);
		temp._image = (this->_image * c._real -this->_real *c._image)/
			(c._real*c._real + c._image * c._image);

		return temp;
	}
	Complex& operator++()	//前置++
	{
		this->_real++;

		return *this;
	}

	Complex operator++(int) //后置++
	{
		Complex temp(*this);

		this->_real++;
		return temp;
	}

	Complex& operator--()	//前置--
	{
		this->_real--;

		return *this;
	}
	Complex operator--(int) //后置--
	{
		Complex temp(*this);

		this->_real--;
		return temp;
	}

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

		return *this;
	}

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

		return *this;
	}

private:
	double _real;
	double _image;
};


void TestComplex ()
{
	Complex c1(2.2, 1.1);

	Complex c2 = c1++;
	c1.Display();
	c2.Display();
}


int main()
{
	Complex c1(2.2,1.1);
	c1.Display ();
	
	Complex c2(c1);		//调用拷贝构造函数
	c2.Display();
	
	Complex c3;		//赋值运算符的重载
	c3 = c2;
	c3.Display();

	Complex c4 = c1 + c2;
	c4.Display();

	Complex c5 = c3 - c2;
	c5.Display();

	Complex c6 = c3 * c2;
	c6.Display();

	Complex c7(c1);
	c7++;
	c7.Display();

	Complex c8;
	c8 += c1;
	c8.Display();

	getchar();
	return 0;
}

你可能感兴趣的:(构造函数,拷贝构造函数,赋值运算符的重载,析构函数,C++)