当前位置: 首页 > news >正文

c++ 三目运算符在类中的使用

简介

在类比较方面,三目运算符可以用于重载比较运算符。

代码示例1

#include <iostream>
#include <cstring>class Person {
public:Person(const char* name, int age) : m_age(age) {m_name = new char[strlen(name) + 1];strcpy(m_name, name);}~Person() {delete[] m_name;}bool operator==(const Person& other) const {return m_age == other.m_age && strcmp(m_name, other.m_name) == 0;}bool operator!=(const Person& other) const {return !(*this == other);}private:char* m_name;int m_age;
};int main() {Person john("John", 25);Person jane("Jane", 27);Person john2("John", 25);std::cout << "john == jane: " << ((john == jane) ? "true" : "false") << std::endl;std::cout << "john != jane: " << ((john != jane) ? "true" : "false") << std::endl;std::cout << "john == john2: " << ((john == john2) ? "true" : "false") << std::endl;return 0;
}

输出结果如下

在这里插入图片描述

代码实例2

三目运算符可以在继承类中使用,与之前的示例演示无异。比较运算符重载同样可以在继承类中使用,子类可以重载父类的比较运算符。例如,以下是一个使用继承和比较运算符重载的例子

#include <iostream>
#include <cstring>class Person {
public:Person(const char* name, int age) : m_age(age) {m_name = new char[strlen(name) + 1];strcpy(m_name, name);}~Person() {delete[] m_name;}bool operator==(const Person& other) const {return m_age == other.m_age && strcmp(m_name, other.m_name) == 0;}protected:char* m_name;int m_age;
};class Student : public Person {
public:Student(const char* name, int age, int score) : Person(name, age), m_score(score) {}bool operator==(const Student& other) const {return Person::operator==(other) && m_score == other.m_score;}private:int m_score;
};int main() {Person john("John", 25);Person jane("Jane", 27);Student john2("John", 25, 90);Student john3("John", 25, 91);std::cout << "john == jane: " << ((john == jane) ? "true":"false") << std::endl;std::cout << "john == john2: " << ((john == john2) ? "true" : "false") << std::endl;std::cout << "john2 == john3: " << ((john2 == john3) ? "true" : "false") << std::endl;return 0;
}

在这个示例中,我们定义了两个类 Person 和 Student,Student 类继承自 Person 类,并添加了一个名为 m_score 的成员变量和一个名为 operator== 的比较运算符重载。在 operator== 函数中,我们首先调用基类的 operator== 方法,然后比较 m_score 的值。

输出结果如下

在这里插入图片描述

相关文章:

  • 蓝桥杯 动态规划
  • 深入理解Linux用户权限管理:常用命令详解
  • 【译】IOC容器2
  • 前端时间的失败总结复盘
  • 对标Gen-2!Meta发布新模型,进军文生视频赛道
  • idea使用问题(idea相关)快捷键及窗口没有service
  • threejs中获取不规则物体中心点坐标
  • 使用Redis实现接口防抖
  • 字节跳动开源基于SD1.5的 MagicAnimate 一张照片秒变真人舞蹈视频
  • 灰度发布专题---3、Nginx+Lua灰度发布
  • C++空类的那点事儿
  • 虾皮在线定价工具:知虾轻松制定有竞争力的价格策略
  • 一张网页截图,AI帮你写前端代码,前端窃喜,终于不用干体力活了
  • Python技术操作1-高效办公:将文本、图片和表格信息批量写入Word文档
  • QList简单使用
  • 【MySQL经典案例分析】 Waiting for table metadata lock
  • 30天自制操作系统-2
  • C语言笔记(第一章:C语言编程)
  • Docker 1.12实践:Docker Service、Stack与分布式应用捆绑包
  • golang 发送GET和POST示例
  • JavaScript实现分页效果
  • Laravel 实践之路: 数据库迁移与数据填充
  • Redis中的lru算法实现
  • SpiderData 2019年2月16日 DApp数据排行榜
  • spring security oauth2 password授权模式
  • Theano - 导数
  • VuePress 静态网站生成
  • 聊聊redis的数据结构的应用
  • 前端js -- this指向总结。
  • 实现简单的正则表达式引擎
  • 小试R空间处理新库sf
  • 在Docker Swarm上部署Apache Storm:第1部分
  • 阿里云ACE认证之理解CDN技术
  • ​软考-高级-信息系统项目管理师教程 第四版【第14章-项目沟通管理-思维导图】​
  • !!【OpenCV学习】计算两幅图像的重叠区域
  • #数学建模# 线性规划问题的Matlab求解
  • #中国IT界的第一本漂流日记 传递IT正能量# 【分享得“IT漂友”勋章】
  • (delphi11最新学习资料) Object Pascal 学习笔记---第2章第五节(日期和时间)
  • (java版)排序算法----【冒泡,选择,插入,希尔,快速排序,归并排序,基数排序】超详细~~
  • (Matlab)遗传算法优化的BP神经网络实现回归预测
  • (二)windows配置JDK环境
  • (附源码)springboot太原学院贫困生申请管理系统 毕业设计 101517
  • (四)汇编语言——简单程序
  • (学习日记)2024.04.04:UCOSIII第三十二节:计数信号量实验
  • (转)甲方乙方——赵民谈找工作
  • *_zh_CN.properties 国际化资源文件 struts 防乱码等
  • .NET Core Web APi类库如何内嵌运行?
  • .NET/C# 使用反射调用含 ref 或 out 参数的方法
  • .NET/C# 推荐一个我设计的缓存类型(适合缓存反射等耗性能的操作,附用法)
  • .net6+aspose.words导出word并转pdf
  • .net经典笔试题
  • .NET开源全面方便的第三方登录组件集合 - MrHuo.OAuth
  • .NET平台开源项目速览(15)文档数据库RavenDB-介绍与初体验
  • @AutoConfigurationPackage的使用
  • @RestControllerAdvice异常统一处理类失效原因