wl991001 2018-11-15 05:13 采纳率: 0%
浏览 552
已采纳

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

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

  • 写回答

1条回答

  • threenewbee 2018-11-15 05:24
    关注
    #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); }
        Complex operator -(const Complex &c) { return Complex(real - c.real, img - c.img); }
        Complex operator *(const Complex &c) { return Complex(real * c.real - img * c.img, img * c.real + real * c.img); }
        Complex operator /(const Complex &c) { return Complex((real * c.real + img * c.img) / (c.real * c.real + c.img * c.img), (img * c.real - real * c.img) / (c.real * c.real + c.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;
        c4 = c2 - c3;
        c4.Output();
        c4 = c2 * c3;
        c4.Output();
        c4 = c2 / c3;
        c4.Output();
        return 0;
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 微信会员卡接入微信支付商户号收款
  • ¥15 如何获取烟草零售终端数据
  • ¥15 数学建模招标中位数问题
  • ¥15 phython路径名过长报错 不知道什么问题
  • ¥15 深度学习中模型转换该怎么实现
  • ¥15 HLs设计手写数字识别程序编译通不过
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向
  • ¥15 如何用python向钉钉机器人发送可以放大的图片?