VS2013
#define _CRT_SECURE_NO_WARNINGS 1#include<assert.h> #include <iostream>using namespace std;//before implementing the operation of the date, the date is checked for illegal class date{public:date (int year,int month,int day) //Constructor {if (year >= 2000 && month > 0 & & month<13 && day>0 < getmonthday (year, month)) //determine if the date is illegal {_year = year;_month = month;_ Day = day;} else{cout << "Date illegal" << endl;assert (FALSE);}} Date (const date& d) //Copy Construction {_year = d._year;_month = d._month;_day = d._day;} Void display () {cout << _year << "-" << _month << "-" << _day << endl;} ~date () {//cout << "~date ()" &NBSP;<<&NBSP;ENDL;} Operator overloading bool operator== (const date &d) {return (_year == d._year) & & (_month == d._month) && (_day == d._day);} Bool operator> (const date &d) {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;} Elsereturn false;} 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&NBSP;>&NBSP;D);} Date operator= (const date &d) //assignment operator overloading {if ( This != &d) {This->_year = d._year;this->_month = d._month;this->_ Day = d._day;} Return *this;} Private:bool isleapyear (int year) //whether the leap year {if ((year % 4 == 0) && (year % 100 != 0)) | | (year % 400 == 0)) Return true;elsereturn false;} Int getmonthday (Int year, int month) //gets the number of days of the month, based on the known date {int montharray[13] = { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };int day = montharray[month];if (Month == 2 && isleapyear (year)) {day += 1 ;} Return day;} public:date operator+ (Int day) //gives a date plus day {if (day < 0) {return operator-(-day);} date tmp = *this;int sumdays = tmp._day + day;while (sumDays > getmonthday (Tmp._year, tmp._month)) {Sumdays -= getmonthday (tmp._year, tmp. _month);tmp._month++;if (tmp._month > 12) {tmp._year++;tmp._month %= 12;} Else{tmp._day = sumdays;} }return tmp;} date & operator+= (Int day) // Add the corresponding number of days after the assignment {*this = operator+ (day); return *this;} date operator-(Int day) &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBsp;//gives a date minus day days {if (day < 0) {return operator+ (-day);} date tmp = *this;while (day >= tmp._day) {day -= tmp._day;if ( tmp._month == 1) {tmp._year--;tmp._month = 12;} else{tmp._month--;} Tmp._day = getmonthday (tmp._year, tmp._month);} Tmp._day -= day;return tmp;} date & operator-= (Int day) // Subtract the corresponding number of days after the assignment {*this = operator-(day); return *this;} date & operator++ () //date plus 1{++_day;if (_day > getmonthday (_year, _month)) {_day -= getmonthday (_year, _month);++_month;if (_month > 12) {++_year;_month = 1;}} Return *this;} date & operator++ (int) //back ++{date tmp = *this;*this = operator++ (); return tmp;} date & operator--() {if (_day > 1) {--_day;} else{if (_month == 1) {--_year;_month = 12;_day = getmonthday (_year, _ month);} Else{--_month;_day = getmonthday (_year, _month);}} Return *this;} date & operator--(int) {date tmp = *this;*this = operator--(); return tmp;} Calculates the number of days between two dates int operator-(date & d) {if (_year <d. _year) {Date tmp =* this;*this = d;d = tmp;} else if (_year == d._year&&_month < d._month) {Date tmp = *this;*this = d;d = tmp;} else if (_year ==d. _year&&_month == d._month&&_day < d._day) {date tmp = *this;*this = d;d = tmp;} DATE&NBSP;TMP1 (*this);D ate&NBSP;TMP2 (d);int ret = 0;while (! ( TMP2&NBSP;==&NBSP;TMP1) {tmp2.operator++ (); ret++;} Return ret;} private:int _year;int _month;int _day;}; Void test1 () {/*date d1 (2016, 1, 15);d 1. Display ();D ate d2 = d1;d2. Display ();D ate d3;d3 = d1;d2. Display (); */date d1 (2016, 1, 15);D ate d2 (2016, 1, 16);d 1. Display ();d 2. Display ();// == 0//bool ret = d1 == d2;//cout < < ret << endl;// > 1//bool ret = d1 >d2;//cout << ret << endl;// < 0//bool ret = d1 < d2;//cout << ret << endl;// >= 1//bool ret = d1 >= d2;//cout << ret << endl;// <= 0//bool ret = d1 <= d2;//cout << ret << endl;// = //d1 = d2; //d1. Display (); //d2. Display ();} Void test2 () {date d1 (2016, 2,1);d 1. Display ();//date ret = d1+1;//ret. Display ();//date ret=d1+70;//ret. Display ();//date ret=d1-25;//ret. Display ();////date ret = d1 += 70;////ret. Display ();////d1. Display ();//date ret = d1 -= 70;//ret. Display ();//d1. Display ();//d1++;//d1. Display ();//++d1;//d1. Display ();//--d1;//d1. Display ();//d1--;//d1. Display ();D ate d2 (2016, 2, 29);d 2. Display (); Int ret = d1 - d2;cout << ret << endl;} Int main () {//test1 (); Test2 (); System ("pause"); return 0;}
C + + Implementation Date Class