经过前面两篇文章的学习,相信聪明的你应该已经初步了解类与对象了,现在我们将一起实现一个日期类,进一步加深我面对类的理解。
在软件开发中,日期和时间的处理无处不在,从日程管理到金融计算,从数据分析到天气预报,日期类的设计都是开发者必须面对的挑战。在本文中,我们将从零开始,一步步实现一个功能完备的日期类。无论你是C++新手,还是想巩固面向对象编程基础,这个项目都会让你收获满满。
想象一下:如果你要实现你手机里的日历这个app,它应该有些什么功能呢?
我认为主要功能如下:
+
, -
, ==
, <<
等)要想实现上面的功能,我们需要三个内部成员来表示日期(年、月、日),在通过各种函数来实现各种功能
class Date
{
//友元声明
friend ostream& operator<<(ostream& _cout, const Date& d);
friend istream& operator>>(istream& _cin, Date& d);
public:
//构造函数
Date(int year = 1, int month = 1, int day = 1);
//拷贝构造函数
Date(Date& d)
{
_year = d._year;
_month = d._month;
_day = d._day;
}
//打印日历
void PrintCalendar()
{
cout << _year << "-" << _month << "-" << _day << endl;
}
//操作符重载
bool operator<(const Date& x) const;
bool operator==(const Date& x) const;
bool operator<=(const Date& x) const;
bool operator>(const Date& x) const;
bool operator>=(const Date& x) const;
bool operator!=(const Date& x) const;
// 获取某年某月的天数
int GetMonthDay(int year, int month);
Date& operator+=(int days);
Date operator+(int days);
Date& operator-=(int days);
Date operator-(int days);
Date& operator++();
Date operator++(int);
Date& operator--();
Date operator--(int);
// 日期类成员
private:
int _year; // 年
int _month; // 月
int _day; // 日
};
比较两天日期的大小我们用重载函数 bool operate<(const class& Date) const 和 bool operator==(const Date& x) const两个重载函数完成,剩下的可以用这两个表示
bool Date::operator<(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;
else
return false;
}
bool Date::operator==(Date& d)
{
if (_year == d._year && _month == d._month && _day == d._day)
return true;
else
return false;
}
bool Date::operator<=(Date& d)
{
return (*this) < d || (*this) == d;
}
bool Date::operator>(Date& d)
{
return !(*this <= d);
}
bool Date::operator>=(Date& d)
{
return !(*this < d);
}
bool Date::operator!=(Date& d)
{
return !(*this == d);
}
想完成日期类的加减,其实就是把日期里的天数都合法化,大于这个月的天数,就加一个月,日期小于等于0,就减少一个月,当月份加到13的时候,就重置为1,年份加1,同理,当月份减少到0的时候,就置为12,年份减1。这里我们还需要写一个函数来获取这个月的天数有多少天(主要是判断是否为闰年闰月,如果是就返回29天,其他就正常返回)
int Date::GetMonthDay(int year, int month)
{
int days[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;
return days[month];
}
Date& Date::operator+=(int days)
{
_day += days;
while (_day > GetMonthDay(_year, _month))
{
_day -= GetMonthDay(_year, _month);
_month++;
if (_month == 12)
{
_year++;
_month = 1;
}
}
return *this;
}
Date Date::operator+(int days) const
{
Date tmp = (*this);
tmp += days;
return tmp;
}
Date& Date::operator-=(int days)
{
_day -= days;
while (_day <= 0)
{
_day += GetMonthDay(_year, _month);
_month--;
if (_month == 0)
{
_month = 12;
_year--;
}
}
return *this;
}
Date Date::operator-(int days) const
{
Date tmp = *this;
tmp -= days;
return tmp;
}
Date& Date::operator++()
{
*this += 1;
return *this;
}
Date Date::operator++(int) const
{
Date tmp = *this;
tmp += 1;
return tmp;
}
Date& Date::operator--()
{
*this -= 1;
return *this;
}
Date Date::operator--(int) const
{
Date tmp = *this;
tmp -= 1;
return tmp;
}
int Date::operator-(const Date d) const
{
Date max = *this, min = d;
int flag = 1;
if (max < min)
{
flag = -1;
max = d;
min = *this;
}
int n = 0;
while (max != min)
{
++min;
++n;
}
return n * flag;
}
这里需要用到友元函数,才能将按我们的习惯把日出输出出来
ostream& operator<<(ostream& _cout, const Date& d)
{
_cout << d._year << "-" << d._month << "-" << d._day ;
return _cout;
}
istream& operator>>(istream& _cin, Date& d)
{
_cin >> d._year >> d._month >> d._day;
return _cin;
}
完成这些之后,我们可以通过库函数<ctime>去获得当天的日期,然后做一份倒计时出来
void TestCalendar1()
{
Date d1,d2;
d1.GetTodayData();
cout << "今天是:" << d1 << endl;
cout << "请输入蓝桥杯日期:" ;
cin >> d2;
cout << "距离蓝桥杯还有" << (d2 - d1) << "天,加油吧骚年" << endl;
}
int main()
{
TestCalendar1();
return 0;
}
这里就是日期类的全部实现内容啦,如果你也自己实现一遍,你对类和对象的理解将会上升一个层次,加油学习吧!