1. Consider the validity of the date. If it is not legal, set it as January 1, 1990
2. Because in date class, we don't use the development of resources, so we can use the copy construction of compiler automatic synthesis, overload of assignment operator, etc.
3. What is the date after the implementation of a date plus N days
4. What is the date after subtracting N days from a date
5. How many days are there between the two dates
The code is as follows:
#include<iostream>
using namespace std;
class Date
{
public:
Date(int year, int month, int day)
: _year(year)
, _month(month)
, _day(day)
{
if (!(year > 0 && month > 0 && month < 13 && day>0 && day <= GetDayOfMonth(year, month)))
{
_year = 1990;
_month = 1;
_day = 1;
}
}
bool isleap(int year) //Determine if it is a leap year
{
if ((0 == year % 4 && 0 != year % 100) || (year % 400 == 0))
{
return true;
}
else
{
return false;
}
}
int GetDayOfMonth(int year, int month)//The number of days in each month is different. Get the number of days in this month according to each month
{
int mon[] = { 0,31,28,31,30,31,30,31,31,30,31,30,31 };
if (2 == month)
{
if (isleap(year))
{
return mon[2] += 1;
}
}
return mon[month];
}
// What is the day after the current day?
Date operator+(int days)
{
Date tmp(*this);
if (days < 0) // Adding a negative number of days is equivalent to subtracting a positive number of days
{
days = -days;
return tmp - days;
}
tmp._day += days;
while (tmp._day > GetDayOfMonth(tmp._year,tmp._month))
{
tmp._day -= GetDayOfMonth(tmp._year, tmp._month);
tmp._month++;
if (tmp._month > 12)
{
tmp._year++;
tmp._month = 1;
}
}
return tmp;
}
// What is the day before the current date?
Date operator-(int days)
{
Date tmp(*this);
if (days < 0) //Subtracting a negative number of days is equivalent to adding a positive number of days
{
days = -days;
return tmp + days;
}
tmp._day -= days;
while (tmp._day < 0)
{
tmp._month--;
if (tmp._month < 1)
{
tmp._year--;
tmp._month = 12;
}
tmp._day += GetDayOfMonth(tmp._year, tmp._month);
}
return tmp;
}
// Find the difference between two dates
int operator-(const Date& d)
{
int count = 0;
Date Mindate(*this); //Hypothetical minimum
Date Maxdate(d); //Maximum hypothesis
if (Mindate > Maxdate) //If not, exchange two objects to satisfy
{
Maxdate = Mindate;
Mindate = d;
}
while (Maxdate != Mindate)
{
Mindate++;
count++;
}
return count;
}
// Prefix + +
Date& operator++()
{
*this = *this + 1;
return *this;
}
// Post + +
Date operator++(int)
{
Date tmp(*this);
*this = *this + 1;
return tmp;
}
Date& operator--()
{
*this = *this - 1;
return *this;
}
Date operator--(int)
{
Date tmp(*this);
*this = *this - 1;
return tmp;
}
// Judge whether two dates are equal
bool operator==(const Date& d)
{
if (_year == d._year &&
_month == d._month &&
_day == d._day)
{
return true;
}
else
{
return false;
}
}
// Judge whether two dates are different
bool operator!=(const Date& d)
{
if (*this == d)
{
return false;
}
else
{
return true;
}
}
bool operator>(const Date& d)
{
if ((_year > d._year) ||
(_year == d._year&&_month > d._month) ||
(_year == d._year&&_month == d._month&&_day > d._day))
{
return true;
}
else
{
return false;
}
}
bool operator>=(const Date& d)
{
if (*this > d || *this == d)
{
return true;
}
else
{
return false;
}
}
bool operator<(const Date& d)
{
if (*this > d)
{
return false;
}
else
{
return true;
}
}
bool operator<=(const Date& d)
{
if (*this >= d)
{
return false;
}
else
{
return true;
}
}
private:
int _year;
int _month;
int _day;
};
int main()
{
Date d1(2018, 5, 6);
Date d2(2026, 10, 25);
cout << d2 - d1 << endl;
d1++;
d1--;
--d1;
++d1;
//d1 - 999;
//d1 + 100;
//d1 - (-999);
return 0;
}
The above is a simple implementation of date class. If you have any suggestions, please get your suggestions. If you have any errors, please get correction.