重温操作符重载(二)

#include<iostream>
#include<math.h>
#include<string>

using namespace std;
/************************************************************************/
/* 要先对Complex和friend函数进行声明,不然会报错*/
/************************************************************************/
class Complex;
Complex operator+( Complex &, Complex &);
Complex operator-( Complex &,  Complex &);
Complex operator*( Complex &,  Complex &);
Complex operator/( Complex &,  Complex &);

class Complex
{
public:
	Complex();
	Complex(double);
	Complex(double, double);
	Complex(double, double, string);
	void setReal(double);
	void setImag(double);
	void setComplex(double, double);
	void setName(string);

	double getReal();
	double getImag();
	Complex getComplex();
	string getName();

	void showComplex();

	friend Complex operator+( Complex &,Complex &);
	friend Complex operator-( Complex &,  Complex &);
	friend Complex operator*( Complex &,  Complex &);
	friend Complex operator/( Complex &,  Complex &);

	friend ostream & operator<<(ostream & out,  Complex &);
	/************************************************************************/
	/* cin >> i; 可以翻译为 cin.operator>>(i);
	同理cout << i;   cout.operator<<(i);*/
	/************************************************************************/
private:
	double real;
	double imag;
	string name;
};

Complex::Complex()
{
	real = 0.0;
	imag = 0.0;
	name = "complex";
}

Complex::Complex(double r)
{
	real = r;
	imag = 0.0;
	name = "complex";
}

Complex::Complex(double curReal, double curImag)
{
	real = curReal;
	imag = curImag;
	name = "complex";
}

Complex::Complex(double curReal, double curImag, string newName)
{
	real = curReal;
	imag = curImag;
	name = newName;
}

double Complex::getReal()
{
	return real;
}

double Complex::getImag()
{
	return imag;
}

Complex Complex::getComplex()
{
	return Complex(real, imag);
}

string Complex::getName()
{
	return name;
}

void Complex::setReal(double curReal)
{
	real = curReal;
}

void Complex::setImag(double curImag)
{
	imag = curImag;
}

void Complex::setComplex(double r, double i)
{
	real = r;
	imag = i;
}

void Complex::setName(string newName)
{
	name = newName;
}

void Complex::showComplex()
{
	cout << real << " + " << imag << "i" << endl;
}

inline Complex operator+ (Complex &a,  Complex &b)
{
	double newReal = a.getReal() + b.getReal();
	double newImag = a.getImag() + b.getImag();
	Complex newComplex(newReal, newImag);
	return newComplex;
}

Complex operator- (Complex &a,  Complex &b)
{
	double newReal = a.getReal() - b.getReal();
	double newImag = a.getImag() - b.getImag();
	Complex newComplex(newReal, newImag);
	return newComplex;
}

Complex operator* (Complex &a,  Complex &b)
{
	double newReal = a.getReal()*b.getReal() - a.getImag()*b.getImag();
	double newImag = a.getReal()*b.getImag() + a.getImag()*b.getReal();
	Complex newComplex(newReal, newImag);
	return newComplex;
}

Complex operator/ (Complex &a,  Complex &b)
{
	double abs_sq = b.getReal()*b.getReal() + b.getImag()*b.getImag();
	double newReal = (a.getReal()*b.getReal() + a.getImag()*b.getImag())/abs_sq;
	double newImag = (a.getImag()*b.getReal() - a.getReal()*b.getImag())/abs_sq;
	Complex newComplex(newReal, newImag);
	return newComplex;
}

ostream operator<<(ostream &out,  Complex &c)
{
	return out << c.getName() << ": " << c.getReal() << " + " << c.getImag() << "i" << endl;
}

void test()
{
	Complex c1;
	c1.setName("c1");
	c1.showComplex();

	Complex c2(1.2);
	c2.setName("c2");
	c2.showComplex();

	Complex c3(3.4, 5.6);
	c3.setName("c3");
	c3.showComplex();
	
	Complex c4(2, 4, "c4");
	c4.showComplex();

	c1 = c2 + c3;
	c1.showComplex();

	c1 = c2 - c3;
	c1.showComplex();

	c1 = c2*c3;
	c1.showComplex();

	c1 = c2/c3;
	c1.showComplex();
}

int main()
{
	test();
	return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值