欢迎来到尧图网

客户服务 关于我们

您的位置:首页 > 教育 > 锐评 > 【C++】日期类的实现

【C++】日期类的实现

2026/5/1 21:01:59 来源:https://blog.csdn.net/HEBRE/article/details/143064185  浏览:    关键词:【C++】日期类的实现

日期类是我们C++学习类和对象的一个很好的练习实例,它可以帮助我们更好的理解和掌握类和对象的内容,日期类中实现了日期类的构造,析构,拷贝构造以及日期大小的比较,日期加减天数等一系列功能。

一.日期类的功能

这就是日期类要实现的内容,我们将它放在.h头文件当中。

​
class Date
{
public:// 获取某年某月的天数int GetMonthDay(int year, int month);// 全缺省的构造函数Date(int year = 0, int month = 0, int day = 0);// 拷贝构造函数Date(const Date& d);// 赋值运算符重载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);
private:int _year;int _month;int _day;
};​

二.日期类的实现

1.默认构造

由于日期类的默认构造比较简单,所以我们直接在.h文件里实现就好,之后要实现的代码在.c文件中编写。

	// 全缺省的构造函数Date(int year = 0, int month = 0, int day = 0):_year(year),_month(month),_day(day){}// 拷贝构造函数Date(const Date& d){_year = d._year;_month = d._month;_day = d._day;}// 赋值运算符重载Date& operator=(const Date& d){_year = d._year;_month = d._month;_day = d._day;return *this;}// 析构函数~Date(){_year = 0;_month = 0;_day = 0;}

2.获取某年某月的天数

这个成员函数是为了给之后比较大小以及日期加减铺垫一下。

int Date::GetMonthDay(int year, int month)
{int arr[13] = { 0,31,28,31,30,31,30,31,31,30,31,30,31 };//判断是否为闰年if (month == 2 && ((year % 400 == 0) || (year % 100 != 0 && year % 4 == 0))){return 29;}return arr[month];
}

3.日期类的比较大小

这里我们其实只需要实现>和=的运算符重载就可以了,其他的运算符都可以使用这两个进行复用进而实现代码。

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

4.日期+和+=一个天数

这里我们同样可以只实现+=,在对+=复用去实现+

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

5.日期-和-=一个天数

实现原理和+和+=相类似

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

6.自加和自减的运算符重载

重载++运算符时,有前置++和后置++,运算符重载函数名都是operator++,无法很好的区分。C++规定,后置++重载时,增加⼀个int形参,跟前置++构成函数重载,方便区分。

这里的代码我们可以直接复用之前+=的代码就可以了。

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

7.日期-日期并返回天数

日期类中,日期加另一个日期是没有任何意义的,而日期减日期可以算出两个日期之间间隔的天数。

// 日期-日期 返回天数
int Date::operator-(const Date& d)
{Date Big = *this;Date Small = d;int flag = 1;if (Big < Small){Big = d;Small = *this;flag = -1;}int num = 0;while (Big > Small){Small++;num++;}return num * flag;
}

8.输入输出运算符重载

输入输出运算符的重载需要我们将它们重载成一个全局的函数,因为要保证流输入和流输出类的参数保持在第一位,而如果将其定义为一个类的成员函数的话,它的第一个参数默认就会是this,这就无法保证输入输出运算符的两个参数的位置,也就无法连续输入或者输出数据。

这里为了能够使用Date类中的成员变量,我们的做法是将其设置为Date类的友元函数,只需要在Date类中添加友元声明:

	friend ostream& operator<<(ostream& out, const Date& d);friend istream& operator>>(istream& in, Date& 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;
}

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com

热搜词