hw12_01.cpp

#include <iostream>

using namespace std;

class Complex 
{
      public:
		Complex(double newx=0, double newy=0) { r = newx; i = newy; }
		Complex operator+(const Complex& c) const { return Complex(r + c.r, i + c.i); }
		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;
};

int main() 
{
	Complex a(2.1,5.7), b(7.5,8), c, d;
	c = a + b;
	d = b + 5.6;
    c.Display();
    d.Display();
    return 0; 
}

发表评论

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

滚动至顶部