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;
}

 

转载于:https://www.cnblogs.com/weiyouqing/p/9657465.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值