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;
	}

}
相关推荐
TracyCoder12319 分钟前
LeetCode Hot100(1/100)——1. 两数之和 (Two Sum)
算法·leetcode
进击的小头24 分钟前
常用数字滤波器的特性与适用场景
c语言·算法
weixin_395448911 小时前
tidl_import_mul_rmfsd_psd_u8_3x480x544_bise_raw_dynamic.txt
java·服务器·前端
狐571 小时前
2026-01-19-LeetCode刷题笔记-1292-元素和小于等于阈值的正方形的最大边长
笔记·算法·leetcode
Maggie_ssss_supp1 小时前
Linux-Percona XtraDB Cluster (PXC)集群部署实战
linux·运维·服务器
张祥6422889041 小时前
误差理论与测量平差基础笔记六
笔记·算法·概率论
txinyu的博客1 小时前
std::function
服务器·开发语言·c++
吃花椒的冰冰1 小时前
ubuntu自动检测断网重联
运维·服务器
刘哥测评技术zcwz6262 小时前
希音shein自养号测评怎么做,有哪些技术要求
运维·服务器·网络
a程序小傲2 小时前
国家电网面试被问:FactoryBean与BeanFactory的区别和动态代理生成
java·linux·服务器·spring boot·spring·面试·职场和发展