吝为您导航吝●六个人主页:你帅你先说.
●欢迎点赞关注收藏
●既选择了远方,便只顾风雨兼程。
●蘭欢迎大家有问题随时私信我!
●類版权:本文由[你帅你先说.]原创,CSDN首发,侵权必究。
裡1.设计思路2.Date类框架3.类成员函数实现
裡1.设计思路本次我们设计的日期类大致要实现日期大小的判断、日期的加减、日期的输入输出、日期对应星期几。这将把我们前面类和对象中所学的知识点运用起来,让我们对类和对象有一个更深的理解。
2.Date类框架Date.h
#pragma once #include3.类成员函数实现using namespace std; class Date { // 友元函数 friend ostream& operator<<(ostream& out, const Date& d); friend istream& operator>>(istream& in, Date& d); public: Date(int year = 1, int month = 1, int day = 1); void Print() const; int GetMonthDay(int year, int month) 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; bool operator!=(const Date& d)const; Date& operator+=(int day); Date operator+(int day)const; Date& operator-=(int day); Date operator-(int day)const; Date& operator++(); Date operator++(int); Date& operator--(); Date operator--(int); int operator-(const Date& d) const; void PrintWeekDay() const; private: int _year; int _month; int _day; };
这些实现放在Date.cpp文件里
首先要实现的接口就是构造函数
void Date::Print() const
{
cout << _year << "-" << _month << "-" << _day << endl;
}
Date::Date(int year, int month, int day)
{
_year = year;
_month = month;
_day = day;
if (!(_year >= 0
&& (month > 0 && month < 13)
&& (day > 0 && day <= GetMonthDay(year, month))))
{
cout << "非法日期->";
Print();
}
}
接下来我们要实现Date类中最核心的一个接口:GetMonthDay,这个接口当你传入年和月时,它会返回对应的天数,这里就涉及到平年和闰年的问题了。C语言阶段我们学过判断闰年,有一句口诀:四年一闰,百年不闰,四百年再闰,用代码来实现就是
year % 4 == 0 && year % 100 != 0 || year % 400 == 0
int Date::GetMonthDay(int year, int month) const
{
static int monthDayArray[13] = { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
int day = monthDayArray[month];
if (month == 2 && ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0)))
{
day += 1;
}
return day;
}
完成了这个接口的实现,接下来的接口就轻松了。
重载>运算符
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;
}
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);
}
重载>=运算符
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);
}
錄重载+=运算符
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)
{
_month = 1;
_year++;
}
}
return *this;
}
重载+运算符
Date Date::operator+(int day) const
{
Date ret(*this);
//ret.operator+=(day);
ret += day;
return ret;
}
樓重载-=运算符
Date& Date::operator-=(int day)
{
if (day < 0)
{
return *this += -day;
}
_day -= day;
while (_day <= 0)
{
--_month;
if (_month == 0)
{
--_year;
_month = 12;
}
_day += GetMonthDay(_year, _month);
}
return *this;
}
重载-运算符
//计算减掉几天后的日期
Date Date::operator-(int day) const
{
Date ret(*this);
ret -= day;
return ret;
}
//计算日期差
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 count = 0;
while (min != max)
{
++min;
++count;
}
return count * flag;
}
重载前置++运算符
Date& Date::operator++()
{
*this += 1;
return *this;
}
重载后置++运算符
Date Date::operator++(int)
{
Date ret(*this);
*this += 1;
return ret;
}
重载前置--运算符
Date& Date::operator--()
{
*this -= 1;
return *this;
}
婢重载后置--运算符
Date Date::operator--(int)
{
Date ret(*this);
*this -= 1;
return ret;
}
输出星期几
void Date::PrintWeekDay() const
{
const char* arr[] = { "星期一", "星期二", "星期三", "星期四", "星期五", "星期六", "星期天" };
//Date start(1900, 1, 1);
//int count = *this - start;
int count = *this - Date(1900, 1, 1);
cout << arr[count % 7] << endl;
}
徭重载<<运算符
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;
}
Date.h和Date.cpp文件里的内容实现完后,整个日期类就完成了。接下来就可以在Test.cpp文件里调用运行了。觉得太简陋了,也可以在Test.cpp里写个菜单。
喜欢这篇文章的可以给个一键三连 点赞关注收藏



