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

类和对象——日期类实现

目录

前言:

一、构造

二、运算符重载

三、++与--

四、实现+与+= 操作

        4.1 实现+= 操作

        4..2 实现+操作

        4.3 效率分析

五、实现 - 与-= 操作符

六、日期减日期

七、流插入和流提取

八、完整代码

        Date.h

        Date.cpp


前言:

        我们之前已经学习过了类和对象,我们今天来实践应用一下,来增强感悟!

一、构造

        我们要实现日期类第一步便是对其经行构造,使其成为一个类,以下便是创建的类:

class Date
{public:Date(int year = 0, int month = 0, int day = 0);private:int _year;int _month;int _day;  
};

        日期类只需创建出年月日即可,以下是实例化,我们这次是声明和实现分开。

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

        这便是日期类的创建,看着这代码,你不由陷入了深思:如果我随便输入月和日该如何处理,比如:2月31日,这不就是坑吗?咱们要防患于未然,那我们能不能写一个函数,来获取月份的天数,然后我们在进行判断,符合我们就采取,不符合就进行提示。

        说干就干,那我们该如何获取月份天数呢?我们可采取这个方法:先定义一个数组,把每个月天数放进去,二月按照28天先放,然后进行闰年判断,是闰年2月返回29,否则就按照数组中的值进行返回。

int Date::GetMonthDay(int year,int month)
{assert(month > 0 && month < 13);int _month[13] = { 0,31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };  //此处定义为13是为了方便,不用进行处理if ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0)				//闰年判断{if (month == 2){return 29;}}return _month[month];
}

        那,我们日期类的创建可以做出以下的改进:

Date::Date(int year,int month, int day)
{if (month > 0 && month < 13&& (day > 0 && day <= GetMonthDay(year, month))){_year = year;_month = month;_day = day;}else{cout << "非法坐标" << endl;}
}

        这下你看的顺眼多了,但还是觉得缺点什么?还没实现打印函数,那就顺手实现吧!

void Date::Print()
{cout << _year << "/" << _month << "/" << _day;
}

        好了,接下来实现下一个函数吧!

二、运算符重载

        运算符重载在之前文章都有介绍,这里就简单说一下:我们只需任意两个即可,其余我们都可以进行复用。实现如下:

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;}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);
}bool Date::operator!=(const Date& d)
{return !(*this == d);
}

        我们这里实现的是== 与 < 这两个运算符,大家可自行选择,难度不大。

三、++与--

        关于这两个运算符都有前置与后置两个用法,区别为:前置后面无参数,后置后面需加int,但这个int不参与运算。实现如下:

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 *this;
}

四、实现+与+= 操作

        4.1 实现+= 操作

                大家想一想:对于+= 这个操作,大家如何实现。我们想要实现可不可以这样搞:先啥都不管先把日期加上再说,那要是超出日期怎么办?简单,把月++然后把日减去月++前的月日期,如果还不符合,循环即可。如果月份大于十二,那就月份置一,年++。这个思路应该没啥问题。那我们考虑一点:如何接受返回值?在使用该操作符时我们明白:会改变该变量本身。所以,我们直接将其本身返回即可。实现如下:

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

        4..2 实现+操作

                对于+这个操作符我们是否能够可以参考以上的思路?当然可以,不过我们思考一件事:我们使用+这个操作符,它本身是不是不改变?改变的原因是不是最后的赋值?大家想清楚这个问题,想清楚了这个问题我们来想如何解决这个问题,那既然是不改变,我们能不能创建一个临时变量叫它来帮助我们,就像实现后置++、--操作符。我们的实现如下:

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._month = 1;tmp._year++;}}return tmp;
}

        4.3 效率分析

                对于以上我们是不是可以用运算符重载来实现,那我们不禁思考:用谁来实现谁比较好?是+来复用+=,还是+=来复用+?这关乎与效率问题,大家选好心目中的队伍,我们来分析吧!

五、实现 - 与-= 操作符

        有了 + 与 += 操作符的实现,我们对于该操作符实现可以说是很简单了,所以,大家参考以下代码即可:

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

        咱们现在以是实现完毕,这时我们想一个问题:如果有人故意捣乱传的值为负数怎么办?很简单,+= 移交给 -= ,反之,亦然。咱们以-= 为例,只需加入以下代码即可:

Date& Date::operator-=(int day)
{if (day < 0)					//进行判断,符合条件进行复用即可{*this += day;return *this;}_day -= day;while (_day <= 0){_month--;if (_month == 0){_year--;_month = 12;}_day += GetMonthDay(_year, _month);}return *this;
}

六、日期减日期

        我们明白,日期加日期没啥用,但是日期减去一个日期就有用了不少,我们不禁想明白这个该如何实现:直接减去,我们啥都要考虑,闰年不润年先放一边,主要是你月份相减所得的天数是哪几个月的?这是个很复杂的问题。现提供如下思路:我们先把大的选出来,如果小的减去大的最后乘以-1即可。然后,我们一直使小的++最后等于大的即可得出。代码如下:

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

七、流插入和流提取

        对于流插入和提取我们要实现的目的是:Date为自定义类型,对于自定义类型我们只能自己实现,不能依靠别人。我们要实现插入与提取最好放在全局,不放入类中,因为this指针会替代第一个问题,放入类中会反过来使用,不符合习惯。实现如下:

ostream& operator<<(ostream& out, const Date& d)
{out << d._year << "/" << d._month << "/" << d._day << endl;return out;
}istream& operator>>(istream& in, Date& d)
{in >> d._year >> d._month >> d._day;return in;
} 

八、完整代码

        Date.h

#pragma once
#include <iostream>
#include <assert.h>
using namespace std;class Date 
{friend ostream& operator<<(ostream& out, const Date& d);friend istream& operator>>(istream& in, Date& d);public:Date(int year = 0, int month = 0, int day = 0);void Print();int GetMonthDay(int year, int month);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);Date& operator+=(int day);Date operator+(int day);Date& operator-=(int day);// d1 - 100Date operator-(int day);// d1 - d2;int operator-(const Date& d);// ++d1Date& operator++();// d1++Date operator++(int);Date& operator--();Date operator--(int);private:int _year;int _month;int _day;
};

        Date.cpp

#include"Data.h"int Date::GetMonthDay(int year,int month)
{assert(month > 0 && month < 13);int _month[13] = { 0,31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };  //此处定义为13是为了方便,不用进行处理if ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0)				//闰年判断{if (month == 2){return 29;}}return _month[month];
}Date::Date(int year,int month, int day)
{if (month > 0 && month < 13&& (day > 0 && day <= GetMonthDay(year, month))){_year = year;_month = month;_day = day;}else{cout << "非法坐标" << endl;}
}Date& Date::operator+=(int day)
{if (day < 0){*this -= day;return *this;}_day += day;while (_day > GetMonthDay(_year, _month)){_day -= GetMonthDay(_year,_month);_month++;if (_month > 12){_year++;_month = 1;}}return *this;
}Date Date::operator+(int day)
{Date tmp(*this);tmp += day;return tmp;
}//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._month = 1;
//			tmp._year++;
//		}
//	}
//	return tmp;
//}
//
//Date& Date::operator+=(int day)
//{
//	*this = *this + day;
//	return *this;
//}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 *this;
}Date& Date::operator-=(int day)
{if (day < 0)					//进行判断,符合条件进行复用即可{*this += day;return *this;}_day -= day;while (_day <= 0){_month--;if (_month == 0){_year--;_month = 12;}_day += GetMonthDay(_year, _month);}return *this;
}Date Date::operator-(int day)
{Date tmp(*this);tmp -= day;return tmp;
}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 n = 0;while (max != min){min++;n++;}return n * flag;
}void Date::Print()
{cout << _year << "/" << _month << "/" << _day << endl;
}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;}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);
}bool Date::operator!=(const Date& d)
{return !(*this == d);
}ostream& operator<<(ostream& out, const Date& d)
{out << d._year << "/" << d._month << "/" << d._day << endl;return out;
}istream& operator>>(istream& in, Date& d)
{in >> d._year >> d._month >> d._day;return in;
} 

完!

相关文章:

  • 北京网站建设多少钱?
  • 辽宁网页制作哪家好_网站建设
  • 高端品牌网站建设_汉中网站制作
  • 从一到无穷大 #32 TimeCloth,云上的快速 Point-in-Time Recovery
  • [Meachines] [Easy] shocker CGI-BIN Shell Shock + Perl权限提升
  • python rsa如何安装
  • live2d C++ sdk 分析
  • 高仲富:49岁搞AI,白天种菜卖菜,晚上学数学搞程序
  • 都2024年了,SQL语句还需要手写吗?
  • 【限免】通信信号与干扰信号【附MATLAB代码】
  • python 图片爬虫记录
  • 实时数仓分层架构详解
  • 【Python】常用的pdf提取库介绍对比
  • 电子电气架构 --- SOVD在域控制器的应用
  • 缓存一致性问题
  • tcpdump使用指南
  • 前端的学习-CSS(二)-弹性盒子-flex
  • 在C#中为图片添加数字水印的几种办法
  • Asm.js的简单介绍
  • docker python 配置
  • docker容器内的网络抓包
  • IE报vuex requires a Promise polyfill in this browser问题解决
  • JavaScript 无符号位移运算符 三个大于号 的使用方法
  • JavaScript/HTML5图表开发工具JavaScript Charts v3.19.6发布【附下载】
  • JavaScript-Array类型
  • js
  • leetcode46 Permutation 排列组合
  • Linux快速复制或删除大量小文件
  • Linux中的硬链接与软链接
  • niucms就是以城市为分割单位,在上面 小区/乡村/同城论坛+58+团购
  • React Transition Group -- Transition 组件
  • TiDB 源码阅读系列文章(十)Chunk 和执行框架简介
  • vue数据传递--我有特殊的实现技巧
  • 阿里云Kubernetes容器服务上体验Knative
  • 爱情 北京女病人
  • 解决iview多表头动态更改列元素发生的错误
  • 前嗅ForeSpider采集配置界面介绍
  • 一个SAP顾问在美国的这些年
  • 一起来学SpringBoot | 第十篇:使用Spring Cache集成Redis
  • 运行时添加log4j2的appender
  • 白色的风信子
  • 大数据全解:定义、价值及挑战
  • 回归生活:清理微信公众号
  • ​3ds Max插件CG MAGIC图形板块为您提升线条效率!
  • #{}和${}的区别是什么 -- java面试
  • #etcd#安装时出错
  • (2)Java 简介
  • (3)Dubbo启动时qos-server can not bind localhost22222错误解决
  • (5)STL算法之复制
  • (C语言)输入一个序列,判断是否为奇偶交叉数
  • (Redis使用系列) Springboot 实现Redis 同数据源动态切换db 八
  • (二十六)Java 数据结构
  • (非本人原创)我们工作到底是为了什么?​——HP大中华区总裁孙振耀退休感言(r4笔记第60天)...
  • (分享)一个图片添加水印的小demo的页面,可自定义样式
  • (分享)自己整理的一些简单awk实用语句
  • (附源码)node.js知识分享网站 毕业设计 202038
  • (附源码)springboot课程在线考试系统 毕业设计 655127
  • (附源码)计算机毕业设计SSM疫情下的学生出入管理系统