Loading

C++运算符重载案例

1.加号运算符重载

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

// 加号运算符重载
// 实现两个自定义数据类型的相加运算
// 1.通过成员函数  2.通过全局函数

class Person{
    public:

    // 1.通过成员函数重载+号
    // Person operator+(Person &p){ // 以值方式返回一个新的拷贝
    //     Person temp;
    //     temp.m_A = this->m_A + p.m_A;
    //     temp.m_B = this->m_B + p.m_B;
    //     return temp;
    // }

    int m_A;
    int m_B;
};

// 2.通过全局函数重载+号
Person operator+(Person p1, Person p2){
    Person temp;
    temp.m_A = p1.m_A + p2.m_A;
    temp.m_B = p1.m_B + p2.m_B;
    return temp;
}

// 运算符重载也可以发生 函数重载(同一作用域下函数名相同,但函数参数类型/个数/顺序不同)
Person operator+(Person p1, int num){
    Person temp;
    temp.m_A = p1.m_A + num;
    temp.m_B = p1.m_B + num;
    return temp;
}


void test01(){
    Person p1;
    p1.m_A = 10;
    p1.m_B = 10;

    Person p2;
    p2.m_A = 10;
    p2.m_B = 10;

    // 1.成员函数重载+号 本质调用
    // Person p3 = p1.operator+(p2);

    // 2.全局函数重载+号 本质调用
    // Person p3 = operator+(p1, p2);

    // 上面两种调用都可以简化为以下形式 即实现了加号运算符重载函数operator+()后,便可使用这种形式相加
    Person p3 = p1 + p2; 

    cout << "p3.m_A = " << p3.m_A << endl;
    cout << "p3.m_B = " << p3.m_B << endl;
}

void test02(){
    // 测试运算符重载的函数重载是否可行
    Person p1;
    p1.m_A = 10;
    p1.m_B = 10;

    Person p4 = p1 + 100; 

    cout << "p4.m_A = " << p4.m_A << endl;
    cout << "p4.m_B = " << p4.m_B << endl;
}


int main(){
    test01();
    test02();
}

2.左移运算符重载

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

// 作用:可以输出自定义数据类型
// 1.通过成员函数(不行)  2.(只能)通过全局函数


class Person{

    friend ostream& operator<<(ostream &cout, Person &p);

   public:
   Person(int a, int b){
       m_A = a;
       m_B = b;
   }

    // 1.利用成员函数来重载左移运算符 p.operator<<(cout) 简化版本 p << cout
    //   因此一般不会利用成员函数重载<<运算符,因为无法实现cout在左侧
    // void operator<<(cout){
    //     cout << "" << endl;
    // }

    private:
    int m_A;
    int m_B;

};

// 只能通过全局函数重载左移运算符
ostream& operator<<(ostream &cout, Person &p){ 
    // 该函数 的本质是 operator<<(cout,p) 简化形式为 cout << p
    // cout 的数据类型是标准的输出流对象,并且全局只有一个,因此只能以引用的方式传过来,而不能新创建一个对象
    cout << "m_A = " << p.m_A << endl;
    cout << "m_B = " << p.m_B << endl;
    return cout; // 从而可以链式编程 可以无限追加输出
}

void test01(){
    Person p(10,10);

    cout << p << endl;

}

int main(){
    test01();
}

3.递增运算符重载

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

// 作用:通过重载递增运算符,实现自己的整型数据
// 1.前置递增  // 2.后置递增

// 首先需要重载左移运算符
// 然后实现前置递增和后置递增的++运算符



class MyInteger{

    friend ostream& operator<<(ostream& cout, MyInteger myint);

    public:
    MyInteger(){
        m_Num = 0;
    }
    // 重载前置++运算符 返回引用而不是返回值 是为了可以一直对一个数据进行递增
    MyInteger& operator++(){
        // 先递增
        m_Num++;
        // 再将自身返回
        return *this;
    }

    // 重载后置++运算符 返回值而不是返回引用 因为返回的temp是局部对象的引用,局部对象销毁后就是非法引用了
    // int代表占位参数,用于区分前置和后置递增
    MyInteger operator++(int){
        // 先 记录当时结果
        MyInteger temp = *this;
        // 然后递增
        m_Num++;
        // 然后将记录的结果做返回
        return temp;
    }

    private:
    int m_Num;

};

// 首先需要重载左移运算符
ostream& operator<<(ostream& cout, MyInteger myint){
    cout << myint.m_Num;
    return cout;
}


void test01(){
    MyInteger myint;
    cout << ++(++myint) << endl;  // 2
    cout << myint << endl;        // 2
}


void test02(){
    MyInteger myint;
    cout << myint++ << endl;  // 0    
    cout << myint << endl;   // 1

    int a = 0;
    // cout << (a++)++ << endl;  // 0  // (a++)++是报错的,所以我们自己实现后置递增也不用考虑这种情况
    cout << a << endl;   // 1
}


int main(){
    test01();
    test02();
}

4.递减运算符重载

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

// 首先需要重载左移运算符
// 然后实现前置递减和后置递减的--运算符

class MyInteger{

    friend ostream& operator<<(ostream& cout, MyInteger myint);

    public:
    MyInteger(){
        m_Num = 0;
    }
    // 前置递减
    MyInteger& operator--(){
        m_Num--;
        return *this;
    }

    // 后置递减
    MyInteger operator--(int){
        MyInteger temp = *this;
        m_Num--;
        return temp;
    }

    private:
    int m_Num;
};

// 首先需要重载左移运算符
ostream& operator<<(ostream& cout, MyInteger myint){
    cout << myint.m_Num;
    return cout;
}


void test01(){
    MyInteger myint;
    cout << --(--myint) << endl;  // -2
    cout << myint << endl;  // -2

}

void test02(){
    MyInteger myint;
    cout << myint-- << endl;  // 0
    cout << myint << endl;  // -1
}


int main(){
    test01();
    test02();
}

5.赋值运算符重载

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

// 赋值运算符重载
// 目的:编译器提供的赋值运算符操作是浅拷贝操作
//      当对象间的拷贝例如p1 = p2; 这种,当涉及到堆区数据的拷贝时,会出现内存重复释放的错误
//      因此需要赋值运算符重载,进行深拷贝的操作

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){
        // 编译器提供是的浅拷贝 m_Age = p.m_Age; 
        // 但我们不应该这样做

        // 应该先判断是否有属性在堆区(因为Person p2(20);后已经在堆区开辟过内存了)
        // 如果有 先释放干净 然后再深拷贝
        if(m_Age != NULL){
            delete m_Age;
            m_Age = NULL;
        }
        m_Age = new int(*p.m_Age);  // 深拷贝 重新在堆区申请一块内存空间,然后让自身指针去指向这个新的空间

        return *this; // 返回自身,从而可以链式编程
    }

    int *m_Age; // 使用指针,这里年龄的真实数据要开辟到堆区
};


void test01(){
    // 测试对象间的赋值操作
    cout << "test02" << endl;

    Person p1(18);
    Person p2(20);

    p2 = p1; // 赋值操作,当释放堆区内存时也会出现内存重复释放的错误,因此需要赋值运算符重载

    cout << "p1 的年龄为 " << *p1.m_Age << endl;   // 18
    cout << "p2 的年龄为 " << *p2.m_Age << endl;   // 18
}

void test02(){
    // 测试链式赋值操作
    cout << "test02" << endl;

    Person p1(18);
    Person p2(20);
    Person p3(30);

    p3 = p2 = p1;

    cout << "p1 的年龄为 " << *p1.m_Age << endl;   // 18
    cout << "p2 的年龄为 " << *p2.m_Age << endl;   // 18
    cout << "p3 的年龄为 " << *p3.m_Age << endl;   // 18
}

int main(){
    test01();
    test02();
}

6.关系运算符重载

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

// 关系运算符重载, 可以让两个自定义类型对象进行对比操作

class Person{

    public:
    Person(string name, int age){
        m_Name = name;
        m_Age = age;
    }

    // 重载关系运算符==
    bool operator==(Person &p){
        if(this->m_Name == p.m_Name && this->m_Age == p.m_Age){
            return true;
        }
        else{
            return false;
        }
    }

    // 重载关系运算符!=
    bool operator!=(Person &p){
        if(this->m_Name == p.m_Name && this->m_Age == p.m_Age){
            return false;
        }
        else{
            return true;
        }
    }

    string m_Name;
    int m_Age;

};

void test01(){
    Person p1("Tom", 18);
    Person p2("Sun", 18);

    if (p1==p2){
        cout << "对象p1 == p2" << endl;
    }
    else{
        cout << "对象p1 != p2" << endl;
    }
    if (p1!=p2){
        cout << "对象p1 != p2" << endl;
    }
    else{
        cout << "对象p1 == p2" << endl;
    }

}

int main(){
    test01();

}

7.函数调用运算符重载

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

// 函数调用运算符()也能发生重载
// 由于重载后使用的方式非常像函数的调用,因此称为 仿函数
// 仿函数 没有固定写法 非常灵活   ———— 在STL中用到的比较多

// ----------------------------------------------------------------------------------
// 打印输出类
class MyPrint{
    public:

    // 重载函数调用运算符
    void operator()(string test){
        cout << test << endl;
    }

};

// 正常打印输出函数
void print(string test){
    cout << test << endl;
}


void test01(){
    MyPrint myprint;
    myprint("hello world");
    // ^
    // I   上面是重载的仿函数 下面是正常函数 两者调用形式非常像!
    // v
    print("hello world");
}
// ----------------------------------------------------------------------------------


// ----------------------------------------------------------------------------------
// 仿函数非常灵活 没有一个固定的写法 可以依据需求编写
// 加法类
class MyAdd{
    public:
    int operator()(int num1, int num2){
        return num1+num2;
    }

};
void test02(){
    
    MyAdd myadd;
    int result = myadd(20,30);
    cout << result << endl;

    // 匿名函数对象 
    // MyAdd()创建出了一个匿名函数对象,可以省去创建对象的步骤
    cout << MyAdd()(20,30) << endl;
}
// ----------------------------------------------------------------------------------

int main(){
    test01();
    test02();
}
posted @ 2022-01-17 19:33  少年人永远倔强  阅读(103)  评论(0编辑  收藏  举报