C++ 日期类Date的实现(运算符重载)
该类综合考察了关于类的基本操作,包括构造,拷贝构造,析构,运算符重载等方面的知识。
Date类的内容和实现
1. 该日期类主要实现日期时间的加减递增递减等基本运算
2. 主要通过运算符重载来实现日期的各种运算
3. 重载的运算符之间会反复调用,测试阶段的数据逐一进行测试,易于排错
Date.cpp
#pragma once
#include <iostream>
using namespace std;
class Date
{
public:
// 获取某年某月的天数
int GetMonthDay(int year, int month)
{
static const int monthday[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 monthday[month];
}
// 全缺省的构造函数
Date(int year = 1900, int month = 1, int day = 1)
{
if (year >= 0
&& month > 0 && month < 13
&& day>0 && day <= GetMonthDay(year, month))
{
_year = year;
_month = month;
_day = day;
}
else
{
cout << "Date invalid" << endl;
}
}
// 拷贝构造函数
Date(const Date& d)
{
this->_year = d._year;
_month = d._month;
_day = d._day;
}
// 赋值运算符重载
// d2 = d3 -> d2.operator=(&d2, d3)
Date& operator=(const Date& d)
{
if (this != &d)
{
this->_year = d._year;
this->_month = d._month;
this->_day = d._day;
}
return *this;
}
// 析构函数
~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;
}
// 日期+天数
Date operator+(int day)
{
Date tmp(*this);
tmp += day;
return tmp;
}
// 日期-天数
Date operator-(int day)
{
Date tmp(*this);
tmp -= day;
return tmp;
}
// 日期-=天数
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& operator++()
{
*this += 1;
return *this;
}
// 后置++
Date operator++(int)
{
Date tmp(*this);
*this += 1;
return tmp;
}
// 后置--
Date operator--(int)
{
Date tmp(*this);
*this -= 1;
return tmp;
}
// 前置--
Date& operator--()
{
*this -= 1;
return *this;
}
// >运算符重载
bool operator>(const Date& d)
{
if (_year > d._year)
{
return true;
}
else if (_year == d._year)
{
if (_month > d._month)
{
return true;
}
else if (_month == d._month)
{
if (_day > d._day)
{
return true;
}
}
}
return false;
}
// <运算符重载
bool operator < (const Date& d)
{
return !(*this>=d);
}
// ==运算符重载
bool operator==(const Date &d)
{
return ((_year == d._year) && (_month == d._month) && (_day == d._day));
}
// >=运算符重载
bool operator >= (const Date &d)
{
return (*this > d)||(*this==d);
}
// <=运算符重载
bool operator <= (const Date &d)
{
return !(*this > d);
}
// !=运算符重载
bool operator != (const Date &d)
{
return !(*this == d);
}
// 日期-日期 返回天数
int operator-(const Date &d)
{
int flag = 1;
Date max = *this;
Date min = d;
if (*this < d)
{
max = d;
min = *this;
flag = -1;
}
int day = 0;
while (min < max)
{
++(min);
++(day);
}
return day * flag;
}
void Print()
{
cout << _year << "-" << _month << "-" << _day << endl;
}
private:
int _year;
int _month;
int _day;
};
int main()
{
Date d(2020, 7, 26);
d.Print();
Date d1 = d + 50;
d1.Print();
Date d2(2020, 6, 8);
d2 += 50;
d2.Print();
Date d3 = d2 - (-50);
d3.Print();
Date d4 = d3 + (-50);
d4.Print();
int k = d3 > d4;
cout << k << endl;
Date d5 = d4;
(d5++).Print();
(++d5).Print();
int a = d3 - d4;
cout << a << endl;
Date d6 = d5;
(d6--).Print();
(--d6).Print();
int c = d5 - d6;
cout << c << endl;
system("pause");
return 0;
}