hw12_02.cpp

#include <iostream>

using namespace std;

class Complex
{
public:
	Complex(double newx=0, double newy=0) { r = newx; i = newy; }

	friend Complex operator+(const Complex& ,const Complex& ) ;
	void Display() {
		if (i > 0)
			cout << r << '+' << i << 'i' << endl;
		else if (i < 0)
			cout << r << i << 'i' << endl;
		else
			cout << r << endl;
	}
private:
	double r, i;
};
Complex operator+(const Complex& a, const Complex& b) {
	return Complex(a.r + b.r, a.i + b.i);
}
int main()
{
	Complex a(2.1,5.7), b(7.5,8), c, d,e;
	c = a + b;
	d = b + 5.6;
	e = 4.1 + a;
	c.Display();
	d.Display();
	e.Display();
	return 0;
}

发表评论

您的邮箱地址不会被公开。 必填项已用 * 标注

滚动至顶部