C++ operator

#ifndef CComplex_hpp

#define CComplex_hpp

class CComplex{

private:

    double m_fReal;

    double m_fImag;

    

public:

    CComplex(){

        m_fReal = m_fImag;

    }

    CComplex(double fReal,double fImag = 0){

        m_fReal = fReal;

        m_fImag = fImag;

    }

    ~CComplex(){

        

    }

    

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

    friend CComplex &operator++(CComplex &c1);

    friend CComplex &operator++(CComplex &c2,int);

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

    friend CComplex &operator--(CComplex &c1);

    friend CComplex &operator--(CComplex &c2,int);

    friend CComplex &operator!(const CComplex &c1);

    friend CComplex &operator*(const CComplex &c1,const CComplex &c2);

    friend CComplex &operator/(const CComplex &c1,const CComplex &c2);

    friend CComplex &operator+=(CComplex &c1,const CComplex &c2);

    friend CComplex &operator-=(CComplex &c1,const CComplex &c2);

    friend CComplex &operator*=(CComplex &c1,const CComplex &c2);

    friend CComplex &operator/=(CComplex &c1,const CComplex &c2);

    friend bool operator==(const CComplex &c1,const CComplex &c2);

    friend bool operator>(const CComplex &c1,const CComplex &c2);

    friend bool operator<(const CComplex &c1,const CComplex &c2);

    friend bool operator>=(const CComplex &c1,const CComplex &c2);

    friend bool operator<=(const CComplex &c1,const CComplex &c2);

    void Print();

};

#endif /* CComplex_hpp */

/************************* CComplex.cpp************************************/

#include "CComplex.hpp"

#include <iostream>

CComplex operator+(const CComplex &c1,const CComplex &c2){

    返回一个无名对象 无名对象的生命周周期为当前行 用完立即回析构

    return CComplex(c1.m_fReal+c2.m_fReal,c1.m_fImag+c2.m_fReal);

}

CComplex &operator++(CComplex &c1){

    ++c1.m_fReal;

    ++c1.m_fImag;

    return c1;

}

CComplex &operator++(CComplex &c2,int){

    c2.m_fReal++;

    c2.m_fImag++;

    return c2;

}

CComplex operator-(const CComplex &c1,const CComplex &c2){

    return CComplex(c1.m_fReal-c2.m_fReal,c1.m_fImag-c2.m_fImag);

}

CComplex &operator--(CComplex &c1){

    --c1.m_fReal;

    --c1.m_fImag;

    return c1;

}

CComplex &operator--(CComplex &c2,int){

    c2.m_fReal--;

    c2.m_fImag--;

    return c2;

}

//

//CComplex &operator!(const CComplex &c1){

//    CComplex c;

//    

//}

CComplex &operator*(const CComplex &c1,const CComplex &c2){

    CComplex c;

    c.m_fReal = c1.m_fReal*c2.m_fReal;

    c.m_fImag = c1.m_fImag*c2.m_fImag;

    

    return c;

}

CComplex &operator/(const CComplex &c1,const CComplex &c2){

    CComplex c;

    c.m_fReal = c1.m_fReal/c2.m_fReal;

    c.m_fImag = c1.m_fImag/c2.m_fImag;

    return c;

}

CComplex &operator+=(CComplex &c1,const CComplex &c2){

    c1.m_fReal += c2.m_fReal;

    c1.m_fImag += c2.m_fImag;

    return c1;

}

CComplex &operator-=(CComplex &c1,const CComplex &c2){

    c1.m_fReal -= c2.m_fReal;

    c1.m_fImag -= c2.m_fImag;

    return c1;

}

CComplex &operator*=(CComplex &c1,const CComplex &c2){

    c1.m_fReal *= c2.m_fReal;

    c1.m_fImag *= c2.m_fImag;

    return c1;

}

CComplex &operator/=(CComplex &c1,const CComplex &c2){

    c1.m_fReal /= c2.m_fReal;

    c1.m_fImag /= c2.m_fImag;

    return c1;

}

bool operator==(const CComplex &c1,const CComplex &c2){

    return c1.m_fReal==c2.m_fReal && c1.m_fImag==c2.m_fImag;

}

bool operator>(const CComplex &c1,const CComplex &c2){

    if(c1.m_fReal > c1.m_fReal)

        return true;

    if(c1.m_fReal < c1.m_fReal)

        return false;

    return c1.m_fImag > c2.m_fImag;

}

bool operator<(const CComplex &c1,const CComplex &c2){

    if(c1.m_fReal < c1.m_fReal)

        return true;

    if(c1.m_fReal > c1.m_fReal)

        return false;

    return c1.m_fImag < c2.m_fImag;

}

bool operator>=(const CComplex &c1,const CComplex &c2){

    if(c1.m_fReal >= c1.m_fReal)

        return true;

    if(c1.m_fReal <= c1.m_fReal)

        return false;

    return c1.m_fImag >= c2.m_fImag;

}

bool operator<=(const CComplex &c1,const CComplex &c2){

    if(c1.m_fReal <= c1.m_fReal)

        return true;

    if(c1.m_fReal >= c1.m_fReal)

        return false;

    return c1.m_fImag <= c2.m_fImag;

}

void CComplex::Print(){

    printf("%0.2lf  %0.2lf \n",m_fImag,m_fReal);

}

重载运算符++

1、前++的时候,是返回一对象,而不是返回一个局部变量,所以返回值可以为引用,在返回这个对香前都已经存,在出函数的时候不会被销毁,

2、后++的时候,同前++一样可以为引用返回,但是他的函数必须加个缺省的int类型,这个int不参与计算只是为了区别前++与后++;

3、他们的参数不能加const因为他自身要发生改变,加了const就成为不可改变的;因为 const 修饰的成员变量在第一次定义后不可改变;

例如、

Complex &operator++(Complex &c1) 正确

Complex &operator++(const Complex &c1) 错误

重载运算符 “+=”“-=” “*=” “/=”

1、返回值能用引用:在返回c1的时候C1 的地址不是在当前函数内产生的,在函数外就已经有了c1,所以c1的的生命周期不仅限于当前函数,能用引用。如果c1是在当前函数中产生的变量,那么在离开当前函数的时候就会被清理。这个时候c1是一个不存在的乱码地址,就会错误。

2、用引用返回,速度快,在返回的时候不会产生数据复制;

3、参数不能用const 返回值要作修改不能加const;

重载比较运算符 "==" ">=" "<=" "!="

1、返回值为bool类型, 参数可以加const 返回值不做修改;

猜你喜欢

转载自blog.csdn.net/pengkejie1314520/article/details/87870563