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

【C++】———— 日期类练习

目录

前言

时间类定义

成员函数具体实现 

1.获取某年某月天数

 2.构造函数

3.析构函数 

4.拷贝构造

5.日期类的运算符重载

5.1赋值运算符重载 

5.2 ++ 和 - - 的前置后置

5.3日期比较(> , < , >= ,<= ,== ,!=)

 5.4 += 、+ 、- 、-= 

5.5日期减日期,求差值 

前言

对于C++类与对象的练习。

我们生活中离不开时间,今天来实现一个简易日期计算器

方法:

1.定义一个时间类,包括成员函数的声明和成员变量的定义。

2.实现相关功能(例如:计算时间差、加减天数等等)。

3.调试优化。

时间类定义

#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);

	// 赋值运算符重载
  // d2 = d3 -> d2.operator=(&d2, d3)
	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();
private:
	int _year;
	int _month;
	int _day;
};

成员函数具体实现 

1.获取某年某月天数

int Date::GetMonthDay(int year, int month)
	{
		int array[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)))
		{
			array[2]++;
		}
		return array[month];
	}

 2.构造函数

实现日期类年月日的初始化,我们实现的是全缺省的构造,不传值就默认1900年1月1日。

// 全缺省的构造函数
	Date::Date(int year, int month , int day)
	{
		_year = year;
		_month = month;
		_day = day;
	}

这里需要注意的是构造函数定义和声明分离的情况下在声明处定义缺省值。 

3.析构函数 

 析构函数用于清理资源,在对象生命周期结束自动调用。如果类没有申请资源,其实可以不写,直接调用编译器自动生成的默认析构函数。所以我们就没写。

4.拷贝构造

// 拷贝构造函数
  // d2(d1)
	Date::Date(const Date& d)
	{
		_year = d._year;
		_month = d._month;
		_day = d._day;
	}

5.日期类的运算符重载

5.1赋值运算符重载 

Date& Date::operator=(const Date& d)
	{
		if (*this != d)//防止自己给自己赋值
		{
			_year = d._year;
			_month = d._month;
			_day = d._day;
		}
		
		return *this;//支持连续复制 eg:d1 = d2 = d3;
	}

如果用户没有显式实现赋值重载,编译器会默认生成,以值的方式按字节拷贝(浅拷贝)。内置类型直接复制,自定义类型成员变量需要调用对应类的赋值重载。

5.2 ++ 和 - - 的前置后置

先说++,前置++和后置++都是一元运算符,为了让前置++与后置++形成能正确重载 C++规定:后置++重载时多增加一个int类型的参数,但调用函数时该参数不用传递,编译器自动传递。

 前置++:

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

函数销毁,*this还存在,故选择引用作为返回值。 

 后置++:

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

函数销毁,tmp就没了。故传址返回。

那么前置-- 和后置-- 一个道理:

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

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

5.3日期比较(> , < , >= ,<= ,== ,!=)

这类的成员函数通常可以相互复用,我们写 == 、>。其他复用。

== :

// ==运算符重载
	bool Date::operator==(const Date& d)
	{
		return _year == d._year
			&& _month == d._month
			&& _day == d._day;
	}

 > :

// >运算符重载
	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;
		}
		else
		{
			return false;
		}
	}

其他复用即可,举一个例子,例如 >=:

// >=运算符重载
	bool Date::operator >= (const Date& d)
	{
		return *this > d
			|| *this == d;
	}

 5.4 += 、+ 、- 、-= 

有了上面的经验,其实+= 和 +也是可以相互复用的。我们先写+= :

// 日期+=天数
	// Date ret = d1 + 100; 
	Date& Date::operator+=(int day)
	{
		if (day < 0)
		{
			return *this -= (-day);
		}//防止穿 += 负值
		_day += day;
		while (_day > GetMonthDay(_year, _month))
		{
			_day -= GetMonthDay(_year, _month);
			_month++;
			if (_month > 12)
			{
				_month -= 12;
				_year++;
			}
		}
		return *this;
	}

+(复用+=):

// 日期+天数
	Date Date::operator+(int day)
	{
		Date tmp = *this;
		tmp += day;
		return tmp;
	}

 我们再先写+,让+=复用+:

Date Date::operator+(int day)
	{
		Date tmp(*this);
		if (day < 0)
		{
			return tmp -= (-day);
		}
		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._month -= 12;
				tmp._year++;
			}
		}
		return tmp;
	}
Date& Date::operator+=(int day)
	{
		return *this = *this + day;
	}

我们对比两种,可以发现第一种比较好。因为第一种拷贝构造调用的少。 

- 和-= 也是一个道理,不一样的可能只是代码实现的逻辑:

// 日期-天数
	Date Date::operator-(int day)
	{
		Date tmp = *this;
		tmp -= day;
		return tmp;
	}


	// 日期-=天数
	Date& Date::operator-=(int day)
	{	
		if (day < 0)
		{
			return *this += (-day);
		}
		_day -= day;
		while (_day<=0)
		{
			_month--;
			if (_month == 0)
			{
				_year--;
				_month = 12;
			}
			_day += GetMonthDay(_year,_month);
		}

		return *this;
	}

5.5日期减日期,求差值 

// 日期-日期 返回天数

	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 day = 0;
		while (min != max)
		{
			++min;
			++day;
		}

		return day * flag;
	}

代码逻辑其实很简单。用计数器day求差值。 


 今天的分享就到这里!!!


http://www.kler.cn/a/349197.html

相关文章:

  • 【LwIP源码学习3】TCP协议栈分析——数据接收流程
  • 深入解析单片机原理及其物联网应用:附C#示例代码
  • 关于this指针
  • K8s的储存
  • springboot 集成flyway数据库迁移版本控制详细教程
  • C++list
  • 初级网络工程师之从入门到入狱(七)
  • 如何使用 Puppeteer 和 Browserless 运行自动化测试?
  • 期货数据接口 - 包含实时+历史数据
  • rtsp协议:rtsp协议参数介绍
  • Leetcode热题100-200 岛屿数量
  • 2.1 机器学习--KNN算法(分类)
  • ARP欺骗的多种手法
  • 基于离群点修正、优化分解和DLinear模型的多步风速预测方法
  • 【Vue】vue中针对地址栏参数进行加解密
  • 深入理解Transformer的笔记记录(精简版本)---- ELMO->GPT->BERT
  • Windows 下纯手工打造 QT 开发环境
  • 总结拓展十四:批次管理(2)
  • SAP SD学习笔记- 豆知识 - SAP中的英文 - SD中英文,日语,中文
  • [Python学习日记-47] Python 中的系统调用模块—— os 与 sys