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

【C ++基础】第五篇 类和对象 日期计算器

【C ++基础】第五篇 类和对象 日期计算器

写在前面

更新情况记录:

最近更新时间更新次数
2022/10/141

参考博客与书籍以及链接:
(非常感谢这些博主们的文章,将我的一些疑问得到解决。)

参考博客链接或书籍名称
总目录:https://blog.csdn.net/m0_54381284/article/details/126810194

正文

文章目录

  • 【C ++基础】第五篇 类和对象 日期计算器
      • 1.前置知识
      • 2.功能描述
      • 3.功能接口及日期类的声明
      • 4.接口的实现
          • 1.构造函数
          • 2.析构函数
          • 3.获取某年某月的天数
          • 4.赋值运算符重载
          • 5.日期+=天数
          • 6.日期+天数
          • 7.日期-天数
          • 8.日期-=天数
          • 9.前置++
          • 10.后置++
          • 11.后置--
          • 12.前置--
          • **13.>运算符重载**
          • 14. ==运算符重载
          • 15. >=运算符重载
          • 16.<运算符重载
          • 17.<=运算符重载
          • 18.!=运算符重载
          • 19.日期-日期 返回天数
      • 5.完整代码
          • 1.Date.h
          • 2.Date.cpp

1.前置知识

直接上我写的博客

C++类和对象

2.功能描述

日期的运算

日期+=天数、日期+天数、日期-天数、日期-=天数、日期前置++、日期后置++、日期前置–、日期后置++、日期-日期返回一个天数。

日期之前比较大小

==、>、>=、<、<=、!=。

3.功能接口及日期类的声明

Date.h

using namespace std;

class Date
{
public:
	// 获取某年某月的天数
	int GetMonthDay(int year, int month);
	//全缺省的构造函数
	Date(int year = 1900, int month = 1, int day = 1);
	//拷贝构造函数
	Date(const Date& d);
	//赋值运算符重载
	Date& operator = (const Date & d);
	// 析构函数
	~Date();
	// 日期+=天数
	Date& operator+=(int day);
	// 日期+天数
	Date operator+(int day);
	// 日期-天数
	Date operator-(int day);
	// 日期-=天数
	Date& operator-=(int day);
	// 前置++
	Date& operator++();
	// 后置++
	Date operator++(int);
	// 后置--
	Date operator--(int);
	// 前置--
	Date& operator--();
	// >运算符重载
	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 operator-(const Date& d);
	//打印日期
	void Print()
	{
		cout << _year << "-" << _month << "-" << _day << endl;
	}
private:
	int _year;
	int _month;
	int _day;
};

4.接口的实现

没有特别说明这里面代码都是放在Date.cpp中。

1.构造函数

写成全缺省的,不需要编译器自己构造

Date.h

Date(int year = 1900, int month = 1, int day = 1);

Date.cpp

Date::Date(int year, int month, int day)
{
	_year = year;
	_month = month;
	_day = day;
}
2.析构函数

又没有开辟空间,编译器自己的够用。

Date::~Date()
{
}
3.获取某年某月的天数
int Date::GetMonthDay(int year, int month)
{
    //加static免得每次调用都要构造
	static int monthDayArray[13] = { 0,31,28,31,30,31,30,31,31,30,31,30,31 };
	if (month == 2 && (year % 4 == 0 && year % 100 != 0) || (year % 400 == 0))
	{
		return 29;
	}
	else
	{
		return monthDayArray[month];
	}
}
4.赋值运算符重载
Date& Date::operator = (const Date& d)
{
	_year = d._year;
	_month = d._month;
	_day = d._day;
	return *this;
}
5.日期+=天数
Date& Date::operator+=(int day)
{
	_day += day;
	while (_day > GetMonthDay(_year, _month))
	{
		_day -= GetMonthDay(_year, _month);
		_month++;

		if (_month == 13)
		{
			++_year;
			_month = 1;
		}
	}
	return *this;
}
6.日期+天数
Date Date::operator+(int day)
{
	Date ret(*this);
	ret += day;
	return ret;
}
7.日期-天数
Date Date::operator-(int day)
{
	Date ret(*this);
	ret -= day;
	return *this;
}
8.日期-=天数
Date& Date::operator-=(int day)
{
	_day -= day;
	while (_day <= 0)
	{
		_day+= GetMonthDay(_year, _month);
		_month--;

		if (_month == 0)
		{
			--_year;
			_month = 12;
		}
	}
	return *this;
}
9.前置++

效果:前置++,每次先++,然后再用

Date& Date::operator++()
{
	*this += 1;
	return *this;
}
10.后置++

效果:后置++,每次先用(保持原值),然后再++

Date Date::operator++(int)
{
	//返回的值没有加加
	Date tmp = *this;
	//但实际上已经加加了
	*this += 1;
	//返回的值没有加加
	return tmp;
}
11.后置–
Date Date::operator--(int)
{
	Date tmp = *this;
	*this -= 1;
	return tmp;
}
12.前置–
Date& Date::operator--()
{
	*this -= 1;
	return *this;
}
13.>运算符重载
bool Date::operator>(const Date& d)
{
	if (_year > d._year)
	{
		return true;
	}
	else if (_year == d._year && _month > d._month)
	{
		return true;
	}
	else if (_year == d._year && _month == d._month && _day > d._day)
	{
		return true;
	}
	return false;
}
14. ==运算符重载
bool Date::operator==(const Date& d)
{
	return (_year == d._year && _day == d._day && _month == d._month);
}
15. >=运算符重载
bool Date::operator >= (const Date& d)
{
	return *this>d||*this==d;
}
16.<运算符重载
bool Date::operator < (const Date& d)
{
	return !(*this > d);
}
17.<=运算符重载
bool Date::operator <= (const Date& d)
{
	return *this < d || *this == d;
}
18.!=运算符重载
bool Date::operator != (const Date& d)
{
	return !(*this == d);
}
19.日期-日期 返回天数

不需要条件判断,用一个n来计数。

int Date::operator-(const Date& d)
{
	Date max = *this;
	Date min = d;
	int flag = 1;
	if (max < min)
	{
		max = d;
		min = *this;
		flag = -1;
	}
	int n = 0;
	while (max != min)
	{
		n++;
		min+=1;
	}
	return n * flag;
}

5.完整代码

1.Date.h
#pragma once
#include<iostream>
using namespace std;

class Date
{
public:
	 获取某年某月的天数
	int GetMonthDay(int year, int month);
	全缺省的构造函数
	Date(int year = 1900, int month = 1, int day = 1);
	拷贝构造函数
	Date(const Date& d);
	赋值运算符重载
	Date& operator = (const Date & d);
	 析构函数
	~Date();
	/// 日期+=天数
	Date& operator+=(int day);
	/// 日期+天数
	Date operator+(int day);
	/// 日期-天数
	Date operator-(int day);
	/// 日期-=天数
	Date& operator-=(int day);
	// 前置++
	Date& operator++();
	// 后置++
	Date operator++(int);
	// 后置--
	Date operator--(int);
	// 前置--
	Date& operator--();
	/// >运算符重载
	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 operator-(const Date& d);
	///打印日期
	void Print()
	{
		cout << _year << "-" << _month << "-" << _day << endl;
	}
private:
	int _year;
	int _month;
	int _day;
};
2.Date.cpp
#define _CRT_SECURE_NO_WARNINGS
#include "Date.h"

Date::Date(int year, int month, int day)
{
	_year = year;
	_month = month;
	_day = day;
}
Date::~Date()
{

}
Date::Date(const Date& d)
{
	_year = d._year;
	_month = d._month;
	_day = d._day;
}
int Date::GetMonthDay(int year, int month)
{
	static int monthDayArray[13] = { 0,31,28,31,30,31,30,31,31,30,31,30,31 };
	if (month == 2 && (year % 4 == 0 && year % 100 != 0) || (year % 400 == 0))
	{
		return 29;
	}
	else
	{
		return monthDayArray[month];
	}
}

Date& Date::operator = (const Date& d)
{
	_year = d._year;
	_month = d._month;
	_day = d._day;
	return *this;
}
Date& Date::operator+=(int day)
{
	_day += day;
	while (_day > GetMonthDay(_year, _month))
	{
		_day -= GetMonthDay(_year, _month);
		_month++;

		if (_month == 13)
		{
			++_year;
			_month = 1;
		}
	}
	return *this;
}
Date Date::operator+(int day)
{
	Date ret(*this);
	ret += day;
	return ret;
}
Date Date::operator-(int day)
{
	Date ret(*this);
	ret -= day;
	return *this;
}
Date& Date::operator-=(int day)
{
	_day -= day;
	while (_day <= 0)
	{
		_day+= GetMonthDay(_year, _month);
		_month--;

		if (_month == 0)
		{
			--_year;
			_month = 12;
		}
	}
	return *this;
}
bool Date::operator>(const Date& d)
{
	if (_year > d._year)
	{
		return true;
	}
	else if (_year == d._year && _month > d._month)
	{
		return true;
	}
	else if (_year == d._year && _month == d._month && _day > d._day)
	{
		return true;
	}
	return false;
}
bool Date::operator==(const Date& d)
{
	return (_year == d._year && _day == d._day && _month == d._month);
}
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);
}
int Date::operator-(const Date& d)
{
	Date max = *this;
	Date min = d;
	int flag = 1;
	if (max < min)
	{
		max = d;
		min = *this;
		flag = -1;
	}
	int n = 0;
	while (max != min)
	{
		n++;
		min+=1;
	}
	return n * flag;
}

Date& Date::operator++()
{
	*this += 1;
	return *this;
}
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;
}

观众老爷们自己写测试用例吧。
本文完毕。

相关文章:

  • SpringBoot+Vue实现前后端分离大学信息及院校推荐网站
  • 编程初学者如何缓解迷茫和焦虑?墙裂推荐此文,助你赢在起跑线
  • [创业之路-42] 创业是只有一小部分人活下来的游戏,探究创业失败的20个主要原因与提高成功率
  • FPGA实现SPI协议
  • 2022年金砖国家职业技能大赛(决赛)网络空间安全赛项 | 浙江赛区选拔赛 任务书
  • 第8章 聚合函数
  • Turbot4机器人入门教程-应用-读取图片文件并发布图像话题
  • Redis的性能优化一些方案
  • 你可能不知道的CSS特征查询
  • 【pygame】之小球基础
  • C++ Reference: Standard C++ Library reference: C Library: cstdarg: va_arg
  • Eclipse技巧(一):快速定位文件的层级位置 | 快速查找文件在工程根目录的位置
  • 汇编笔记[04][内存寻址方式]
  • 开发行业门槛越来越高,Android 开发者的未来之路到底在哪里?
  • Java如何为函数定义一个可变长度的参数呢?
  • 网络传输文件的问题
  • [js高手之路]搞清楚面向对象,必须要理解对象在创建过程中的内存表示
  • 【5+】跨webview多页面 触发事件(二)
  • 【译】理解JavaScript:new 关键字
  • 【跃迁之路】【585天】程序员高效学习方法论探索系列(实验阶段342-2018.09.13)...
  • 【跃迁之路】【669天】程序员高效学习方法论探索系列(实验阶段426-2018.12.13)...
  • Android框架之Volley
  • egg(89)--egg之redis的发布和订阅
  • electron原来这么简单----打包你的react、VUE桌面应用程序
  • iOS编译提示和导航提示
  • Java 实战开发之spring、logback配置及chrome开发神器(六)
  • Java多线程(4):使用线程池执行定时任务
  • Java新版本的开发已正式进入轨道,版本号18.3
  • js正则,这点儿就够用了
  • MyEclipse 8.0 GA 搭建 Struts2 + Spring2 + Hibernate3 (测试)
  • PHP那些事儿
  • Ruby 2.x 源代码分析:扩展 概述
  • SAP云平台运行环境Cloud Foundry和Neo的区别
  • SpringBoot 实战 (三) | 配置文件详解
  • VirtualBox 安装过程中出现 Running VMs found 错误的解决过程
  • 阿里中间件开源组件:Sentinel 0.2.0正式发布
  • 个人博客开发系列:评论功能之GitHub账号OAuth授权
  • 开源地图数据可视化库——mapnik
  • 面试遇到的一些题
  • 前端面试之CSS3新特性
  • 嵌入式文件系统
  • 用jquery写贪吃蛇
  • 鱼骨图 - 如何绘制?
  • 这几个编码小技巧将令你 PHP 代码更加简洁
  • 机器人开始自主学习,是人类福祉,还是定时炸弹? ...
  • (2)nginx 安装、启停
  • (Redis使用系列) Springboot 使用redis实现接口幂等性拦截 十一
  • (Redis使用系列) SpringBoot中Redis的RedisConfig 二
  • (顶刊)一个基于分类代理模型的超多目标优化算法
  • (十五)Flask覆写wsgi_app函数实现自定义中间件
  • (转)Groupon前传:从10个月的失败作品修改,1个月找到成功
  • (转)Linux整合apache和tomcat构建Web服务器
  • ***汇编语言 实验16 编写包含多个功能子程序的中断例程
  • .Net Remoting(分离服务程序实现) - Part.3
  • .net Signalr 使用笔记