Fork me on GitHub

c++之关系运算符重载

#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;
}
posted @ 2019-12-26 09:35  西西嘛呦  阅读(392)  评论(0编辑  收藏  举报