IT数码 购物 网址 头条 软件 日历 阅读 图书馆
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
图片批量下载器
↓批量下载图片,美女图库↓
图片自动播放器
↓图片自动播放器↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁
 
   -> C++知识库 -> 【C++】日期类的实现 -> 正文阅读

[C++知识库]【C++】日期类的实现

一、日期类声明

// 日期类声明
class Date
{
public:
private:
	int _year;
	int _month;
	int _day;
};`

.h

#pragma once
#include<iostream>
// 只展开常用的防止命名冲突
using std::cin;
using std::cout;
using std::endl;

二、日期类接口声明

// 日期类声明
class Date
{
public:
	// 构造函数
	Date(int year = 0, int month = 1, int day = 1);
	// 打印年月日
	void Print();
	// 获取某年某月的天数
	int GetMonthDay(int year, int month);
	// 日期+=天数
	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);
	// >=运算符重载
	inline 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);
private:
	int _year;
	int _month;
	int _day;
};

日期类的拷贝构造,析构,复制重载用默认生成的就行。


三、接口实现

3.1 构造函数及检查日期合法

Date::Date(int year, int month, int day)
{
	//判断日期是否合法
	if (year >= 0
		&& month >= 1 && month <= 12
		&& day >= 0 && day <= GetMonthDay(year, month))
	{
		_year = year;
		_month = month;
		_day = day;
	}
	else
	{
		cout << "非法日期" << endl;
	}
}

注意缺省参数在声明和定义只能出现一处,GetMonthDay函数在后面讲解。


3.2 打印日期

void Date::Print()
{
	cout << _year << "年" << _month << "月" << _day << "日" << endl;
}

3.3 获取某年某月的天数

inline int Date::GetMonthDay(int year, int month)
{
	// 0对应0
	int Day[13] = { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
	int day = Day[month];
	// 特殊情况:闰年2月
	if (month == 2 && (((year % 4 == 0) && (year % 100 != 0)) || year % 400 == 0))
	{
		day++;
	}
	return day;
}

注意两点:

1)创建数组要创建13个位置,因为要对应“0月”。
2)因为要多次调用,所以设置成内敛函数


3.4 日期+=天数

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

能传引用就传引用,提高效率(减少拷贝)


3.5 日期+天数

Date Date::operator+(int day)
{
	Date tmp = *this;
	tmp._day += day;
	while (tmp._day > GetMonthDay(tmp._year, tmp._month))
	{
		tmp._day -= GetMonthDay(tmp._year, tmp._month);
		tmp._month++;
		if (tmp._month > 12)
		{
			tmp._year++;
			tmp._month = 1;
		}
	}
	return tmp;
}

+的意思是不能改变原来的值,所以要创建临时变量返回,并且不能用引用返回(出作用域后销毁)。
我们发现我们写过+=,所以我们可以采用复用

Date Date::operator+(int day)
{
	Date tmp(*this);//拷贝构造
	tmp += day;
	return tmp;
}

3.6 日期-=天数

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

要注意day为负数的情况。上面的+=也同理。


3.7 日期-天数

Date Date::operator-(int day)
{
	Date tmp(*this);
	tmp -= day;
	return tmp;
}

3.8 前置++和后置++

前置++和后置++都完成了++,不同的地方在于返回值不同
由于无法分辨前置和后置,所以函数名修饰规则:后面加int参数为后置,不加为前置
前置++:

Date& Date::operator++()
{
	*this += 1;
	return *this;
}

后置++:

Date Date::operator++(int)
{
	Date tmp(*this);
	*this += 1;
	return tmp;
}

3.9 前置–和后置–

前置–:

Date& Date::operator--()
{
	*this -= 1;
	return *this;
}

后置–:

Date Date::operator--(int)
{
	Date tmp(*this);
	*this -= 1;
	return tmp;
}

3.10 ==

bool Date::operator==(const Date& d)
{
	return _day == d._day
		&& _month == d._month
		&& _year == d._year;
}

3.11 比较符号

>

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)
		{
			if (_day > d._day)
			{
				return true;
			}
		}
	}
	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);
}

3.12 日期-日期 返回天数

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


  C++知识库 最新文章
【C++】友元、嵌套类、异常、RTTI、类型转换
通讯录的思路与实现(C语言)
C++PrimerPlus 第七章 函数-C++的编程模块(
Problem C: 算法9-9~9-12:平衡二叉树的基本
MSVC C++ UTF-8编程
C++进阶 多态原理
简单string类c++实现
我的年度总结
【C语言】以深厚地基筑伟岸高楼-基础篇(六
c语言常见错误合集
上一篇文章      下一篇文章      查看所有文章
加:2022-07-17 16:02:42  更:2022-07-17 16:02:47 
 
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁

360图书馆 购物 三丰科技 阅读网 日历 万年历 2024年5日历 -2024/5/12 1:15:16-

图片自动播放器
↓图片自动播放器↓
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
图片批量下载器
↓批量下载图片,美女图库↓
  网站联系: qq:121756557 email:121756557@qq.com  IT数码