关系运算符重载
作用:重载关系运算符,可以让两个自定义类型对象进行对比操作 示例:
#include <iostream>
using namespace std;
class Person {
public:
Person(string name,int num)
{
m_name = name;
m_num = num;
}
//重载关系运算符==
bool operator==(Person& P)
{
if (this->m_name == P.m_name && this->m_num == P.m_num) {
return true;
}
return false;
}
bool operator!=(Person& P)
{
if (this->m_name != P.m_name || this->m_num != P.m_num) {
return true;
}
return false;
}
private:
string m_name;
int m_num;
};
void test01()
{
Person P1("张三", 18);
Person P2("张三", 18);
if (P1 == P2) {
cout << "== isSame" << endl;
}
else {
cout << "== is No Same" << endl;
}
if (P1 != P2) {
cout << "!= is No Same" << endl;
}
else {
cout << "!= isSame" << endl;
}
}
int main()
{
test01();
system("pause");
return 0;
}