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

}
相关推荐
TracyCoder1233 小时前
LeetCode Hot100(15/100)——54. 螺旋矩阵
算法·leetcode·矩阵
开发者小天3 小时前
python中For Loop的用法
java·服务器·python
绾樘3 小时前
RHCE--基于Nginx的Web服务器配置
运维·服务器·nginx
生活很暖很治愈3 小时前
Linux基础开发工具
linux·服务器·git·vim
u0109272714 小时前
C++中的策略模式变体
开发语言·c++·算法
2501_941837264 小时前
停车场车辆检测与识别系统-YOLOv26算法改进与应用分析
算法·yolo
Aevget5 小时前
MFC扩展库BCGControlBar Pro v37.2新版亮点:控件功能进一步升级
c++·mfc·界面控件
六义义5 小时前
java基础十二
java·数据结构·算法
四维碎片5 小时前
QSettings + INI 笔记
笔记·qt·算法
Tansmjs5 小时前
C++与GPU计算(CUDA)
开发语言·c++·算法