点击支付下载即可看见答案
直接提交即可,无需任何更改

上机实验9:操作符重载的应用
一、实验内容
下面程序定义了复数类Complex的3个对象:Complex u(10, 20), v(5,-4), s;
在程序中通过s = u + v;来计算u和v的和,并将结果存入s中,填补缺失的语句并调试出正确运行结果。
#include <iostream>
using namespace std;
class Complex
{private:
    double real, imag; //real、imag分别保存复数的实部、虚部
public:
    Complex() { real = 0; imag = 0;}
    Complex(double r, double i) {real = r; imag = i; }
    void display()
    { cout<< “(“<< real;
        if (imag >0)cout<< “+” << imag << “i)”;
        else if (imag<0) cout<< imag << “i)”;
    }
    // 通过成员函数重载运算符“+”
};
void main()
    { Complex u(10, 20), v(5,-4), s;
    s = u + v; // 使用重载的运算符“+”
    u.display(); cout<<“+ “;
    v.display(); cout<<“= “;
    s.display(); cout<<endl;
}
二、实验要求
在Complex类中编写重载运算符“+”的函数,保证语句的用法正确并显示复数运算结果。
三、实验结果截图
(请将实际上机运行的结果截图并上传)
本站可能内容不全,更多内容及资料请添加客服微信了解