007 operator

 

/*
目录:
   一 CComplex 类内
   二 CComplex 类外
   三 仿写std::cin和std::cout
*/

 

一 CComplex 类内

#include "stdafx.h"
#include <iostream>

using namespace std;

class CComplex
{
public:
    CComplex()
    {
        m_fImag = m_fReal = 1;
    }
    CComplex(double r, double i):m_fReal(r), m_fImag(i)
    {
    }

    void Print()
    {
        cout << m_fReal << "," << m_fImag << endl;
    }
    operator double()
    {
        return m_fReal;
    }

    CComplex& operator!();
    CComplex& operator--();        // --i
    CComplex operator--(int);    // i--
    CComplex& operator++();        // ++i
    CComplex operator++(int);    // i++

    CComplex operator+(const CComplex &c2);
    CComplex operator-(const CComplex &c2);
    CComplex operator*(const CComplex &c2);
    CComplex operator/(const CComplex &c2);

private:
    double m_fReal;
    double m_fImag;
};

CComplex& CComplex::operator!()
{
    m_fReal = !m_fReal;
    m_fImag = !m_fImag;
    return *this;
}

CComplex& CComplex::operator--()
{
    --m_fReal;
    --m_fImag;
    return *this;
}

CComplex CComplex::operator--(int)
{
    CComplex temp = *this;
    --m_fReal;
    --m_fImag;
    return temp;
}

CComplex& CComplex::operator++()
{
    ++m_fReal;
    ++m_fImag;
    return *this;
}

CComplex CComplex::operator++(int)
{
    CComplex temp = *this;
    ++m_fReal;
    ++m_fImag;
    return temp;
}


CComplex CComplex::operator+(const CComplex &c2)
{
    CComplex temp;
    temp.m_fReal = this->m_fReal + c2.m_fReal;
    temp.m_fImag = this->m_fImag + c2.m_fImag;
    return temp;
}

CComplex CComplex::operator-(const CComplex &c2)
{
    CComplex temp;
    temp.m_fReal = this->m_fReal - c2.m_fReal;
    temp.m_fReal = this->m_fReal - c2.m_fReal;
    return temp;
}

CComplex CComplex::operator*(const CComplex &c2)
{
    CComplex temp;
    temp.m_fReal = this->m_fReal * c2.m_fReal;
    temp.m_fReal = this->m_fReal * c2.m_fReal;
    return temp;
}

CComplex CComplex::operator/(const CComplex &c2)
{
    CComplex temp;
    temp.m_fReal = this->m_fReal / c2.m_fReal;
    temp.m_fReal = this->m_fReal / c2.m_fReal;
    return temp;
}

int main(int argc, char *argv[], char **envp)
{
    CComplex c1(2, 3), c2(4, 5);
    CComplex c3 = c1 + (++c2);

    double d = c3;    // 类型重载
    c3.Print();

    CComplex c4;
    c4 = !c3;
    c4.Print();

    return 0;
}

 

二 CComplex 类外

#include "stdafx.h"
#include <iostream>

using namespace std;

class CComplex
{
public:
    CComplex()
    {
        m_fImag = m_fReal = 1;
    }
    CComplex(double r, double i):m_fReal(r), m_fImag(i)
    {
    }

    void Print()
    {
        cout << m_fReal << "," << m_fImag << endl;
    }
    operator double()
    {
        return m_fReal;
    }

    friend CComplex& operator!(CComplex &c1);
    friend CComplex& operator--(CComplex &c1);        // --i
    friend CComplex operator--(CComplex &c1, int);    // i--
    friend CComplex& operator++(CComplex &c1);        // ++i
    friend CComplex operator++(CComplex &c1, int);    // i++

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

private:
    double m_fReal;
    double m_fImag;
};

CComplex& operator!(CComplex &c1)
{
    c1.m_fReal = !c1.m_fReal;
    c1.m_fImag = !c1.m_fImag;
    return c1;
}

CComplex& operator--(CComplex &c1)
{
    --c1.m_fReal;
    --c1.m_fImag;
    return c1;
}

CComplex operator--(CComplex &c1, int)
{
    CComplex temp = c1;
    --c1.m_fReal;
    --c1.m_fImag;
    return temp;
}

CComplex& operator++(CComplex &c1)
{
    ++c1.m_fReal;
    ++c1.m_fImag;
    return c1;
}

CComplex operator++(CComplex &c1, int)
{
    CComplex temp = c1;
    ++c1.m_fReal;
    ++c1.m_fImag;
    return temp;
}


CComplex operator+(const CComplex &c1, const CComplex &c2)
{
    CComplex temp;
    temp.m_fReal = c1.m_fReal + c2.m_fReal;
    temp.m_fImag = c1.m_fImag + c2.m_fImag;
    return temp;
}

CComplex operator-(const CComplex &c1, const CComplex &c2)
{
    CComplex temp;
    temp.m_fReal = c1.m_fReal - c2.m_fReal;
    temp.m_fReal = c1.m_fReal - c2.m_fReal;
    return temp;
}

CComplex operator*(const CComplex &c1, const CComplex &c2)
{
    CComplex temp;
    temp.m_fReal = c1.m_fReal * c2.m_fReal;
    temp.m_fReal = c1.m_fReal * c2.m_fReal;
    return temp;
}

CComplex operator/(const CComplex &c1, const CComplex &c2)
{
    CComplex temp;
    temp.m_fReal = c1.m_fReal / c2.m_fReal;
    temp.m_fReal = c1.m_fReal / c2.m_fReal;
    return temp;
}


int main(int argc, char *argv[], char **envp)
{
    CComplex c1(2, 3), c2(4, 5);
    CComplex c3 = c1 + (++c2);

    double d = c3;    // 类型重载
    c3.Print();

    CComplex c4;
    c4 = !c3;
    c4.Print();

    return 0;
}

 


三 仿写std::cin和std::cout

  压缩包: 链接

 

没系统 std::cin 和std::cout好用
        1 兼容性
        2 支持少(endl)

 

上一篇:北冥乘海生:996其实没什么卵用


下一篇:haproxy实验练习