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

}
相关推荐
日取其半万世不竭1 天前
Minecraft Java版社区服务器搭建教程(Linux,适合新手)
java·linux·服务器
aseity1 天前
跨平台项目中QString 与 非Qt 跨平台动态库在字符集上的一个实用的互操作约定.
c++·经验分享
时空自由民.1 天前
蓝牙协议之GAP协议
linux·服务器·网络
CN-Dust1 天前
【C++】while语句例题专题
数据结构·c++·算法
用户805533698031 天前
现代Qt开发教程(新手篇)1.11——定时器
c++·qt
澈2071 天前
STL迭代器:容器遍历的万能钥匙
开发语言·c++
灵智实验室1 天前
PX4位置速度估计技术详解(四):LPE 激光雷达高度融合的实现错误
算法·无人机·px 4
azoo1 天前
emplace_back和push_back() 函数添加 cv::Point 类型数据
c++·opencv
byoass1 天前
企业云盘与设计软件深度集成:AutoCAD/Revit/SolidWorks插件开发与API集成实战
服务器·网络·数据库·安全·oracle·云计算
leaves falling1 天前
Linux 基础指令完全指南 —— 从入门到熟练
linux·运维·服务器