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

}
相关推荐
一个不知名程序员www31 分钟前
算法学习入门---vector(C++)
c++·算法
云飞云共享云桌面37 分钟前
无需配置传统电脑——智能装备工厂10个SolidWorks共享一台工作站
运维·服务器·前端·网络·算法·电脑
明洞日记43 分钟前
【数据结构手册002】动态数组vector - 连续内存的艺术与科学
开发语言·数据结构·c++
福尔摩斯张1 小时前
《C 语言指针从入门到精通:全面笔记 + 实战习题深度解析》(超详细)
linux·运维·服务器·c语言·开发语言·c++·算法
橘颂TA1 小时前
【剑斩OFFER】算法的暴力美学——两整数之和
算法·leetcode·职场和发展
虚伪的空想家1 小时前
arm架构服务器使用kvm创建虚机报错,romfile “efi-virtio.rom“ is empty
linux·运维·服务器·javascript·arm开发·云原生·kvm
深藏bIue2 小时前
linux服务器mysql目录下的binlog文件删除
linux·服务器·mysql
Dream it possible!2 小时前
LeetCode 面试经典 150_二叉搜索树_二叉搜索树的最小绝对差(85_530_C++_简单)
c++·leetcode·面试
xxxxxxllllllshi2 小时前
【LeetCode Hot100----14-贪心算法(01-05),包含多种方法,详细思路与代码,让你一篇文章看懂所有!】
java·数据结构·算法·leetcode·贪心算法
前端小L2 小时前
图论专题(二十二):并查集的“逻辑审判”——判断「等式方程的可满足性」
算法·矩阵·深度优先·图论·宽度优先