当前位置: 首页 > news >正文

C++——定义一个复数类Complex,重载运算符“+”,使之能用于复数的加法。参加运算的两个操作数可以都是类对象,也可以一个是整数,其顺序任意。

没注释的源代码

#include <iostream>

using namespace std;

class Complex

{

public:

    Complex(){real=0;imag=0;}

    Complex(double r,double i){real=r;imag=i;}

    Complex operator+(Complex &c2)

    {

        Complex c;

        c.real=real+c2.real;

        c.imag=imag+c2.imag;

        return c;

    }

    double get_real()

    {

        return real;

    }

    double get_imag()

    {

        return imag;

    }

    Complex operator+(int &i)

    {

        Complex c;

        c.real=real+i;

        c.imag=imag;

        return c;

    }

    void display()

    {

        cout<<"("<<real<<","<<imag<<"i)"<<endl;

    }

    friend Complex operator+(int &i,Complex &c2);

private:

    double real;

    double imag;

};

Complex operator+(int &i,Complex &c2)

{

    Complex c;

    c.real=i+c2.real;

    c.imag=c2.imag;

}

int main()

{

    Complex c1(3,4),c2(5,-10),c3;

    int i=5;

    c3=c1+c2;

    c3.display();

    c3=i+c1;

    c3.display();

    c3=c1+i;

    c3.display();

    return 0;

}


http://www.mrgr.cn/news/53580.html

相关文章:

  • 反欺诈与数字信任:保障数字经济安全的关键
  • 衡石分析平台系统分析人员手册-应用空间
  • 【微知】RDMA IB verbs中的ABI是什么?作用是什么?(application binary interface、规范、兼容)
  • PCDN 技术如何优化网络延迟(壹)
  • 机械视觉光源选型
  • 解决mac ssh端终端只有黑白颜色的问题
  • 计算机前沿技术-人工智能算法-大语言模型-最新研究进展-2024-10-17
  • C++——有两个矩阵a和b,均为2行3列。求两个矩阵之和。重载运算符“+”,使之能用于矩阵相加。(如c=a+b)
  • 动销方案在合作伙伴场地执行,好处多多!
  • S7-1500 通过PN/PN Coupler 通信
  • Python | Leetcode Python题解之第493题翻转对
  • UniHttp 框架,请求http接口
  • 文章解读与仿真程序复现思路——电网技术EI\CSCD\北大核心《基于AGCN-LSTM模型的海上风电场功率概率预测 》
  • 15分钟学Go 第8天:控制结构 - 循环
  • 请问平库管理系统有哪些功能流程?
  • 高职单招如何报考与备考?这份指南为你解惑
  • 使用django-simple-captcha遇到的坑
  • 杨氏矩阵(有一个数字矩阵,矩阵的每行从左到右的递增的,矩阵从上到下是递增的请编写一个程序,在这样的矩阵中查找某个数字是否存在)
  • 2023年华为杯数学建模竞赛题F论文和代码
  • Python库matplotlib之十二