C++ Implementation date class (Date)
- 2020-06-07 05:00:00
- OfStack
This article example Shared C++ implementation date class specific code for your reference, the specific content is as follows
#include<iostream>
#include<stdlib.h>
using namespace std;
class Date
{
public:
// The constructor
Date(int year = 1900, int month = 1, int day = 1)
:_year(year)
, _month(month)
, _day(day)
{
if (!IsInvalidDate(_year, _month, _day))
{
_year = 1900;
_month = 1;
_day = 1;
}
}
// Copy function
Date(const Date& d)
: _year(d._year)
, _month(d._month)
, _day(d._day)
{}
// The destructor
~Date()
{}
// Decide if it's a leap year
bool IsLeapYear(int year)
{
if ((year % 400 == 0) ||
((year % 4 == 0) && (year % 100 != 0)) )
{
return true;
}
return false;
}
// Determine if the date is valid
bool IsInvalidDate(int year, int month, int day)
{
if ((year < 1) ||
(month < 0 || month >12) ||
(day < 0 || day > YearsOfMonth(year, month)))
{
return false;
}
return true;
}
// Determine the number of days in the current month
int YearsOfMonth(int year, int month)
{
int day;
int days[13] = {0,31,28,31,30,31,30,31,31,30,31,30,31};
day = days[month];
if (month == 2 && IsLeapYear(year))
{
day += 1;
}
return day;
}
// Revision date
Date ToCorrect(Date &d)
{
while (d._day > YearsOfMonth(d._year, d._month) || d._day <= 0)
{
if(d._day <= 0)
{
d._day += YearsOfMonth(d._year,( d._month - 1));
if (d._month == 1)
{
d._month = 12;
d._year--;
}
else
{
d._month--;
}
}
else
{
d._day -= YearsOfMonth(d._year, d._month);
if (d._month == 12)
{
d._year++;
d._month = 1;
}
else
{
d._month++;
}
}
}
return d;
}
// The current date days What's the date?
Date operator+(int days)
{
Date tmp(*this);
tmp._day += days;
ToCorrect(tmp);
return tmp;
}
// The current date days What date was it ten days ago?
Date operator-(int days)
{
Date tmp(*this);
tmp._day -= days;
ToCorrect(tmp);
return tmp;
}
// Date ratio size
bool operator>(const Date& d)
{
return ( _year > d._year ||
(_year == d._year && _month > d._month) ||
(_year == d._year && _month == d._month && _day > d._day));
}
bool operator<(const Date& d)
{
return (_year < d._year ||
(_year == d._year && _month < d._month) ||
(_year == d._year && _month == d._month && _day < d._day));
}
bool operator==(const Date& d)
{
return ((_year == d._year) && (_month == d._month) && (_day == d._day));
}
bool operator!=(const Date& d)
{
return !(*this == d);
}
bool operator>=(const Date &d)
{
return !(*this<d);
}
bool operator<=(const Date &d)
{
return !(*this>d);
}
// Overload take address symbol
Date* operator&()
{
}
// Pre - ++
Date& operator++()
{
(*this)++;
return *this;
}
// The rear ++
Date operator++(int)// Distinguish between prepositions and postpositions by return values and pass arguments ++
{
Date tmp(*this);
(*this) = (*this) + 1;
return tmp;
}
// Pre - --
Date& operator--()
{
(*this)--;
return *this;
}
// The rear --
Date operator--(int)
{
Date tmp(*this);
(*this)--;
return tmp;
}
void Display()
{
cout << _year << "-" << _month << "-" << _day << endl;
}
private:
int _year;
int _month;
int _day;
};
int main()
{
Date d(2018, 9, 9);
d.Display();
Date d1 = d + 50;
d1.Display();
d1 =d1 - 50;
d1.Display();
cout << "------"<<endl;
cout << " Pre - ++" << endl;
d1.Display();
(++d1).Display();
d1.Display();
cout << " The rear ++" << endl;
d1.Display();
(d1++).Display();
d1.Display();
cout << "------" << endl;
system("pause");
return 0;
}