目录

一、日期计算器的功能

二、获取每个月的天数

三、Date类中的默认成员函数

1、构造函数

2、析构函数

3、拷贝构造

4、赋值运算符重载

5、取地址操作符重载和const取地址操作符重载

四、运算符重载

1、+=、+、-=、-

2、==、!=、>、>=、<、<=

3、前置++和--、后置++和--

4、<<流插入、>>流提取运算符

五、日期类代码


一、日期计算器的功能

实现日期类的==、!=、+=、+、-=、-、>=、>、<=、<、前置++和--、后置++和--。

二、获取每个月的天数

int GetMonthDay(int year, int month)
{//静态数组,每次调用不用频繁在栈区创建数组static int monthArr[12] = { 31,28,31,30,31,30,31,31,30,31,30,31 };//判断是否闰年int day = monthArr[month - 1];if (month == 2 && ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0)){day = 29;}return day;
}

1、因为GetMonthDay这个函数需要在日期类中被频繁调用,所以将 monthArr存放至静态区,减少数组频繁开辟、销毁空间的开销。

三、Date类中的默认成员函数

1、构造函数

Date(int year = 1, int month = 1, int day = 1)
{if (year >= 1 && month >= 1 && day >= 1 && day <= GetMonthDay(year, month)){_year = year;_month = month;_day = day;//cout << "构造成功" << endl;}else{cout << "日期不合法" << endl;}
}

日期类的构造函数需要对日期的的合法性进行判断。

2、析构函数

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

日期类因为没有申请资源(动态开辟空间、文件的打开等),所以无需写析构函数,系统默认生成的就可以。

3、拷贝构造

Date(const Date& d)//可不写
{_year = d._year;_month = d._month;_day = d._day;//cout << "拷贝构造成功" << endl;
}

系统默认生成的拷贝构造函数会对内置类型完成浅拷贝,所以内置类型也可以不用写,用系统默认生成的就可以。

后续处理有资源的对象时,需要深拷贝。

4、赋值运算符重载

Date& operator=(const Date& d)
{_year = d._year;_month = d._month;_day = d._day;//cout << "赋值成功" << endl;return *this;
}

也可不写,使用系统默认生成的即可。

拷贝构造和赋值运算符重载的区别在于拷贝构造用于对象构造时使用,而赋值运算符重载用于已存在对象赋值时使用。

后续处理有资源的对象时,需要先把旧空间释放,再开一块同样大小的空间,进行数据拷贝。

5、取地址操作符重载和const取地址操作符重载

Date* operator&()
{return this;//return nullptr;
}
const Date* operator&()const
{return this;//return nullptr;
}

不用自己写,除非想让别人通过取地址操作符获取到特定值(自己在重载函数内部写)或屏蔽类地址。

四、运算符重载

1、+=、+、-=、-

Date& operator+=(int day){if (day < 0)*this -= -day;else{_day += day;while (_day > GetMonthDay(_year, _month)){_day -= GetMonthDay(_year, _month);++_month;if (_month > 12){_month = 1;++_year;}}}return *this;}Date operator+(int day)const{Date tmp(*this);return tmp += day;}Date& operator-=(int day){if (day < 0)*this += -day;else{_day -= day;while (_day <= 0){--_month;if (_month <= 0){_month = 12;--_year;}_day += GetMonthDay(_year, _month);}}return *this;}Date operator-(int day)const{Date tmp(*this);return tmp -= day;}int operator-(const Date& d)const{Date max = *this, min = d;int flag = 1;//用于表示符号位if (*this < d){max = d;min = *this;flag = -1;}int count = 0;//用于计数while (max > min){++min;++count;}return count * flag;}

1、注意这几个运算符要防止外部传入的day是负数。例如+=传入的参数如果是负数,则去调用-=函数。

2、注意传值返回和传引用返回,当return对象出了作用域还存在时,可以用传引用返回,减少一次拷贝构造。

3、实现完+=、-=后,+、-运算符可复用逻辑。

4、对于*this不变的成员函数,函数后记得加上const。

2、==、!=、>、>=、<、<=

bool operator==(const Date& d)const{if (_year == d._year && _month == d._month && _day == d._day){return true;}return false;}bool operator>(const Date& d)const{if (_year > d._year)return true;if (_year == d._year && _month > d._month)return true;if (_year == d._year && _month == d._month && _day > d._day)return true;return false;}bool operator>=(const Date& d)const{return *this > d || *this == d;}bool operator!=(const Date& d)const{return !(*this == d);}bool operator<(const Date& d)const{return !(*this >= d);}bool operator<=(const Date& d)const{return !(*this > d);}

1、注意右操组数一定要加上&,减少一次传参时的拷贝构造;再加上const,防止被引用的对象被改变。

2、写完==和>函数,其他运算符都可以复用逻辑。

3、前置++和--、后置++和--

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

1、因为++和--是单操作数的运算符,在重载时,无法区分是前置的重载还是后置的重载,所以C++规定:前置重载与普通运算符重载一致,后置重载需要在参数列表中加入一个无用的参数。这个参数必须是int类型(用别的类型编译器报错)。

2、前置++--可以使用传引用返回,但后置++--因为返回值暂时不改变,所以只能传值返回。这也是使用前置++--性能优于后置++--的原因。

4、<<流插入、>>流提取运算符

//类内声明为友元函数
friend ostream& operator<<(ostream& out, const Date& d);
friend istream& operator>>(istream& in, Date& d);
//需要在类外定义
inline ostream& operator<<(ostream& out, const Date& d)
{out << d._year << " " << d._month << " " << d._day << endl;return out;
}
inline istream& operator>>(istream& in, Date& d)
{in >> d._year >>d._month >> d._day;return in;
}

需要在类外定义,在类内声明为友元函数。

五、日期类代码

class Date
{
public:int GetMonthDay(int year, int month){//静态数组,每次调用不用频繁在栈区创建数组static int monthArr[12] = { 31,28,31,30,31,30,31,31,30,31,30,31 };//判断是否闰年int day = monthArr[month - 1];if (month == 2 && ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0)){day = 29;}return day;}//构造函数Date(int year=1 , int month = 1, int day = 1){if (year >= 1 && month >= 1 && day >= 1 && day <= GetMonthDay(year, month)){_year = year;_month = month;_day = day;//cout << "构造成功" << endl;}else{cout << "日期不合法" << endl;}}//析构函数~Date(){cout << "析构成功" << endl;;}//拷贝构造Date(const Date& d){_year = d._year;_month = d._month;_day = d._day;cout << "拷贝构造成功" << endl;}//赋值运算符重载Date& operator=(const Date& d){_year = d._year;_month = d._month;_day = d._day;cout << "赋值成功" << endl;return *this;}//运算符重载Date& operator+=(int day){if (day < 0)*this -= -day;else{_day += day;while (_day > GetMonthDay(_year, _month)){_day -= GetMonthDay(_year, _month);++_month;if (_month > 12){_month = 1;++_year;}}}return *this;}Date operator+(int day)const{Date tmp(*this);return tmp += day;}Date& operator-=(int day){if (day < 0)*this += -day;else{_day -= day;while (_day <= 0){--_month;if (_month <= 0){_month = 12;--_year;}_day += GetMonthDay(_year, _month);}}return *this;}Date operator-(int day)const{Date tmp(*this);return tmp -= day;}int operator-(const Date& d)const{Date max = *this, min = d;int flag = 1;//用于表示符号位if (*this < d){max = d;min = *this;flag = -1;}int count = 0;//用于计数while (max > min){++min;++count;}return count * flag;}bool operator==(const Date& d)const{if (_year == d._year && _month == d._month && _day == d._day){return true;}return false;}bool operator>(const Date& d)const{if (_year > d._year)return true;if (_year == d._year && _month > d._month)return true;if (_year == d._year && _month == d._month && _day > d._day)return true;return false;}bool operator>=(const Date& d)const{return *this > d || *this == d;}bool operator!=(const Date& d)const{return !(*this == d);}bool operator<(const Date& d)const{return !(*this >= d);}bool operator<=(const Date& d)const{return !(*this > d);}Date& operator++(){++_day;if (_day > GetMonthDay(_year, _month)){_day = 1;++_month;if (_month > 12){_month = 1;++_year;}}return *this;}Date operator++(int){Date tmp(*this);++* this;return tmp;}Date& operator--(){--_day;if (_day <= 0){--_month;if (_month == 0){_month = 12;--_year;}_day += GetMonthDay(_year, _month);}return *this;}Date operator--(int){Date tmp(*this);--* this;return tmp;}friend ostream& operator<<(ostream& out, const Date& d);friend istream& operator>>(istream& in, Date& d);//取地址重载和const取地址重载Date* operator&(){return this;}const Date* operator&()const{return this;}
private:int _year;int _month;int _day;
};
inline ostream& operator<<(ostream& out, const Date& d)
{out << d._year << " " << d._month << " " << d._day << endl;return out;
}
inline istream& operator>>(istream& in, Date& d)
{in >> d._year >>d._month >> d._day;return in;
}

因为函数的声明和定义全部放在类中,会被编译器当成内联函数处理。所以可以根据自身需要,将部分调用不频繁、稍长的函数的声明写在类中,而定义写在类外。

CSDN话题挑战赛第2期
参赛话题:学习笔记

【C++】实现一个日期计算器相关推荐

  1. 用C语言写一个日期计算器

    首先,我们应知道日期计算器包括哪些功能 1.明天的日期 2 .n天后的日期 3.两个日期之间的天数 我们先从第一个功能开始,首先创建一个日期的结构体,包括:年.月.日. struct date {in ...

  2. 手把手教你写一个日期计算器(C++)

    放弃信念,无异死亡. 文章目录

  3. 【C++】---日期计算器

    [C++] 日期计算器 注:可能有时你也会被这样的问题所烦恼,你想要知道自己活了多少天的话,乍一想这该怎么计算呢,捋一捋,要计算平年闰年,每个月多少天等等等苦恼着,下面我们通过C++来实现一个日期计算 ...

  4. 【Java】淘宝网店(计算某日是一年当中的第几天/日期计算器...)

    题目描述: 解题思路: 这是一个变相的日期计算器.只不过2.3.5.7.11月算1天,其他7个月算2天. 既然是一个变相的日期计算器,那就写一个日期计算器,然后加以修改即可.那么,日期计算器怎么写呢? ...

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

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

  6. c语言日期加减天数,日期计算器

    一  完成的功能 注意:此日历只能用于计算1982年10月15日以及以后的日期 1.日期+/-天数=返回日期(处理:1.如果+/-一个负的天数的情况 2.如果加之后,或减之后的日期非法) 2.两个日期 ...

  7. 日期计算器-java(含界面)

    日期计算器 一. 系统描述 完成日期计算器的编写. 系统功能: 1.输入日期,完成星期几的计算,并输出星期值 2.输入日期加天数,完成日期加/减天数的计算,并输出新日期 3.输入日期以及一个年月日,完 ...

  8. c++Date(日期)类方法实现日期计算器

    1.日期类应该具有什么功能 计算两个日期间的间隔时间 给定一个日期,计算出减少x天数后的日期 给定一个日期,减少x天数后的日期 2.日期类的具体实现 需要注意的是: 为了提高的安全性,我只将要实现对象 ...

  9. 【C++】日期计算器

    文章目录 一.前言 二.日期类的实现 1.Date类中默认成员函数的使用 1.构造函数 2.析构函数 3.拷贝构造函数 4.赋值运算符重载 5.const成员函数 6.取地址操作符重载和const取地 ...

  10. python实现简单的日期计算器

    因为一直要计算当天日期往前推N天的日期,所以想做一个简单的日期计算器实现这个功能. 另外把计算两个日期之间的天数也一起给加进去了,就更完整. 这个日期计算器的界面很简陋,功能也非常简单. 暂时没有做通 ...

最新文章

  1. CSP 201812-1 小明上学 Python实现+详解
  2. 未转变者空投指令服务器,未转变者空投指令 | 手游网游页游攻略大全
  3. linux安装java tar.gz_Linux(CentOS)安装java运行环境JDK1.8(.tar.gz)
  4. 单机编程c语言,完美的8051单机C语言编程模板.doc
  5. 帮助方老师使用固态硬盘安装win10,赚了150软妹币(但是他赖账了!)
  6. 15-CoreData删除所有数据之NSBatchDeleteRequest
  7. 2018年的AI/ML惊喜及预测19年的走势(一)
  8. mysql 同一帐号多次登录_freeradius2.1.3 防止用户帐号重复登录
  9. Centos7 安装netcat(NC瑞士军刀)
  10. mysql数据绑定listview_将数据库数据用代码绑定到Listview
  11. 卡尔滤波算法 java_卡尔曼滤波算法及其代码
  12. 天正多条线段长度lisp下载_如何快速计算cad中多条多段线的总长
  13. 电脑没有声音,显示“未插入耳机或扬声器”,检测不到Realtek高清晰音频管理器...
  14. 关键时刻不纠结的秘密:极简选择
  15. JavaScript 数组拼接打印_JavaScript 中的“黑话”
  16. AutoCAD安装失败怎样卸载重新安装AutoCAD,解决AutoCAD安装失败的方法总结【转载】
  17. java 佛祖保佑_佛祖保佑 永无bug 注释模板设置详解(仅供娱乐)
  18. 计算机论文的字体要求,关于计算机硕士论文格式要求 论文字体格式
  19. mbk文件导入到oracle,Oracle基于物化视图的远程数据复制
  20. 从一个故事说起,谈谈企业应用架构的演变史

热门文章

  1. OpenCV-Python 直方图-4:直方图反投影 | 二十九
  2. macbook卡在进度条开不了机_mac开机卡在进度条的问题
  3. 毕业4年年薪200万是怎样的一种体验?
  4. Excel插入图表失真(数据格式原因)修复笔记
  5. 如何使用Java以编程方式在 Excel 中创建图表
  6. Xdebug、Zend bugger与Zend Optmizer不兼容问题
  7. CMake安装mysql时报错:remove CMakeCache.txt and rerun cmake
  8. html图片水印的代码,简单实用的给图片加水印源代码
  9. 敲开脑洞(一),如何摆脱痛苦,记自己的短期修行
  10. 电脑系统故障维修,系统C盘满了怎么办?教你c盘清理方法