关系运算符重载
来源:互联网 发布:样本册设计软件 编辑:程序博客网 时间:2024/06/07 00:52
关系运算符重载
类的声明
//// Created by Rdw on 2017/3/9.//#ifndef PROJECT5_TIME_H#define PROJECT5_TIME_Hclass Time {private: int hour; int minute;public: Time();//默认构造函数 Time(int h , int m);//构造函数 ~Time();//析构函数 void show() const; void reset(int h , int m); /*重载关系运算符*/ friend bool operator==(const Time &object1 , const Time &object2); friend bool operator!=(const Time &object1 , const Time &object2); friend bool operator<(const Time &object1 , const Time &object2);};#endif //PROJECT5_TIME_H
类的定义
//// Created by Rdw on 2017/3/9.//#include "Time.h"#include <iostream>using namespace std;Time::Time() {}Time::Time(int h, int m) { hour = h; minute = m;}Time::~Time() {}void Time::reset(int h, int m) { hour = h; minute = m;}void Time::show() const { cout << hour << "hours " << minute << "minutes" << endl;}bool operator==(const Time &object1, const Time &object2) { return (object1.hour == object2.hour && object1.minute == object2.minute);}bool operator!=(const Time &object1, const Time &object2) { return !(object1 == object2);}bool operator<(const Time &object1, const Time &object2) { int temp1 = object1.hour * 60 + object1.minute; int temp2 = object2.hour * 60 + object2.minute; return temp1 < temp2;}
类的使用
#include <iostream>#include "Time.h"using namespace std;int main() { Time time11 = Time(9 , 30); Time time12 = Time(10 , 30); Time time13 = Time(9 , 30); if (time11 == time12) cout << "OK!!!" << endl; else cout << "NOT OK!!!" << endl; if (time11 < time12) cout << "<" << endl; else cout << ">" << endl;}
测试结果
E:\Project5\cmake-build-debug\Project5.exe NOT OK!!!<Process finished with exit code 0
0 0
- 关系运算符重载
- C++关系运算符重载
- C++语言--运算符重载--4.1--一般运算符重载、关系运算符重载、逻辑运算符重载等
- C++重载复合赋值运算符、算术运算符和关系运算符
- 运算符重载 [++,--,+,<<; &&,||(不可重载)]
- 重载之运算符重载
- C++ Primer笔记11_运算符重载_算术/关系运算符_下标运算符
- 重载赋值运算符
- C++运算符重载
- 运算符重载
- 运算符重载总结
- 运算符重载例子
- 运算符重载
- 运算符重载
- 基础:运算符重载
- 运算符重载
- 运算符重载
- 重载赋值运算符
- (1) linux上安装redis
- IO 的底层实现问题
- url编码
- NoSQL Manager for MongoDB 试用期过期怎么办?破解
- 『竞赛』机器学习、数据挖掘相关的竞赛
- 关系运算符重载
- Zend Framework:view helper -- Placeholder Helper && RenderToPlcarholder Helper
- 老牌会议驾到:第九届中国Hadoop技术峰会2017北京站
- Shiro之身份验证
- Objective-C之协议、代码块、分类、扩展
- Elasticsearch基础教程
- Android自定义控件波浪线工具类
- Python实例:网络爬虫抓取豆瓣3万本书(1)
- HTTP协议状态码