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;

    }

     运行结果:

  • 相关阅读:
    java中的单例模式
    数组的冒泡排序
    2019年总结—即将而立之年的90后
    圣诞节开启博客之旅
    分布式多线程的Lock示例
    抽象工厂模式
    观察者模式
    建造者模式
    外观模式(Facade)
    模板方法模式
  • 原文地址:https://www.cnblogs.com/duanqibo/p/12060521.html
Copyright © 2011-2022 走看看