网站模板中文,龙岩网络公司,常见网站类型,自助建设彩票网站作用#xff1a;重载关系运算符#xff0c;可以让两个自定义类型对象进行对比操作
代码如下#xff1a;
#include iostream
using namespace std;
#include cstring
//重载关系运算符class Person {public:Person(string name, int age) {m_Name name;m_…作用重载关系运算符可以让两个自定义类型对象进行对比操作
代码如下
#include iostream
using namespace std;
#include cstring
//重载关系运算符class Person {public:Person(string name, int age) {m_Name name;m_Age age;}bool operator(Person p) {if (this-m_Name p.m_Name this-m_Age p.m_Age) {return true;}return false;}bool operator!(Person p) {if (this-m_Name p.m_Name this-m_Age p.m_Age) {return false;}return true;}string m_Name;int m_Age;
};void test01() {Person p1(Tom, 18);Person p2(Tom, 18);if (p1 p2) {cout p1 和 p2是相等的 endl;} else {cout p1 和 p2是不相等的 endl;}if (p1 ! p2) {cout p1 和 p2是不相等的 endl;} else {cout p1 和 p2是相等的 endl;}
}int main() {test01();return 0;
}