#include<iostream>
using namespace std;
#include<string.h>
class person {
public:
person(string name, int age) {
this->m_name = name;
this->m_age = age;
}
string m_name;
int m_age;
bool operator==(person& p) {
if (this->m_age == p.m_age && this->m_name == p.m_name) {
return true;
}
else {
return false;
}
}
bool operator!=(person& p) {
if (this->m_age != p.m_age && this->m_name != p.m_name) {
return false;
}
else {
return true;
}
}
};
void test() {
person p1("marry", 19);
person p2("marry", 19);
if (p1 == p2) {
cout << "p1和p2是相等的" << endl;
}
else {
cout << "p1和p2是不相等的" << endl;
}
person p3("jarry", 19);
person p4("marry", 19);
if (p3 != p4) {
cout << "p3和p4是不相等的" << endl;
}
else {
cout << "p3和p4是相等的" << endl;
}
}
int main() {
test();
return 0;
}
黑马-关系运算符重载-笔记
最新推荐文章于 2024-09-10 16:09:06 发布
![](https://img-home.csdnimg.cn/images/20240711042549.png)