C++实现Date类各种运算符重载的示例代码

 更新时间:2024年02月18日 09:38:59   作者:伤心男孩拯救世界(Code King)  
这篇文章主要为大家详细介绍了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);
};

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

以上就是C++实现Date类各种运算符重载的示例代码的详细内容,更多关于C++运算符重载的资料请关注脚本之家其它相关文章!

相关文章

  • QT编写地图实现在线轮廓图的示例代码

    QT编写地图实现在线轮廓图的示例代码

    轮廓图也叫行政区划,这里的轮廓图是指百度地图的区域轮廓图。本文将为大家介绍QT如何实现在线轮廓图的编写,感兴趣的小伙伴可以跟随小编一起学习一下
    2021-12-12
  • C++ abs函数实际应用详解

    C++ abs函数实际应用详解

    本文我们来讲C++的abs函数以及实战运用,C++中的abs函数。在C++中使用abs函数要注意存在两种版本,一种是在stdlmb.h中定义的版本,另一个是在cmath头文件中定义的。夷实上在stdlib.h文件是C的函数,而cmath中的是C++版本
    2022-08-08
  • C++基于随机数实现福彩双色球的方法示例

    C++基于随机数实现福彩双色球的方法示例

    这篇文章主要介绍了C++基于随机数实现福彩双色球的方法,结合完整实例形式分析了C++随机数算法的实现与使用技巧,需要的朋友可以参考下
    2017-06-06
  • OpenCV利用霍夫变换实现交通车道线检测

    OpenCV利用霍夫变换实现交通车道线检测

    经典霍夫变换用来检测图像中的直线,后来霍夫变换经过扩展可以进行任意形状物体的识别,例如圆和椭圆。本文就来利用霍夫变换实现交通车道线检测,需要的可以参考一下
    2022-09-09
  • C语言实现24点问题详解

    C语言实现24点问题详解

    24点问题就是在屏幕上输入1〜10范围内的4个整数(可以有重复),对它们进行加、减、乘、除四则运算后(可以任意的加括号限定计算的优先级),寻找计算结果等于24的表达式。本文将通过C语言实现24点问题的求解,需要的可以参考一下
    2021-12-12
  • 详解C++ OpenCV实现图像拼接的原理及方法

    详解C++ OpenCV实现图像拼接的原理及方法

    本文以实现图像拼接为目标,把分割开的图像进行拼接还原,核心的内容包括:OpenCV图像拼接相关原理以及OpenCV图像拼接案例的实现,感兴趣的可以了解一下
    2022-07-07
  • Linux中rm命令使用以及C/C++代码实现

    Linux中rm命令使用以及C/C++代码实现

    m 是remove 的缩写,Linux中 rm 命令的功能为删除一个目录中的一个或多个文件或目录,它也可以将某个目录及其下的所有文件及子目录均删除,这篇文章主要给大家介绍了关于Linux中rm命令使用以及C/C++代码实现的相关资料,需要的朋友可以参考下
    2022-04-04
  • VC实现动态菜单的创建方法

    VC实现动态菜单的创建方法

    这篇文章主要介绍了VC实现动态菜单的创建方法,需要的朋友可以参考下
    2014-07-07
  • 基于C语言实现的贪吃蛇游戏完整实例代码

    基于C语言实现的贪吃蛇游戏完整实例代码

    这篇文章主要介绍了基于C语言实现的贪吃蛇游戏完整实例代码,对于学习游戏开发的朋友有一定的借鉴价值,需要的朋友可以参考下
    2014-08-08
  • C++11非受限联合体的使用

    C++11非受限联合体的使用

    本文主要介绍了C++11非受限联合体的使用,文中通过示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2022-01-01

最新评论