C++重载运算符

重载运算符

加法重载运算符

提要点:
1.可以选取成员函数进行重载,也就是operator+(Person &p),传入一个参数就是直接调用它,另外一个参数作为传入,p1.operator(p2)
2.可以用全局函数进行重载(本人认为这种相对好理解一些)

#include<iostream>
using namespace std;
class Person
{
public:
 //成员函数实现重载
//     Person operator+(Person &p)
//     {
//         Person tmp;
//         tmp.m_A = this->m_A + p.m_A;
//         tmp.m_B = this->m_B + p.m_B;
//         return tmp;
//     }
// public:
//     int m_A;
//     int m_B;
public:
    int m_A;
    int m_B;
};
//全局函数实现重载
Person operator+(Person &p1,Person &p2)
{
    Person tmp;
    tmp.m_A = p1.m_A + p2.m_A;
    tmp.m_B = p1.m_B + p2.m_B;
    return tmp;
}
void test01()
{
    Person p1;
    p1.m_A = 40;
    p1.m_B = 10;
    Person p2;
    p2.m_A = 40;
    p2.m_B = 40;

    Person p3 = p1 + p2;
    cout<<p3.m_A<<" "<<p3.m_B<<"\n";
}
int main()
{
    test01();
    return 0;
}


左移重载运算符

#include<iostream>
using namespace std;
class Person
{
public:
    int m_A;
    int m_B;
};
//利用全局函数重载左移运算符
ostream &operator << (ostream &cout,Person &p)//本质:cout << p
//用 &是因为确保他只有一个
{
    cout<<p.m_A<<" "<<p.m_B;
    return cout;

}
void test()
{
    Person p1;
    p1.m_A = 10;
    p1.m_B = 10;
    cout<<p1<<"\n";
}
int main()
{
    test();
    return 0;
}


重载递增/递减运算符

提要点:
1.需要具有链式思维,注意返回什么类型
2.区分前置递增后置递增:用占位参数来区分,满足重载函数的条件

#include<iostream>
using namespace std;
class selfdefinejia
{
    friend ostream &operator<<(ostream &cout,selfdefinejia s);
public:
    selfdefinejia()
    {
        m_Num = 0;
    }
//前置运算符递增
selfdefinejia &operator++()//返回引用是因为要一直对一个数据进行操作
{
    //先进行 ++ 运算
    m_Num++;
    return *this;
}
//后置递增
selfdefinejia operator++(int)//int表示占位参数可以用于区分前置和后置
{
    //先记录当时结果
    selfdefinejia tmp = *this;
    //后递增
    m_Num++;
     //最后将记录结果返回
    return tmp;
}


private:
    int m_Num;
};

ostream &operator<<(ostream &cout,selfdefinejia s)
{
    cout<<s.m_Num;
    return cout;
}
void test()
{
    selfdefinejia SJ;
    cout << ++ SJ<<"\n";//需要写重载左移运算符
}
int main()
{
    test();
    return 0;
}

递减运算符(自己写的版本)

#include <iostream>
using namespace std;
class selfjian
{
    friend ostream &operator<<(ostream &cout, selfjian jian);

public:
    selfjian()
    {
        m_Num = 7;
    }
//前置--
selfjian &operator--()
{
    m_Num--;
    return *this;
}


//后置 --
selfjian operator--(int)
{
    //先用临时变量存储你原先的值
    selfjian tmp = *this;
    // 再递减
    m_Num--;
    return tmp;
}
private:
    int m_Num;
};
// 重载左移运算符,达到可以输出整个类的效果
ostream &operator<<(ostream &cout, selfjian jian)
{
    cout << jian.m_Num << "\n";
    return cout;
}
void test()
{
    selfjian J;
    cout << --J << "\n";
}
void test1()
{
    selfjian J1;
    cout<<J1--<<"\n";
}
int main()
{
    test();
    test1();
    return 0;
}


重载赋值运算符

提要点:
1.想实现连等操作注意链式思维看函数返回值

#include<iostream>
using namespace std;
class Person
{
public:
    Person(int age)
    {
        m_Age = new int(age);
    }

    ~Person()
    {
        if(m_Age != NULL)
        {
            delete m_Age;
            m_Age = NULL;
        }
    }

    Person &operator=(Person &p)
    {
        //应该先判断是否有属性在堆区,应该先释放干净,再去进行深拷贝操作
        if(m_Age != NULL)
        {
            delete m_Age;
            m_Age = NULL;
        }
        m_Age = new int(*p.m_Age);//深拷贝的操作

        //返回自身就可以连等了
        return *this;
    }
    int *m_Age;
};
void test()
{
    Person p1(20);
    Person p2(19);
    Person p3(18);
    p1 = p2 = p3;
    cout<< *p1.m_Age<<"\n";
    cout<< *p2.m_Age<<"\n";
    cout<< *p3.m_Age<<"\n";
}
int main()
{
    test();
    return 0;
}


关系运算符重载

提要点:
注意返回的是 bool值

#include<iostream>
using namespace std;
class Person
{
public:
    Person(string NAME,int XUEHAO)
    {
        N_name = NAME;
        xuehao = XUEHAO;
    }

   bool operator==(Person &p)
    {
        if(this->N_name == p.N_name && this->xuehao == p.xuehao)
        {
            return true;
        }
        return false;
    }
    string N_name;
    int xuehao;
};
void test()
{
    Person p1("Alaso_shuang",20);
    Person p2("Alaso",19);

    if(p1 == p2)
    {
        cout<<"yes"<<"\n";
    }else cout<<"No"<<"\n";
}
int main()
{
    test();
    return 0;
}
posted @ 2025-03-04 15:24  Alaso_shuang  阅读(23)  评论(0)    收藏  举报
2 3
4