贵州建设职业技术学院官方网站/墨子学院seo
今天我们实现一个日期类,对前面所学知识进行小的应用总结。
首先我们先写日期类Date最基本的东西,私有成员,构造函数,拷贝构造函数,析构函数(可以不写,编译器默认生成),赋值运算符重载。
私有成员变量:
private:int _year=1900;int _month=1;int _day=1;
构造函数:Date(int year,int month,int day):_year(year),_month(month),_day(day){ }
拷贝构造函数:
Date(const Date& d)
{_year=d._year;_month=d._month;_day=d._day;
}
赋值运算符重载:检测是否自己给自己赋值
// d2 = d3 -> d2.operator=(&d2, d3)
Date& Date::operator=(const Date& d)
{if (this != &d){_year = d._year;_month = d._month;_day = d._day;}return *this;
}
我们还要考虑,我们创建的日期是否合法,比如每个月的天数是否符合月数。那我们就要写一个检查日期合法的函数,检查日期又要获得当年当月的天数,又要再写一个获得月份天数的函数。
获得当年月份天数的函数:
// 获取某年某月的天数//频繁调用,放类中定义作inlineint GetMonthDay(int year, int month){static int days[13] = { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };//使用static,静态区,在程序结束前只用开辟一次空间if (month == 2 &&(year % 400 == 0) || (year % 4 == 0 && year % 100 != 0)){return 29;}else{return days[month];}}
检查日期:
bool CheckDate(){if (_year >= 1 && _month > 0 && _month < 13 && _day>0 && _day <= GetMonthDay(_year, _month)){return true;}else{return false;}}
Date.h头文件
#pragma once
#include<iostream>
#include<assert.h>
using namespace std;
class Date
{
public:// 获取某年某月的天数//频繁调用,放类中定义作inlineint GetMonthDay(int year, int month){static int days[13] = { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };//使用static,静态区,在程序结束前只用开辟一次空间if (month == 2 &&(year % 400 == 0) || (year % 4 == 0 && year % 100 != 0)){return 29;}else{return days[month];}}bool CheckDate(){if (_year >= 1 && _month > 0 && _month < 13 && _day>0 && _day <= GetMonthDay(_year, _month)){return true;}else{return false;}}// 全缺省的构造函数Date(int year , int month , int day):_year(year),_month(month),_day(day){}// 拷贝构造函数// d2(d1)Date(const Date& d){_year = d._year;_month = d._month;_day = d._day;}// 赋值运算符重载// d2 = d3 -> d2.operator=(&d2, d3)Date& operator=(const Date& d);// 日期+=天数Date& operator+=(int day);// 日期+天数Date operator+(int day) const;// 日期-天数Date operator-(int day) const;// 日期-=天数Date& operator-=(int day);// 前置++Date& operator++();// 后置++Date operator++(int);// 后置--Date operator--(int);// 前置--Date& operator--();// >运算符重载bool operator>(const Date& d) const;// ==运算符重载bool operator==(const Date& d) const;// >=运算符重载bool operator >= (const Date& d) const;// <运算符重载bool operator < (const Date& d) const;// <=运算符重载bool operator <= (const Date& d) const;// !=运算符重载bool operator != (const Date& d) const;// 日期-日期 返回天数int operator-(const Date& d) const;private:int _year=1900;int _month=1;int _day=1;
};
下面我们来写Date.cpp文件,定义各运算符重载
任何一个类只需要写一个> ==或< ==重载,剩下比较运算符重载复用即可。
// >运算符重载
bool Date::operator>(const Date& d) const
{if ((_year > d._year)|| (_year == d._year && _month > d._month)|| (_year == d._year && _month == d._month && _day > d._day)){return true;}else{return false;}
}
// ==运算符重载
bool Date::operator==(const Date& d) const
{return _year == d._year&& _month == d._month&& _day == d._day;
}
// >=运算符重载
bool Date::operator>=(const Date& d) const
{return (*this == d) || (*this > d);
}// <运算符重载
bool Date::operator < (const Date& d) const
{return !(*this >= d);
}// <=运算符重载
bool Date::operator <= (const Date& d) const
{return !(*this > d);
}// !=运算符重载
bool Date::operator != (const Date& d) const
{return !(*this == d);
}
实现日期+=天数,日期+天数,日期-=天数,日期-天数。
同样的我们可以,写其中一种另一种进行复用。
这里我们采用写日期+=天数,日期-=天数。
// 日期+=天数 d2+=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 == 13){++_year;_month = 1;}}return *this;
}// 日期+天数 d2+100
Date Date::operator+(int day) const
{Date ret = *this;ret += day;return ret;
}// 日期-天数
Date Date::operator-(int day) const
{Date ret(*this);ret -= day;return ret;
}// 日期-=天数
Date& Date::operator-=(int day)
{if (_day < 0){return *this += -day;}_day -= day;while (_day <= 0){--_month;if (_month == 0){_month = 12;--_year;}_day += GetMonthDay(_year, _month);}return *this;
}
【问题】
1.这里为什么采用写+=和-=,而不是写+和-?
答:+=重载和-=重载中,并没有使用拷贝构造函数,如果使用+和-,则会使用拷贝构造函数。
Date Date::operator+(int day) {Date ret = *this;// ...ret._day += day;while (ret._day > GetMonthDay(ret._year, ret._month)){//...}return ret; }// d1 += 100 Date& Date::operator+=(int day) {*this = *this + day;return *this; }
写+的话,内部创建ret拷贝构造,值返回拷贝构造,2次拷贝构造。+=复用中,需要调用+,2次拷贝构造。
写+=的话,内部没有拷贝构造,复用+=,2个拷贝构造,所以综上还是写+=调用拷贝构造的次数少,效率好。
2.日期+天数和日期-天数,为什么是const成员函数?
答:因为日期+天数和日期-天数都不需要改变原来的日期,即this指针指向的内容,所以我们用const修饰,const Date* const this ,使指向内容不变。
前置++,后置++,前置--,后置--
// 前置++:返回+1之后的结果// 注意:this指向的对象函数结束后不会销毁,故以引用方式返回提高效率
Date& Date::operator++()
{return *this += 1;
}// 后置++// 前置++和后置++都是一元运算符,为了让前置++与后置++形成能正确重载// C++规定:后置++重载时多增加一个int类型的参数,但调用函数时该参数不用传递,编译器自动传
递// 注意:后置++是先使用后+1,因此需要返回+1之前的旧值,故需在实现时需要先将this保存一份,
然后给this+1// 而temp是临时对象,因此只能以值的方式返回,不能返回引用Date Date::operator++(int)
{Date tmp(*this);*this += 1;return tmp;
}// 后置--
Date Date::operator--(int)
{Date tmp(*this);*this -= 1;return tmp;
}// 前置--
Date& Date::operator--()
{return *this -= 1;
}
日期-日期 返回天数
// 日期-日期 返回天数
int Date::operator-(const Date& d) const
{//思路,用小的一天天加上去//假定前一个是大日期,后一个小日期Date max = *this;Date min = d;int flag = 1;if (*this < d){max = d;min = *this;flag = -1;}int n = 0;while (min != max){++n;++min;}return n * flag;
}
Date.cpp文件内容
#include"Date.h"// >运算符重载
bool Date::operator>(const Date& d) const
{if ((_year > d._year)|| (_year == d._year && _month > d._month)|| (_year == d._year && _month == d._month && _day > d._day)){return true;}else{return false;}
}// ==运算符重载
bool Date::operator==(const Date& d) const
{return _year == d._year&& _month == d._month&& _day == d._day;
}// >=运算符重载
bool Date::operator>=(const Date& d) const
{return (*this == d) || (*this > d);
}// <运算符重载
bool Date::operator < (const Date& d) const
{return !(*this >= d);
}// <=运算符重载
bool Date::operator <= (const Date& d) const
{return !(*this > d);
}// !=运算符重载
bool Date::operator != (const Date& d) const
{return !(*this == d);
}// 赋值运算符重载
// d2 = d3 -> d2.operator=(&d2, d3)
Date& Date::operator=(const Date& d)
{if (this != &d){_year = d._year;_month = d._month;_day = d._day;}return *this;
}// 日期+=天数 d2+=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 == 13){++_year;_month = 1;}}return *this;
}// 日期+天数 d2+100
Date Date::operator+(int day) const
{Date ret = *this;ret += day;return ret;
}// 日期-天数
Date Date::operator-(int day) const
{Date ret(*this);ret -= day;return ret;
}// 日期-=天数
Date& Date::operator-=(int day)
{if (_day < 0){return *this += -day;}_day -= day;while (_day <= 0){--_month;if (_month == 0){_month = 12;--_year;}_day += GetMonthDay(_year, _month);}return *this;
}// 前置++
// 前置++:返回+1之后的结果// 注意:this指向的对象函数结束后不会销毁,故以引用方式返回提高效率
Date& Date::operator++()
{return *this += 1;
}// 后置++// 前置++和后置++都是一元运算符,为了让前置++与后置++形成能正确重载// C++规定:后置++重载时多增加一个int类型的参数,但调用函数时该参数不用传递,编译器自动传
递
// 注意:后置++是先使用后+1,因此需要返回+1之前的旧值,故需在实现时需要先将this保存一份,
然后给this + 1
// 而temp是临时对象,因此只能以值的方式返回,不能返回引用
Date Date::operator++(int)
{Date tmp(*this);*this += 1;return tmp;
}// 后置--
Date Date::operator--(int)
{Date tmp(*this);*this -= 1;return tmp;
}// 前置--
Date& Date::operator--()
{return *this -= 1;
}// 日期-日期 返回天数
int Date::operator-(const Date& d) const
{//思路,用小的一天天加上去//假定前一个是大日期,后一个小日期Date max = *this;Date min = d;int flag = 1;if (*this < d){max = d;min = *this;flag = -1;}int n = 0;while (min != max){++n;++min;}return n * flag;
}
以上就是整个日期类的实现,我们通过实现日期类可以对前面所学默认成员函数进行巩固加强。