20-【每天亿点点C++,简单又有趣儿】运算符重载 ----- 你以为你看到的就是你看到的么

#include<iostream>
#include<string>
using namespace std;

/*【+】运算符重载*/
class Cup
{
public:
    int m_A;
    int m_B;
    // operator+ 编译器给加法起的通用名
    // 成员函数重载 +

    Cup operator+(Cup &p)
    {
        Cup tmp;
        tmp.m_A = this->m_A +p.m_A;
        tmp.m_B = this->m_B +p.m_B;
        return tmp;
    }


};
 /*全局函数重载
Cup operator+(Cup &p1,Cup &p2)
{
    Cup tmp;
    tmp.m_A = p1.m_A +p2.m_A;
    tmp.m_B = p1.m_B +p2.m_B;
    return tmp;
}
*/
void test_20()
{
    Cup p1;
    p1.m_A = 10;
    p1.m_B = 10;

    Cup p2;
    p2.m_A = 10;
    p2.m_B = 10;

    Cup p3 = p1+p2;
    cout << "p2 m_A = " << p3.m_A << " m_B = " << p3.m_B << endl;



}


/*【<<】运算符重载*/
class Cup2
{
    friend ostream & operator<<(ostream &cout ,Cup2 &p);
public:
    Cup2(int a,int b):m_A(a),m_B(b)
    {

    }
private:

    // void operator<<( cout)  === p << cout
    // 不能用成员函数实现重载<<运算符因为无法实现 cout在左边
    int m_A;
    int m_B;
};
/*ostream & operator<<(ostream &cout ,Cup2 &p)
{
    cout << "m_A=" << p.m_A << "  m_B=" << p.m_B;
    return cout;
}
void test_21()
{
    Cup2 c(12,32);


    cout<<c<<endl;
}
*/





/*【++】运算符重载*/
class MyInteger
{
public:
    MyInteger()
    {
        m_Num = 0;
    }
    int getnum()
    {
        return m_Num;
    }
    // 重载前置++
    // 重载后置++
    // 返回引用是因为只对一个变量进行++
    MyInteger & operator++()
    {
        m_Num++;
        return *this;
    }
    //返回的是个值,因为是临时变量
    MyInteger operator++(int) //后置++运算符必备
    {
        //先 记录当前结果
        MyInteger tmp = *this;
        m_Num++;
        return tmp;

    }
private:
    int m_Num;
};

void test_22()
{
    MyInteger myint;
    ++myint;
    cout << "num = " <<myint.getnum() << endl;
    myint++;
    cout << "num = " <<myint.getnum() << endl;
}






/*【=】运算符重载*/
class Fuzhi
{
    // 编译器 提供 赋值运算函数
public:
    Fuzhi(int a)
    {
        m_A = new int(a);
    }
    ~Fuzhi()
    {
        if (*m_A != NULL)
        {
            delete m_A;
            m_A = NULL;
        }
    }
    Fuzhi & operator=(Fuzhi &p)
    {
        // 先判断
        if (*m_A != NULL)
        {
            delete m_A;
            m_A = NULL;
        }
        m_A = new int(*p.m_A);
        return *this;
    }
    int *m_A;
};

void test_23()
{
    Fuzhi f(23);
    Fuzhi f2(5523);
    Fuzhi f3(5523);
    f3 = f2 = f;
    //系统提供 f2 = f;//浅拷贝
    cout << "f  : a = " << *f.m_A << endl;
    cout << "f2 : a = " << *f2.m_A << endl;
    cout << "f3 : a = " << *f3.m_A << endl;

}








/*【== !=】运算符重载*/
class Dengyu
{
public:
    bool operator==(Dengyu &p)
    {
        if (this->Dname == p.Dname && this->Dage == p.Dage)
        {
            return true;
        }
        return false;
    }

    bool operator!=(Dengyu &p)
    {
        if (this->Dname == p.Dname && this->Dage == p.Dage)
        {
            return false;
        }
        return true;
    }
    Dengyu(string name,int age):Dname(name),Dage(age){}
    string Dname;
    int Dage;
};
void test_24()
{
    Dengyu d1("zz",44);
    Dengyu d2("zz",42);
    if (d1 == d2)
    {
        cout << "d1 == d2" << endl;
    }
    if (d1 != d2)
    {
        cout << "d1 != d2" << endl;
    }
}






/*【()】运算符重载[仿函数]*/
class PrinMy
{
public:
    void operator()(string b)
    {
        cout << b << endl;
    }
};
class AddMy
{
public:
    int operator()(int b,int d)
    {
        return d+b;
    }
};
void test_25()
{
    PrinMy m;
    m("hellow world!");//因为像一个函数,就称为仿函数
    AddMy add;
    int nn = add(4,6);
    cout << nn << endl;
    cout << AddMy()(100,100) << endl; //匿名 函数对象
}



int main()
{
    // 不能同时存在
    //成员函数重载 Cup p3 = p1.operator+(p2) === Cup p3 = p1 + p2
    //全局函数重载 Cup p3 = operator+(p1,p2) === Cup p3 = p1 + p2
    test_20();//+
    //test_21();//<<
    test_22();//++
    test_23();//=
    test_24();//== !=
    test_25();//()
}

在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/magic_shuang/article/details/107591452