zoukankan      html  css  js  c++  java
  • C++之重载操作符

    1.类中重载+操作符

    #define  _CRT_SECURE_NO_WARNINGS
    #include <iostream>
    using namespace std;
    
    class Object
    {
    public:
        Object()
        {
            
        }
    
        Object(int a)
        {
            num = a;
        }
    
        Object operator + (const Object& a)  //重载+操作符;
        {
            Object result;
            result.num = num + a.num;
            return result;
        }
    private:
        int num;
    };
    
    int main(int argc, char * argv[])
    {
        int a = 10;
        int b = 20;
    
        Object obja(a);
        Object objb(b);
    
        Object objc = obja + objb;
    
        system("pause");
        return 0;
    }

    2.重载全局操作符

     

    #define  _CRT_SECURE_NO_WARNINGS
    #include <iostream>
    using namespace std;
    
    class Object
    {
        friend Object operator + (const Object &a, const Object &b);
    public:
        Object()
        {
            
        }
    
        Object(int a)
        {
            num = a;
        }
    
    private:
        int num;
    };
    
    Object operator + (const Object &a, const Object &b)
    {
        Object result;
        result.num = a.num + b.num;
        return result;
    }
    
    
    int main(int argc, char * argv[])
    {
        Object obja(10);
        Object objb(20);
    
        Object objc = obja + objb;
    
        system("pause");
        return 0;
    }

    全局操作符,主要注意的是,当重载操作符访问私有成员时,需要将操作符声明为友元;

    4. 重载[] 操作符

    #define  _CRT_SECURE_NO_WARNINGS
    #include <iostream>
    using namespace std;
    
    class Object
    {
    public:
        Object()
        {
        }
    
        Object(char * str)
        {
            int size = strlen(str);
            m_buf = new char[size];
            strcpy(m_buf,str);
        }
    
        char& operator    [] (int index)
        {
            return m_buf[index];
        }
    
        ~Object()
        {
            delete[] m_buf;
        }
    
    private:
        char *m_buf;
    };
    
    int main(int argc, char * argv[])
    {
        Object obja("weiyouqing");
        
        char str = obja[0];
        obja[0] = 'W';
    
        system("pause");
        return 0;
    }

    5.重载==操作符

    #define  _CRT_SECURE_NO_WARNINGS
    #include <iostream>
    using namespace std;
    
    class Object
    {
    public:
        Object()
        {
        }
    
        Object(int a)
        {
            num = a;
        }
    
        bool operator == (const Object &other)
        {
            if (num == other.num)
                return true;
            else
                return false;
        }
    
        ~Object()
        {
            
        }
    
    private:
        int num;
    };
    
    int main(int argc, char * argv[])
    {
        Object obja(2);
        Object objb(0);
        if (obja == objb)
            cout << "equal" << endl;
        else
            cout << "not equal" << endl;
    
        system("pause");
        return 0;
    }

    全局重载操作符:

    #define  _CRT_SECURE_NO_WARNINGS
    #include <iostream>
    using namespace std;
    
    class Object
    {
        friend bool operator == (const Object &a, const Object &b);
    public:
        Object()
        {
        }
    
        Object(int a)
        {
            num = a;
        }
    
        ~Object()
        {
            
        }
    
    private:
        int num;
    };
    
    
    bool operator == (const Object &a,const Object &b)
    {
        if (a.num == b.num)
            return true;
        else
            return false;
    }
    
    
    int main(int argc, char * argv[])
    {
        Object obja(2);
        Object objb(0);
        if (obja == objb)
            cout << "equal" << endl;
        else
            cout << "not equal" << endl;
    
        system("pause");
        return 0;
    }
  • 相关阅读:
    课堂作业之公文流转
    统计字符出现频率(java)
    课堂测试第八周
    HTML学习笔记——语法+骨架
    HTTP协议
    MVC架构模式概述
    CodeIgniter框架——CI中视图路径问题
    CodeIgniter框架——CI组件间信息流走向
    CodeIgniter框架——数据库类(配置+快速入门)
    chm文件无法阅读
  • 原文地址:https://www.cnblogs.com/weiyouqing/p/9657465.html
Copyright © 2011-2022 走看看