zoukankan      html  css  js  c++  java
  • 自考新教材--p154

    源程序:

    //

    //  main.cpp

    //  p154

    //

    //  Created by duanqibo on 2019/12/18.

    //  Copyright © 2019年 duanqibo. All rights reserved.

    //

    #include <iostream>

    using namespace std;

    class myComplex

    {

    private:

        double real,imag;

    public:

        myComplex()

        {

            real=0;

            imag=0;

        }

        myComplex(double r,double i)

        {

            real=r;

            imag=i;

        }

        void outCom();

        myComplex operator-(const myComplex &c);

        

        friend myComplex operator+(const myComplex &c1,const myComplex &c2);

        friend myComplex operator-(const myComplex &c1,const myComplex &c2);

        friend myComplex operator-(const myComplex &c1,double r);

        friend myComplex operator-(double r,const myComplex &c1);

    };

    myComplex operator+(const myComplex &c1,const myComplex &c2)

    {

        return myComplex(c1.real+c2.real,c1.imag+c2.imag);

    }

    myComplex operator-(const myComplex &c1,const myComplex &c2)

    {

        return myComplex(c1.real-c2.real,c1.imag-c2.imag);

    }

    myComplex operator-(const myComplex &c1,double r)

    {

        return myComplex(c1.real-r,c1.imag);

    }

    myComplex operator-(double r,const myComplex &c1)

    {

        return myComplex(r-c1.real-r,-c1.imag);

    }

    void myComplex::outCom()

    {

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

    }

    int main(int argc, const char * argv[])

    {

        // insert code here...

        myComplex c1(1,2),c2(3,4),res;

        c1.outCom();

        cout<<"operator+";

        c2.outCom();

        cout<<"=";

        res=c1+c2;

        res.outCom();

        cout<<endl;

        res=c1-5;

        res.outCom();

        cout<<endl;

        res=5-c1;

        res.outCom();

        cout<<endl;

        return 0;

    }

     运行结果:

  • 相关阅读:
    Vscode 隐藏 工作区中的目录
    java 中 静态泛型方法书写
    Vscode 配置 maven debug
    vscode 配置 java utf-8 编码
    node.js 设置 淘宝 镜像
    vscode 注册表
    ESET Smart Security 6 – 免费60天(SG)
    WIN-8“内置管理员无法激活此应用”问题
    怎样更新PE内的工具
    使用Setup安装Windows8 RTM方法
  • 原文地址:https://www.cnblogs.com/duanqibo/p/12060521.html
Copyright © 2011-2022 走看看