1 #include <cstring>
2 #include <iostream> // 标准的输入输出
3 using namespace std;
4
5 class Person {
6 public:
7 Person(string name, int a)
8 {
9 ¦ this->m_name = name;
10 ¦ this->m_a = a;
11 }
12
13 bool operator==(Person& p)
14 {
15 ¦ if (this->m_name == p.m_name && this->m_a == p.m_a)
16 ¦ ¦ return true;
17 ¦ else
18 ¦ ¦ return false;
19 }
20
21 bool operator!=(Person& p)
22 {
23 ¦ if (this->m_name != p.m_name || this->m_a != p.m_a)
24 ¦ ¦ return true;
25 ¦ else
26 ¦ ¦ return false;
27 }
28 string m_name;
29 int m_a;
30 };
31
32 void test01()
33 {
34 Person p1("张三", 16);
35 Person p2("李四", 17);
36 Person p3("张三", 16);
37
38 if (p1 == p2) {
39 ¦ cout << "他们是同一个人" << endl;
40 } else {
41 ¦ cout << "他们不是同一个人" << endl;
42 }
43
44 if (p1 != p3) {
45 ¦ cout << "他们不是同一个人" << endl;
46 } else {
47 ¦ cout << "他们是同一个人" << endl;
48 }
49 }
50 int main()
51 {
52 test01();
53 return 0;
54 }
c++ 关系运算符重载
最新推荐文章于 2023-08-19 21:55:28 发布