研一寒假C++复习笔记--运算符重载实例

目录

1--运算符重载

2--加号运算符重载(+)

3--左移运算符重载(<<)

4--递增运算符重载

5--赋值运算符重载

6--关系运算符重载

7--函数调用运算符重载


1--运算符重载

        对已有运算符重新进行定义,赋予其另一种功能,以适应不同的数据类型;

① 加号运算符重载

② 左移运算符重载

③ 递增运算符重载

④ 赋值运算符重载

⑤ 关系运算符重载

⑥ 函数调用运算符重载

2--加号运算符重载(+)

# include <iostream>

class Person{

public:
    // 通过成员函数重载 + 号
    Person operator+(Person &p){
        Person temp;
        temp.A = this->A + p.A;
        temp.B = this->B + p.B;
        return temp;
    }

    int A;
    int B;
};

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

// 运算符重载也可以进行函数重载
Person operator+(Person &p1, int val){
    Person temp;
    temp.A = p1.A + val;
    temp.B = p1.B + val;
    return temp;
}

int main(){
    Person p1;
    p1.A = 10;
    p1.B = 10;

    Person p2;
    p2.A = 10;
    p2.B = 10;

    // 成员函数重载的本质调用为: Person p3 = p1.operator+ + p2;
    // 全局函数重载的本质调用为: Person p3 = operator+(p1, p2);
    Person p3 = p1 + p2;
    std::cout << "p3.A = " << p3.A << std::endl;
    std::cout << "p3.B = " << p3.B << std::endl;

    Person p4 = p1 + 100;
    std::cout << "p4.A = " << p4.A << std::endl;
    std::cout << "p4.B = " << p4.B << std::endl;

    return 0;
}

3--左移运算符重载(<<)

# include <iostream>

class Person{

public:
    // 声明友元函数
    friend std::ostream& operator<<(std::ostream &cout, Person &p);

    // 有参构造函数
    Person(int a, int b){
        A = a;
        B = b;
    }
private:
    int A;
    int B;
};

// 只能利用全局函数重载 << 运算符
std::ostream& operator<<(std::ostream &cout, Person &p){
    std::cout << "A: " << p.A << ", B: " << p.B << std::endl;
    return cout;
}

int main(){
    // 调用有参构造初始化
    Person p(10, 10);

    // 本质调用:operator<<(cout, p)
    std::cout << p << p;
    return 0;
}

4--递增运算符重载

# include <iostream>

class MyInteger{

public:

    // 声明友元函数
    friend std::ostream& operator<<(std::ostream &cout, MyInteger p);

    MyInteger(){
        Num = 0;
    }

    // 重载前置++运算符
    MyInteger& operator++(){
        Num++;
        return *this; // 返回引用,一直对同一个数据进行操作
    }

    // 重载后置++运算符
    // int代表占位参数,用于区分前置和后置递增
    MyInteger operator++(int){
        MyInteger temp = *this; // 先记录自身
        Num++; // 再++
        return temp; // 返回引用,一直对同一个数据进行操作
    }

private:
    int Num;
};

// 只能利用全局函数重载 << 运算符
std::ostream& operator<<(std::ostream &cout, MyInteger p){
    std::cout << "Num: " << p.Num << std::endl;
    return std::cout;
}

int main(){
    MyInteger myint1;
    std::cout << myint1 << std::endl;
    std::cout << ++myint1 << std::endl;\

    MyInteger myint2;
    std::cout << myint2 << std::endl;
    std::cout << myint2++ << std::endl;
    std::cout << myint2 << std::endl;

    return 0;
}

5--赋值运算符重载

# include <iostream>

class Person{

public:
    Person(int age){
        Age = new int(age);
    }
    ~Person(){
        if (Age != NULL){
            delete Age;
            Age = NULL;
        }
    }

    // 重载赋值运算符
    Person& operator=(Person &p){
        // 编译器提供的浅拷贝
        // Age = p.Age;
        // 先判断是否有属性在堆区
        if (Age != NULL){
            delete Age;
            Age = NULL;
        }
        // 深拷贝
        Age = new int(*p.Age);
        return *this;
    }
    int *Age;
};

int main(){

    Person p1(10);
    Person p2(20);
    Person p3(30);
    p3 = p2 = p1; // 赋值操作
    std::cout << "p1.age: " << *p1.Age << std::endl;
    std::cout << "p2.age: " << *p2.Age << std::endl;
    std::cout << "p3.age: " << *p3.Age << std::endl;
    return 0;
}

6--关系运算符重载

# include <iostream>

class Person{

public:
    Person(std::string name, int age){
        Age = age;
        Name = name;
    }

    // 重载 == 号
    bool operator==(Person &p){
        if( Name == p.Name && Age == p.Age){
            return true;
        }
        return false;
    }

    // 重载 != 号
    bool operator!=(Person &p){
        if( Name == p.Name && Age == p.Age){
            return false;
        }
        return true;
    }

    int Age;
    std::string Name;
};

int main(){

    Person p1("ljf", 23);
    Person p2("zsy", 23);
    if(p1 == p2){
        std::cout << "p1 is equal to p2!" << std::endl;
    }
    else{
        std::cout << "p1 is not equal to p2!" << std::endl;
    }

    Person p3("ljf", 23);
    Person p4("zsy", 23);
    if(p3 != p4){
        std::cout << "p3 is not equal to p4!" << std::endl;
    }
    else{
        std::cout << "p3 is equal to p4!" << std::endl;
    }


    return 0;
}

7--函数调用运算符重载

# include <iostream>

class MyPrint{

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

class MyAdd{

public:
    int operator()(int num1, int num2){
        return num1 + num2;
    }
};

int main(){

    MyPrint print;
    print("Hello, C++!"); // 类似于函数调用,所以称为仿函数

    std::cout << "The sum is: " << MyAdd()(100, 100) << std::endl; // MyAdd()称为匿名对象

    return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值