007 operator

 

/ * 
Directory: 
   within a class of CComplex 
   outer two CComplex class 
   three Imitating :: std :: cin and COUT STD 
* /

 

 

Rewriting the structure design of the various symbolic representations of operations, the operation object is to expand the structure / class

 

CComplex within a class

#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 << "
    }
    operator 
    {
        return
    }

    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 &
    CComplex operator/(const CComplex &c2);

private:
    double m_fReal;
    double
};

CComplex& CComplex::operator!
{
    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
}


CComplex CComplex::operator+(const CComplex &
{
    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 &
{
    CComplex temp;
    temp.m_fReal = this->m_fReal -
    temp.m_fReal = this->m_fReal - c2.m_fReal;
    return
}

CComplex CComplex::operator*(const CComplex &
{
    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;
}

 

Two outer class 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;     // type overload 
    c3.Print (); 

    CComplex C4; 
    C4 ! = C3; 
    c4.Print (); 

    return  0 ; 
}

 


Three parodies std :: cin and std :: cout

  Archive: Links

 

No system std :: cin and std :: cout handy
         1 compatibility
         2 and less support (endl)

 

Guess you like

Origin www.cnblogs.com/huafan/p/11619459.html