当前位置: 首页 > 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-d2
	int 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;
		else
			return false;
	}
	else
		return false;
}

bool Date::operator==(const Date& d)
{
	if ((_year == d._year) && (_month == d._month) && (_day == d._day))
		return true;
	else
		return 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;
}

相关文章:

  • 【机器学习笔记】 9 集成学习
  • 第73左侧菜单实现
  • 【NI-DAQmx入门】调整数据记录长度再进行数据处理
  • 第二节 zookeeper基础应用与实战
  • 多维时序 | Matlab实现TCN-RVM时间卷积神经网络结合相关向量机多变量时间序列预测
  • C 语言 devc++ 使用 winsock 实现 windows UDP 局域网发送消息
  • 设计模式之结构型模式
  • 通过 Prometheus 编写 TiDB 巡检脚本(脚本已开源,内附链接)
  • TCP/IP C 语言实现单个客户端和服务端 TCP 通信
  • 【FPGA开发】HDMI通信协议解析及FPGA实现
  • HiveSQL——共同使用ip的用户检测问题【自关联问题】
  • CVE-2022-25487 漏洞复现
  • 排序算法---计数排序
  • 安全基础~通用漏洞5
  • 【Spring2】从重构HelloWorld看Spring的作用
  • 面试经典150题——最小覆盖子串(困难)
  • 微信小程序(四十一)wechat-http的使用
  • Java解决下降路径最小和
  • C++数据结构与算法——双指针法
  • BatchNorm介绍:卷积神经网络中的BN
  • 过去24小时中美是否就关税问题进行过接触?外交部:没有
  • 上海市十六届人大常委会第二十一次会议表决通过有关人事任免事项
  • 巴西外长维埃拉:国际形势日益复杂,金砖国家必须发挥核心作用
  • 运动健康|不同能力跑者,跑步前后营养补给差别这么大?
  • 举牌超200轮!中铁建7.76亿元竞得北京通州梨园宅地
  • 费高云调研党的建设工作:营造风清气正劲足的政治生态