C++ 46 之 关系运算符的重载

cpp 复制代码
#include <iostream>
#include <string>
using namespace std;

class Students06{
public:
    string m_name;
    int m_age;
    Students06(string name, int age){
        this->m_name = name;
        this->m_age = age;
    }

    // 重载了 ==
    bool operator==(Students06 &stu){
        if(this->m_name == stu.m_name && this->m_age == stu.m_age){
            return true;
        }
        else{
            return false;
        }
    }

    // 重载了 !=
    // this->m_name == stu.m_name && this->m_age == stu.m_age 词语会自动返回 true 或 false
    bool operator!=(Students06 &stu){
        return !(this->m_name == stu.m_name && this->m_age == stu.m_age);
    }    
};


int main()
{
    // int a = 10;
    // int b = 20;
    // if(a==b)
    // {
    //     cout << "a==b" << endl;
    // }    
    // else{
    //     cout<< "a != b" << endl;
    // }


    Students06 stu1("四",18);
    Students06 stu2("五",18);
    // if(stu1 == stu2){
    //     cout << "stu1 == stu2"<< endl;
    // }
    // else{
    //     cout << "stu1 != stu2"<< endl;
    // }

    if(stu1 != stu2){
        cout << "stu1!=stu2"<< endl;
    }
    else{
        cout<< "stu1 == stu2" << endl;
    }

    return 0;
}
相关推荐
疯狂的喵4 小时前
C++编译期多态实现
开发语言·c++·算法
2301_765703144 小时前
C++中的协程编程
开发语言·c++·算法
m0_748708054 小时前
实时数据压缩库
开发语言·c++·算法
小魏每天都学习4 小时前
【算法——c/c++]
c语言·c++·算法
lly2024065 小时前
jQuery Mobile 表格
开发语言
惊讶的猫5 小时前
探究StringBuilder和StringBuffer的线程安全问题
java·开发语言
m0_748233175 小时前
30秒掌握C++核心精髓
开发语言·c++
Fleshy数模5 小时前
从数据获取到突破限制:Python爬虫进阶实战全攻略
java·开发语言
Duang007_6 小时前
【LeetCodeHot100 超详细Agent启发版本】字母异位词分组 (Group Anagrams)
开发语言·javascript·人工智能·python
froginwe116 小时前
Redis 管道技术
开发语言