C++重载关系运算符

cpp 复制代码
#include<iostream>
using namespace std;

class Person
{
public:
	string Name;
	int Age;

	Person(string name, int age)
	{
		Name = name;
		Age = age;
	} 

	bool operator==(Person& p)
	{
		if ((Name == p.Name) && (Age == p.Age))
		{
			return true;
		}
		else
		{
			return false;
		}
	}

	bool operator>=(Person& p)
	{
		if ((Name >= p.Name) && (Age >= p.Age))
		{
			return true;
		}
		else
		{
			return false;
		}
	}

	bool operator<=(Person& p)
	{
		if ((Name <= p.Name) && (Age <= p.Age))
		{
			return true;
		}
		else
		{
			return false;
		}
	}
};

int main() {
	Person p1("张三", 18);
	Person p2("李四", 20);

	if (p1 == p2)
	{
		cout << "p1和p2相等" << endl;
	}
	else
	{
		cout << "p1和p2不相等" << endl;
	}

}
相关推荐
NAGNIP2 小时前
轻松搞懂全连接神经网络结构!
人工智能·算法·面试
NAGNIP2 小时前
一文搞懂激活函数!
算法·面试
董董灿是个攻城狮3 小时前
AI 视觉连载7:传统 CV 之高斯滤波实战
算法
爱理财的程序媛9 小时前
openclaw 盯盘实践
算法
端平入洛10 小时前
auto有时不auto
c++
Rockbean11 小时前
用40行代码搭建自己的无服务器OCR
服务器·python·deepseek
MobotStone12 小时前
Google发布Nano Banana 2:更快更便宜,图片生成能力全面升级
算法
茶杯梦轩14 小时前
CompletableFuture 在 项目实战 中 创建异步任务 的核心优势及使用场景
服务器·后端·面试
颜酱15 小时前
队列练习系列:从基础到进阶的完整实现
javascript·后端·算法