zoukankan      html  css  js  c++  java
  • 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)
  • 相关阅读:
    超链接解决头部fixed问题
    cookie操作
    JS 阻止整个网页的内容被选中
    stopPropagation(), preventDefault() 和 return false
    获得元素的位置。宽、高
    css 清除浮动
    pytest官网文档の第一章:安装和快速开始
    采用Anaconda安装python
    jmeter利用BeanShell PreProcessor编写脚本实现参数化
    java长连接和短连接
  • 原文地址:https://www.cnblogs.com/huafan/p/11619459.html
Copyright © 2011-2022 走看看