目录

日期类计算器的模拟实现::

1.获取某年某月的天数

2.构造函数

3.拷贝构造函数

4.赋值运算符重载

5.析构函数

6.日期+=天数

7.日期+天数

8.日期-天数

9.日期-=天数

10.前置++的运算符重载

11.后置++的运算符重载

12.前置--的运算符重载

13.后置--的运算符重载

14.>的运算符重载

15.<的运算符重载

16.==的运算符重载

17.>=的运算符重载

18.<=的运算符重载

19.!=的运算符重载

20.<<的运算符重载

21.>>的运算符重载

22.日期-日期


日期类计算器的模拟实现::

1.获取某年某月的天数

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

2.构造函数

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

3.拷贝构造函数

// 拷贝构造函数 形参加const 防止写反了 问题就可以检查出来了
Date(const Date& d)
{_year = d._year;_month = d._month;_day = d._day;
}

4.赋值运算符重载

//d1 = d2
//注:1.要注意两个参数的顺序 2.这里面参数不加引用不会导致无穷递归 但为了避免拷贝构造最好加引用
Date& operator=(const Date& d)
{//为了支持链式赋值 if是为了避免自己给自己赋值 d1 = d1if (this != &d){_year = d._year;_month = d._month;_day = d._day;}return *this;
}

5.析构函数

~Date()//可不写
{;
}

日期类因为没有申请资源,所以无需写析构函数,编译器默认生成的析构函数就可以。

6.日期+=天数

//d1 += 100
//天满了进月 月满了进年
Date& operator+=(int day)
{//避免 d1 += -1000的情形if (day < 0){return *this -= -day;}_day += day;while (_day > GetMonthDay(_year, _month)){_day -= GetMonthDay(_year, _month);_month++;if (_month == 13){++_year;_month = 1;}}return *this;
}

7.日期+天数

//d1 + 100
Date operator+(int day) const
{Date ret(*this);ret += day;//ret.operator+=(day)return ret;
}

8.日期-天数

//d1 - 100
Date operator-(int day) const
{Date ret(*this);ret -= day;return ret;
}

9.日期-=天数

//d1 -= 100
Date& operator-=(int day)
{//避免 d1 -= -1000if (day < 0){return *this += -day;}_day -= day;while (_day <= 0){--_month;if (_month == 0){--_year;_month = 12;}_day += GetMonthDay(_year, _month);}return *this;
}

10.前置++的运算符重载

//前置++
Date& operator++()
{//会调用 operator+=(int day)*this += 1;return *this;
}

11.后置++的运算符重载

//后置++ —多一个int参数主要是为了和前置++进行区分 构成函数重载
Date operator++(int)
{Date tmp(*this);*this += 1;return tmp;
}

12.前置--的运算符重载

//前置--
Date& operator--()
{//复用运算符重载-=*this -= 1;return *this;
}

13.后置--的运算符重载

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

14.>的运算符重载

//d1 > d2
bool operator>(const Date& d) const
{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;
}

15.<的运算符重载

//d1 < d2
bool operator<(const Date& d) const
{return !(*this >= d);
}

16.==的运算符重载

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

17.>=的运算符重载

//d1 >= d2
bool operator>=(const Date& d) const
{return *this > d || *this == d;
}

18.<=的运算符重载

//d1 <= d2
bool operator<=(const Date& d) const
{return !(*this > d);
}

19.!=的运算符重载

//d1 != d2
bool operator!=(const Date& d) const
{return !(*this == d);
}

20.<<的运算符重载

//内联函数和静态成员一样 调用处展开 不进符号表
inline ostream& operator<<(ostream& out, const Date& d)
{out << d._year << "年" << d._month << "月" << d._day << "日" << endl;return out;
}

21.>>的运算符重载

//cin >> d1 编译器转化成operator(cin,d1) 形参中相比<< 去掉了const
inline istream& operator>>(istream& in, Date& d)
{in >> d._year >> d._month >> d._day;return in;
}

22.日期-日期

//日期-日期
int operator-(const Date& d) const
{Date max = *this;Date min = d;int flag = 1;if (*this < d)//总结:凡是内部不改变成员变量 也就是不改变*this数据的 这些成员函数都应该加const//if (d > *this){max = d;min = *this;flag = -1;}int n = 0;while (min != max){++n;//复用++ ++到和d1日期相等 就是相差多少天++min;}return n * flag;
}

Date.h

#pragma once
#include <iostream>
using namespace std;
class Date
{//友元声明(类的任意位置)声明友元时可以不用加inlinefriend ostream& operator<<(ostream& out, const Date& d);friend istream& operator>>(istream& in, Date& d);
public:int 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(int year = 1, int month = 1, int day = 1){_year = year;_month = month;_day = day;//检查日期是否合法if (!((year >= 1)&& (month >= 1 && month <= 12)&& (day >= 1 && day <= GetMonthDay(year, month)))){cout << "非法日期" << endl;}}// 拷贝构造函数 形参加const 防止写反了 问题就可以检查出来了Date(const Date& d){_year = d._year;_month = d._month;_day = d._day;}//d1 == d2bool operator==(const Date& d) const;//d1 > d2bool operator>(const Date& d) const;//d1 >= d2bool operator>=(const Date& d) const;//d1 <= d2bool operator<=(const Date& d) const;//d1 < d2bool operator<(const Date& d) const;//d1 != d2bool operator!=(const Date& d) const;//d1 += 100Date& operator+=(int day);//d1 + 100Date operator+(int day) const;//d1 = d2 注:1.要注意两个参数的顺序 2.这里面参数不加引用不会导致无穷递归 但为了避免拷贝构造最好加引用Date& operator=(const Date& d){//为了支持链式赋值 if是为了避免自己给自己赋值 d1 = d1if (this != &d){_year = d._year;_month = d._month;_day = d._day;}return *this;}//d1 -= 100Date& operator-=(int day);//d1 - 100Date operator-(int day) const;//++的操作数只有一个 不传参//前置++Date& operator++();//编译器为了区分前置++和后置++ 规定在后置的函数上加了一个参数//后置++Date operator++(int);//允许成员函数加const 此时this指针的类型为:const Date* const thisvoid Print() const{cout << _year << "/" << _month << "/" << _day << endl;}//前置--Date& operator--();//后置--Date operator--(int);//日期-日期int operator-(const Date& d) const;//流插入//d1 << cout编译器会转化成d1.operator<<(cout) this指针抢了左操作数d1的位置//<<和>>的重载一般不写成成员函数 因为this默认抢了第一个参数的位置 Date类对象就是左操作数 不符合使用习惯和可读性/*void operator<<(ostream& out){out << _year << "年" << _month << "月" << _day << "日" << endl;}*///取地址重载Date* operator&(){return this;}//const成员取地址重载const Date* operator&() const{return this;}//取地址重载和const成员取地址重载不实现 编译器会默认生成
private:int _year;int _month;int _day;
};
//结论:对于自定义类型,尽量用前置,减少拷贝,提高效率
//全局函数调用:cout << d1转化成operator<<(cout,d1)
//全局函数的定义和全局变量不能放在.h文件中 因为函数的定义在Date.cpp和test.cpp都会展开 函数地址进入符号表 链接器链接两个.cpp文件时相同的函数地址会报错
//解决方法:1.改成静态 2.声明和定义分离
//static修饰函数只在当前文件可见 不会进入符号表
//static void operator<<(ostream& out,const Date& d)
//{
//  out << d._year << "年" << d._month << "月" << d._day << "日" << endl;
//}
//ostream& operator<<(ostream& out, const Date& d);
//内联函数和静态成员一样 调用处展开 不进符号表
inline ostream& operator<<(ostream& out, const Date& d)
{out << d._year << "年" << d._month << "月" << d._day << "日" << endl;return out;
}
//cin >> d1 编译器转化成operator(cin,d1) 形参中相比<< 去掉了const
inline istream& operator>>(istream& in, Date& d)
{in >> d._year >> d._month >> d._day;return in;
}

Date.cpp

#include"Date.h"
//d1 == d2
bool Date::operator==(const Date& d) const
{   return _year == d._year&& _month == d._month&& _day == d._day;
}
//d1 > d2
bool Date::operator>(const Date& d) const
{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;
}
//d1 >= d2
bool Date::operator>=(const Date& d) const
{return *this > d || *this == d;
}
//d1 <= d2
bool Date::operator<=(const Date& d) const
{return !(*this > d);
}
//d1 < d2
bool Date::operator<(const Date& d) const
{return !(*this >= d);
}
//d1 != d2
bool Date::operator!=(const Date& d) const
{return !(*this == d);
}
//d1 += 100
//天满了进月 月满了进年
Date& Date::operator+=(int day)
{//避免 d1 += -1000的情形if (day < 0){return *this -= -day;}_day += day;while (_day > GetMonthDay(_year, _month)){_day -= GetMonthDay(_year, _month);_month++;if (_month == 13){++_year;_month = 1;}}return *this;
}
//d1 + 100
Date Date::operator+(int day) const
{Date ret(*this);ret += day;//ret.operator+=(day)return ret;
}
//d1 -= 100
Date& Date::operator-=(int day)
{//避免 d1 -= -1000if (day < 0){return *this += -day;}_day -= day;while (_day <= 0){--_month;if (_month == 0){--_year;_month = 12;}_day += GetMonthDay(_year, _month);}return *this;
}
//d1 - 100
Date Date::operator-(int day) const
{Date ret(*this);ret -= day;return ret;
}
//前置++
Date& Date::operator++()
{//会调用 operator+=(int day)*this += 1;return *this;
}
//后置++ —多一个int参数主要是为了和前置++进行区分 构成函数重载
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) const
{Date max = *this;Date min = d;int flag = 1;if (*this < d)//总结:凡是内部不改变成员变量 也就是不改变*this数据的 这些成员函数都应该加const//if (d > *this){max = d;min = *this;flag = -1;}int n = 0;while (min != max){++n;//复用++ ++到和d1日期相等 就是相差多少天++min;}return n * flag;
}
//为了支持链式流插入 cout<< d1 <<d2 返回cout类对象
//ostream& operator<<(ostream& out,const Date& d)
//{
//  out << d._year << "年" << d._month << "月" << d._day << "日" << endl;
//  return out;
//}

最后和大家分享一段最近在emo中始终鼓舞自己的一段话,希望大家能在艰苦漫长的学习岁月中,不忘初心找寻真正的自己,把所有的美好装进独属于自己的记忆里,调整心态,继续砥砺前行!

     无人问津也好,技不如人也罢,你都要试着安静下来,去做自己该做的事情,而不是让烦恼和焦虑毁掉你本就不多的热情和定力。心可以碎,手却不能停,该干什么干什么,在崩溃中继续努力前行。

                                                                                                                                        ——余华

出自 "includeevey" 中的一段话

彼此分享给大家的目的一样,希望所有正在学技术被技术所困的友友们,在艰难的时候看到这句话能披荆斩棘,勇往直前!

我的博客即将同步至腾讯云开发者社区,邀请大家一同入驻:https://cloud.tencent.com/developer/support-plan?invite_code=3ngxw7pcde80c

[C++]日期类计算器的模拟实现相关推荐

  1. 使用日期类和计时器模拟商品促销

    使用日期类和计时器模拟商品促销 题目如下: 计算商品促销日期: 商场需要在一件商品过期的前两周的周五开始做促销 计算商品过期日期往前推两周的周五的日期 拟定过期日期为: String dieTime ...

  2. 日期类计算器的实现以及运算符重载

    前言 众所周知,运算符重载是C++类与对象中非常重要的一环.今天我们介绍内容就是通过模拟实现日期类的计算机,来详细的了解运算符是如何重载的. 注:代码给在文末. 目录 前言 创建一个日期类 1.重载 ...

  3. 【C++】日期类+日期万年历+日期计算器

    对于日期类,我们主要实现一下日期类的基本函数,构造,拷贝构造,运算符的重载,析构.当然这里运算符的重载需要实现的还是挺多的,如:=.<.>.<=.>=.等 #include & ...

  4. java 类数组_Java常用类-字符串、日期类、算法及数组工具类等

    大家好,乐字节的小乐又和大家见面了.这次要给大家讲述的是Java常用类. 主要有以下知识点: Ø 字符串相关类(String .StringBuffer.StringBuilder) Ø 算法及数组工 ...

  5. 冰冰学习笔记:string类的简单模拟

    欢迎各位大佬光临本文章!!! 还请各位大佬提出宝贵的意见,如发现文章错误请联系冰冰,冰冰一定会虚心接受,及时改正. 本系列文章为冰冰学习编程的学习笔记,如果对您也有帮助,还请各位大佬.帅哥.美女点点支 ...

  6. date日期相减 java_03时间日期类

    Java8 在 java.time 包中增加了时间日期相关的API,弥补了 Java8 以前对日期.时间处理的不足. 在介绍Java8新的时间日期API前,先看看 java8 以前我们操作日期时间常用 ...

  7. string日期格式化_java面向对象---日期类

    10.日期类 (1)Date Date代表了一个特定的时间,精确到毫秒 方法名 说明 Public Date() 分配一个Date对象并将其初始化,以便它代表它被分配的时间,精确到毫秒. Public ...

  8. 日期类对象与整数之间的加法运算

    日期类对象与整数之间的加法运算 采用友元函数形式, 定义两个友元函数

  9. 蓝桥杯日期计算java_日期类的使用(java)-蓝桥杯

    蓝桥杯日期问题常考,java提供了日期类很方便: //日历类 Calendar c = Calendar.getInstance(); // 获取实例化对象 Date date =c.getTime( ...

最新文章

  1. 【转载】[Windows 8]Hello Windows 8 - Windows 8 Developer Preview搶先預覽
  2. ASP.NET MVC 实现与SQLSERVER的依赖缓存
  3. dll可以在linux下使用吗_Linux下使用rm删除文件,并排除指定文件
  4. F - 上升子序列-超详细注释版
  5. java 内置jetty_内置jetty
  6. SharePoint 2007 权限代码分享
  7. 初学objective c语言4 dealloc
  8. 嵌入式开发Verilog教程(二)——Verilog HDL设计方法概述
  9. python: not found 问题的解决
  10. python推荐系统-Python黑马头条推荐系统项目
  11. Linux 配置网络桥接模式
  12. 【粤教版必修二《信息系统与社会》】知识总结与题目分析
  13. 计算机专业技能考核方案,计算机专业技能课教学目标考核方案.doc
  14. Windows11网速慢解决方案
  15. 利用Ajax爬取今日头条头像,街拍图片。关于崔庆才python爬虫爬取今日头条街拍内容遇到的问题的解决办法。
  16. 浏览器 User-Agent 大全
  17. hdiraw多点分析,使用getevnt进行上报
  18. 基于alios系统门禁uart串口应用编写
  19. AD637原理图PCB电路设计,使用经验和建议——【电路模块经验10】
  20. UVALive 7279 Sheldon Numbers

热门文章

  1. Python开发 之 Jieba分词示例
  2. 游戏平台运营核心是什么?
  3. 【Java提升】设计模式入门教程 工厂模式
  4. mysql 5.7 安装(收藏防迷路)
  5. 安卓 呼吸灯效果--代码实现
  6. ArcEngine9.3编辑工具条的实现
  7. 敏捷教练----Scrum-概述
  8. windows启动redis失败解决方法
  9. 笔记本外接屏幕,怎么独立显示,怎么变换鼠标移动方向
  10. 算法一:求100以内的质数(Java和C#实现)