日期类 Date
生活中经常用到 , 查询两个日期相差多少天 , 多少天之后是哪一天 等等
这些问题都可以用日期类实现
日期的构造
Date(int t_year, int t_month, int t_day)
: m_year(t_year), m_month(t_month), m_day(t_day)
{
cout << "Date(...)" << endl;
if (!(m_year >= 1900
&& m_month > 0 && m_month < 13
&& m_day > 0 && m_day <=
GetMonthDay(m_year, m_month)))
{
cout << "非法日期" << endl;
}
}
日期的拷贝构造
Date(const Date &date)
{
// cout << "Date(const Date& date)" << endl;
m_year = date.m_year;
m_month = date.m_month;
m_day = date.m_day;
}
日期的赋值
Date &operator=(const Date &date)
{
// cout << "operator=(...)" << endl;
if(this != &date) // 防止自己给自己赋值
{
m_year = date.m_year;
m_month = date.m_month;
m_day = date.m_day;
}
return *this;
}
完整代码
#include<iostream>
using namespace std;
// 日期类
// 四个主要的默认成员函数
// 1. 构造函数
// 2. 析构函数
// 3. 拷贝构造函数
// 4. 赋值操作符重载
class Date
{
public:
// 1. 构造函数
Date(int t_year, int t_month, int t_day)
: m_year(t_year), m_month(t_month), m_day(t_day)
{
cout << "Date(...)" << endl;
if (!(m_year >= 1900
&& m_month > 0 && m_month < 13
&& m_day > 0 && m_day <= GetMonthDay(m_year, m_month)))
{
cout << "非法日期" << endl;
}
}
// 2. 析构函数
~Date()
{
cout << "~Date()" << endl;
}
// 3. 拷贝构造函数
Date(const Date &date)
{
cout << "Date(const Date& date)" << endl;
m_year = date.m_year;
m_month = date.m_month;
m_day = date.m_day;
}
// 4. 赋值操作符重载
Date &operator=(const Date &date)
{
cout << "operator=(...)" << endl;
if(this != &date)
{
m_year = date.m_year;
m_month = date.m_month;
m_day = date.m_day;
}
return *this;
}
// 设定日期
void SetDate(int t_year, int t_month, int t_day)
{
if(t_year < 1900 || t_month < 1 || t_month > 12 || t_day < 1 || t_day > GetMonthDay(t_year, t_month))
{
cout << "非法日期" << endl;
return ;
}
m_year = t_year;
m_month = t_month;
m_day = t_day;
}
// 显示日期
void Display()
{
cout << m_year << "-" << m_month << "-" << m_day << endl;
}
// 获得某个月的天数
int GetMonthDay(const int &t_year, const int &t_month)
{
if(t_year < 1900 || t_month < 1 || t_month > 12)
return -1;
int days[13] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
if(t_month == 2)
{
if((t_year % 4 == 0 && t_year % 100 != 0)
|| t_year % 400 == 0)
{
return days[2] + 1;
}
}
return days[t_month];
}
// 重载 + 运算符
// 2018-7-6 + 100天 = ?
Date operator+ (int days)
{
if(days == 0)
return *this;
Date ret = *this;
ret.m_day += days;
while(ret.m_day > GetMonthDay(ret.m_year, ret.m_month))
{
ret.m_day -= GetMonthDay(ret.m_year, ret.m_month);
++ret.m_month;
if(ret.m_month > 12)
{
++ret.m_year;
ret.m_month = 1;
}
}
return ret;
}
Date &operator+= (int days)
{
*this = *this + days;
return *this;
}
// 重载 += 运算符
// 2018-7-6 += 100天 = ?
// Date operator+= (int days)
// {
// ;
// }
// 重载 + - > < == != >= <= & 运算符
// Date operator+(int day);
bool operator!= (const Date &d)
{
return !(*this == d);
}
// 日期 - 天数
Date operator-(int day)
{
Date ret(*this);
ret.m_day -= day;
while(ret.m_day <= 0)
{
ret.m_month--;
if(ret.m_month == 0)
{
ret.m_year--;
}
ret.m_day += GetMonthDay(ret.m_year, ret.m_month);
}
return ret;
}
// 日期 - 日期
int operator-(const Date &d)
{
if(this == &d)
return 0;
Date min(d);
Date max(*this);
int count = 0;
while(min != max)
{
min++;
count++;
}
return count;
}
Date operator++(int)
{
*this += 1;
return *this;
}
bool operator>(const Date &x)
{
if(m_year < x.m_year)
return false;
else if(m_year > x.m_year)
return true;
else
{
if(m_month < x.m_month)
return false;
else if(m_month > x.m_month)
return true;
else
{
if(m_day <= x.m_day)
return false;
else if(m_day > x.m_day)
return true;
}
}
return false;
}
bool operator< (const Date &d)
{
return !(*this == d || *this > d);
}
bool operator==(const Date &x)
{
return m_year == x.m_year &&
m_month == x.m_month &&
m_day == x.m_day;
}
bool operator>=(const Date &x)
{
return !(*this < x);
}
bool operator<=(const Date &x)
{
return !(*this > x);
}
Date *operator& (const Date &x)
{
return this;
// return NULL;
}
private:
int m_year;
int m_month;
int m_day;
};
测试代码
#include "date.h"
void TestDate_01()
{
Date d1(2018, 7, 6);
Date d2 = d1 + 100;
d2.Display();
d1.Display();
d1 += 1000;
d1.Display();
return ;
}
void TestDate_02()
{
Date d1(2018, 7, 16);
Date d2 = d1;
Date d3(2018, 7, 20);
d2 = d3;
d1.Display();
d2.Display();
cout << &d1 << endl;
cout << &d2 << endl;
cout << &d3 << endl;
if(d1 == d2)
{
cout << "d1 == d2" << endl;
}
else
{
cout << "d1 != d2" << endl;
}
}
void TestDate_03()
{
Date d1(2018, 7, 21);
Date d2(2018, 7, 22);
Date d3(2019, 7, 22);
if(d2 < d1)
cout << "true" << endl;
else
cout << "false" << endl;
Date d4 = d1 - 100;
cout << d3 - d2 << endl;
d4.Display();
}
int main()
{
// TestDate_01();
// TestDate_02();
TestDate_03();
return 0;
}