wl991001 2018-11-14 15:31 采纳率: 0%
浏览 1714
已采纳

C++类与对象 怎样利用复数类算复数的模等运算?

定义复数类,并在主函数定义类的对象进行调用测试。
要求:① 数据成员为双精度类型的实部和虚部。
② 具有无参和有参的构造函数。
③ 具有深拷贝构造函数。
④ 具有析构函数。
⑤ 具有输出函数、求模函数、加法函数等。

  • 写回答

2条回答

  • threenewbee 2018-11-14 19:11
    关注
    #include <iostream>
    #include <math.h>
    
    using namespace std;
    
    class Complex
    {
    private:
        double real;
        double img;
    public:
        Complex() { real = img = 0.0; }
        Complex(double r, double i) { real = r; img = i; }
        Complex(const Complex &c) { real = c.real; img = c.img; }
        ~Complex() { cout << "dector has been called!" << endl; }
        void Output() { cout << real << "+" << img << "i" << endl; }
        Complex operator +(const Complex &c) { return Complex(real + c.real, img + c.img); }
        double GetModulus() { return sqrt(img * img + real * real); }
    };
    int main()
    {
        Complex c1, c2(1.0, 2.0), c3(1.5, 2.5);
        c1 = c2 + c3;
        c1.Output();
        Complex c4 = c1;
        c4.Output();
        cout << c4.GetModulus() << endl;
        return 0;
    }
    

    图片说明

    参考资料:https://en.wikipedia.org/wiki/Complex_number

    如果问题得到解决,请点我回答左上角的采纳和向上的箭头

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!