m0_62626383 2022-04-17 22:01 采纳率: 40%
浏览 74
已结题

help这个代码该怎么编写呢

img

img


希望大家能帮我看看最后c3,c4怎么样才能这样输出呢?不能构造display或者show函数,看看这个代码的要求呢

  • 写回答

3条回答 默认 最新

  • Kiloveyousmile 2022-04-17 22:16
    关注

    这个要用到运算符重载,网上搜索关键词 “C++ operator”即可找到答案。

    参考:

    #include <iostream>
    
    using namespace std;
    
    class CComplex {
        double a;
        double b;
    public:
        CComplex() {
            this->a = 0;
            this->b = 0;
        };
        CComplex(double a, double b);
        CComplex operator+(const CComplex &A) const;
        CComplex operator-(const CComplex &A) const;
        friend istream & operator>> (istream &in, CComplex &c);
        friend ostream & operator<< (ostream &ou, const CComplex &c);
    };
    CComplex::CComplex(double a, double b) {
        this->a = a;
        this->b = b;
    }
    
    CComplex CComplex::operator+(const CComplex &A) const {
        CComplex c;
        c.a = this->a + A.a;
        c.b = this->b + A.b;
        return c;
    }
    
    CComplex CComplex::operator-(const CComplex &A) const {
        CComplex c;
        c.a = this->a - A.a;
        c.b = this->b - A.b;
        return c;
    }
    
    istream &operator>>(istream &in, CComplex &c) {
         in >> c.a >> c.b;
         return in;
    }
    ostream &operator<<(ostream &ou, const CComplex &c) {
        ou << noshowpos << c.a << showpos << c.b << "i" <<  endl;
        return ou;
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论 编辑记录
查看更多回答(2条)

报告相同问题?

问题事件

  • 系统已结题 4月26日
  • 已采纳回答 4月18日
  • 创建了问题 4月17日

悬赏问题

  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私