首页 > 编程语言 > 详细

c++之关系运算符重载

时间:2019-12-26 10:04:06      阅读:123      评论:0      收藏:0      [点我收藏+]
#include<iostream>
using namespace std;
class Person {
public:
    Person(string name,int age) {
        m_Name = name;
        m_Age = age;
    }
    string m_Name;
    int m_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;
        }

    }
};
void test() {
    Person p1("tom",18);
    Person p2("tomm", 18);
    if (p1 == p2) {
        cout << "p1与p2是相等的" << endl;
    }
    else {
        cout << "p1与p2是不相等的" << endl;
    }
}


int main() {
    test();
    system("pause");
    return 0;
}

c++之关系运算符重载

原文:https://www.cnblogs.com/xiximayou/p/12100368.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!