复数类

 实现一个复数类,实现以下成员函数
//class Complex
//{
//public:
//	// 带缺省值的构造函数
//	Complex(double real = 0, double image = 0);
//	// 析构函数
//	~Complex();
//	// 拷贝构造函数
//	Complex(const Complex& d);
//	// 赋值运算符重载
//	Complex& operator= (const Complex& d);
//	void Display();
//
//public:
//	Complex& operator++();
//	Complex operator++(int);
//	Complex& operator--();
//	Complex operator--(int); //后置--
//
//	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);
//
//private:
//	double _real;		// 实部
//	double _image;		// 虚部
//};
//
//void TestComplex()
//{
//	Complex c1(2.2, 1.1);
//}




#include<iostream>
using namespace std;
class Complex
{
public:
	Complex(double real = 0, double image = 0);
	~Complex();
	Complex(const Complex& d);
	Complex& operator++();
	Complex operator++(int);
	Complex& operator--();
	Complex operator--(int); //后置--
	Complex& operator= (const Complex& d);
	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(double real, double image)
{
	cout << "构造函数:" << endl;
	_real = real;
	_image = image;
}
Complex::~Complex()
{
	cout << "析构函数:" << endl;
}
Complex::Complex(const Complex& d)
{
	cout << "拷贝构造函数:" << endl;
	_real = d._real;
	_image = d._image;
}
Complex& Complex::operator= (const Complex& d)
{
	cout << "赋值运算符:" << endl;
	_real = d._real;
	_image = d._image;
	return *this;
}
void Complex::Display()  //显示
{
	cout << _real;
	if (_image > 0)
	{
		cout << "+";
	}
	cout<< _image << "i" << endl;
}
Complex& Complex::operator++()
{
	cout << "前置++:" << endl;
	++_real;
	++_image;
	return *this;
}
Complex Complex::operator++(int)
{
	cout << "后置++:" << endl;
	Complex tmp(*this);
	_real++;
	_image++;
	return tmp;
}
Complex& Complex::operator--()
{
	cout << "前置--:" << endl;
	--_real;
	--_image;
	return *this;
}
Complex Complex::operator--(int)
{
	cout << "后置--:" << endl;
	Complex tmp(*this);
	_real--;
	_image--;
	return tmp;
}

Complex Complex::operator+(const Complex& c)
{
	cout << "加法运算符重载:" << endl;
	Complex tmp;
	tmp._real = _real + c._real;
	tmp._image = _image + c._image;
	return tmp;
}
Complex Complex::operator-(const Complex& c)
{
	cout << "减法运算符重载:" << endl;
	Complex tmp;
	tmp._real = _real -c._real;
	tmp._image = _image - c._image;
	return tmp;
}
Complex& Complex::operator+=(const Complex& c)
{
	cout << "+=运算符重载:" << endl;
	/*_real = _real + c._real;
	_image = _image + c._image;
	return *this;*/
	return (*this) = (*this + c);
}
Complex& Complex::operator-=(const Complex& c)
{
	cout << "-=运算符重载:" << endl;
	/*_real = _real - c._real;
	_image = _image - c._image;
	return *this;*/
	return (*this) = (*this - c);
}
Complex Complex::operator*(const Complex& c)
{
	cout << "*运算符重载:" << endl;
	Complex tmp;
	tmp._real = _real*c._real - _image*c._image;
	tmp._image = _image*c._real + _real*c._image;
	return tmp;
}
Complex Complex::operator/(const Complex& c)
{
	cout << "/运算符重载:" << endl;
	Complex tmp;
	double t = c._real*c._real + c._image*c._image;
	tmp._real = (_real*c._real+ _image*c._image)/t;
	tmp._image = (_image*c._real - _real*c._image)/t;
	return tmp;
}
int main()
{
	Complex op1(2.2, 1.1);
	Complex op2(1.5, 2.6);
	op1.Display();
	op1 += op2;
	op1.Display();
	/*op1.Display();
	Complex op2(op1);
	op2.Display();
	Complex op3;
	op3 = op1;
	op3.Display();
	Complex op4;
	op4=op1.operator+(op1);
	op4.Display();
	Complex op5;
	op5 = op4.operator-(op1);
	op5.Display();
	Complex op6;
	Complex op7(5.2, 3.6);
	op1 = op1.operator-=(op7);
	op1.Display();
	Complex op8(2.3, 4.6);
	Complex op9(3.6,2.8);
	Complex op10;
	op10 = op8.operator*(op9);
	op10.Display();
	op10 = op8.operator/(op9);
	op10.Display();*/
	//op1 = op1.operator--();
	/*op1.Display();
	Complex op2;
	op2 = op1++;
	op2.Display();
	op1.Display();*/
	system("pause");
	return 0;
}

评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值