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

【C++】实现Date类的各种运算符重载

在这里插入图片描述

上一篇文章只实现了operator==操作符重载,由于运算符较多,该篇文章单独实现剩余所有的运算符重载。继续以Date类为例,实现运算符重载:
1.Date.h

#pragma once#include <iostream>
#include <assert.h>using namespace std;class Date
{
private:int _year;int _month;int _day;
public:void Print();Date(int yaer, int month, int day);bool operator<(const Date& d);bool operator<=(const Date& d);bool operator>(const Date& d);bool operator>=(const Date& d);bool operator==(const Date& d);bool operator!=(const Date& d);//单独的用一个函数把每个月多少天,封装起来int GetMonthDays(int year, int month){assert(month > 0 && month < 13);static int MonthDay[13] = { 0,31,28,31,30,31,30,31,31,30,31,30,31 };if (month==2&&(year % 4 == 0 && year % 100 != 0)){return 29;}return MonthDay[month];}Date& operator+=(int day);Date operator+(int day);Date& operator-=(int day);Date operator-(int day);//++d,前置++Date& operator++();//d++,后置++Date operator++(int);//前置--Date& operator--();//后置--Date operator--(int);//两个日期相减:d1-d2int operator-(const Date& d);
};

Date.cpp

#define _CRT_SECURE_NO_WARNINGS 1 
#include <stdio.h>#include "Date.h"void Date::Print()
{cout << _year << "/" << _month << "/" << _day << endl;
}Date::Date(int year=1, int month=1, int day=1)
{_year = year;_month = month;_day = day;
}
//   写好一个直接复用!!!
bool Date::operator<(const Date& d)
{if (_year < d._year){return true;}else if (_year == d._year){if (_month < d._month)return true;else if ((_month == d._month) && (_day < d._day))return true;elsereturn false;}elsereturn false;
}bool Date::operator==(const Date& d)
{if ((_year == d._year) && (_month == d._month) && (_day == d._day))return true;elsereturn false;
}bool Date::operator<=(const Date& d)
{return *this == d || *this < d;
}bool Date::operator>(const Date& d)
{return !(*this <= d);
}bool Date::operator>=(const Date& d)
{return (*this > d || *this == d);
}bool Date::operator!=(const Date& d)
{return !(*this == d);
}Date& Date::operator+=(int day)
{_day += day;//先加//这里是while,因为如果是if的话,如果一次加了很大的数据减一次不一定能减得完!!!while(_day > GetMonthDays(_year, _month)){_day -= GetMonthDays(_year, _month);++_month;if (_month == 13){++_year;_month = 1;}}return *this;
}Date Date::operator+(int day)
{Date tmp=*this;tmp += day;return tmp;
}Date& Date::operator-=(int day)
{_day -= day;while (_day <= 0){--_month;if (_month == 0){--_year;_month = 12;}_day += GetMonthDays(_year, _month);}return *this;
}Date Date::operator-(int day)
{Date tmp = *this;tmp -= day;return tmp;
}Date& Date::operator++()
{return *this += 1;
}Date Date::operator++(int) 
{Date tmp = *this;*this += 1;return tmp;
}Date& Date::operator--()
{*this - 1;return *this;
}Date Date::operator--(int)
{Date tmp = *this;*this -= 1;return tmp;
}
//日期-日期,计算两个日期之间相差多少天int Date::operator-(const Date& d)
{int flag = 1;Date max = *this;Date min = d;if (*this < d){//赋值为-1的原因:因为这个函数是有顺序的d1-d2,如果遇到d1<d2,也就是小减大,最终返回的结果是个负数,所以说这里要变成-1。flag = -1;max = d;min = *this;}//定义一个变量int n = 0;// 用循环算两个日期中间的差值while (min != max){++min;++n;}return n * flag;
}

3.Test.cpp

#define _CRT_SECURE_NO_WARNINGS 1 
#include <stdio.h>
#include "Date.h"int main()
{Date d1(2024, 2, 15);Date d2 = d1 + 20;d1.Print();d2.Print();bool ret=d1 > d2;if (ret){d1.Print();}d2 += 100;d2.Print();d2 -= 100;d2.Print();Date d3 = d2 - 10;d3.Print();Date d4(2024, 1, 29);Date d5(2024, 8, 1);cout << d5 - d4 << endl;++d5;d5.Print();d5++;d5.Print();--d5;d5.Print();d5--;d5.Print();return 0;
}
http://www.lryc.cn/news/301160.html

相关文章:

  • 【Linux】程序地址空间 -- 详解 Linux 2.6 内核进程调度队列 -- 了解
  • 10-通用类型、特质和生命周期
  • STM32CubeMX,定时器之定时功能,入门学习,如何设置prescaler,以及timer计算PWM输入捕获方法(重要)
  • 蓝桥杯:C++队列、优先队列、链表
  • 【C语言】长篇详解,字符系列篇1-----“混杂”的各种字符类型字符转换和strlen的模拟实现【图文详解】
  • 2024年【高处安装、维护、拆除】考试总结及高处安装、维护、拆除考试技巧
  • 开源无处不在,发展创新下又有何弊端
  • LeetCode 0429.N 叉树的层序遍历:广度优先搜索(BFS)
  • Practical User Research for Enterprise UX
  • 文生视频:Sora模型报告总结
  • GA 374-2019 电子防盗锁检测
  • 代码随想录day26 Java版
  • 英文论文(sci)解读复现【NO.21】一种基于空间坐标的轻量级目标检测器无人机航空图像的自注意
  • 数据集合
  • php基础学习之作用域和静态变量
  • SP1:基于Plonky3构建的zkVM
  • Python爬虫之文件存储#5
  • Spring Boot 笔记 012 创建接口_添加文章分类
  • Spring-面试题
  • Flink理论—容错之状态
  • 【数据结构】链表OJ面试题5《链表的深度拷贝》(题库+解析)
  • 智慧校园规划建设方案
  • 003 - Hugo, 创建文章
  • HCIA-HarmonyOS设备开发认证V2.0-IOT硬件子系统-GPIO
  • 《Java 简易速速上手小册》第7章:Java 网络编程(2024 最新版)
  • 用keras对电影评论进行情感分析
  • 每日OJ题_算法_递归④力扣24. 两两交换链表中的节点
  • 110 C++ decltype含义,decltype 主要用途
  • PYTHON 120道题目详解(85-87)
  • 【Linux】Linux编译器-gcc/g++ Linux项目自动化构建工具-make/Makefile