"Task Requirements"
1. Implementation: Date + days = date;
2. Implementation: Date-days = date;
3. Implementation: Date-date = number of days;
"code implementation"
#include <iostream>using namespace std;class Date{friend ostream& operator<< (ostream& os ,const date& d);//friend Public:date (int year = &NBSP;1900,&NBSP;INT&NBSP;MONTH&NBSP;=&NBSP;1,&NBSP;INT&NBSP;DAY&NBSP;=&NBSP;1)//constructor: _year (year), _month (month ), _day (day) {if (Isinvaliddate ()) //processing Invalid date {_year = 1900;_month = 1;_day = 1;}} Date (CONST&NBSP;DATE&&NBSP;D)//copy constructor {_year = d._year;_month = d._month;_day = d._day;} Date& operator= (const date& d) //assignment operator overload {if (this != &d) {this->_ Year = d._year;this->_month = d._month;this->_day = d._day;} Return *this;} public:/* determine if the date is valid */bool isinvaliddate () {if ((_year<1900) | | ((_month < 1) | | (_month > 12)) | | ((_day < 1) | | _day > getmonthday ())) {Return true;} Return false;} /* Determine if _year is a leap year */bool leapyear () {return (0 == _year %4 &&0 != _ YEAR&NBSP;%&NBSP;100) | | (0 == _year % 400);} /* Get _month How many days */int getmonthday () {int days[13] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};if (LeapYear ()) {Days[2] += 1 ;} Return days[_month];} /* Determine if two dates are equal */bool operator== (const date& d) {return (this-> _year == d._year) && (this->_month == d._month) && (This->_day == d._day); }bool operator!= (const Date& d) {return ! ( *THIS&NBSP;==&NBSP;D) }/* judge the size relationship between *this and D */bool operator> (const Date& &NBSP;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&NBSP;>&NBSP;D);} bool operator>= (Const date& d) {return ((*this > d) | | (*this == d));} bool operator<= (Const date& d) {return ((*this < d) | | (*this == d));} Public:void getnowdate ()//Get new Date {while (_day <= 0) {if (_month == 1) {_month = 12;_year -= 1; }else {_month -= 1;} _day += getmonthday ();} while (_day > getmonthday ()) {_day -= getmonthday (); if (_month == 12) {_ month = 1;_year += 1;} else{_month += 1;} }}date operator+ (INT&NBsp;day)//date + days = Date {int days[13] = {0, 31, 28, 31, 30, &NBSP;31,&NBSP;30,&NBSP;31,&NBSP;31,&NBSP;30,&NBSP;31,&NBSP;30,&NBSP;31};D ate d (*this);d. _day += day;d.getnowdate (); return d;} date operator-(Int day)//date - days = date {int days[13] = {0, 31, 28,&NBSP;31,&NBSP;30,&NBSP;31,&NBSP;30,&NBSP;31,&NBSP;31,&NBSP;30,&NBSP;31,&NBSP;30,&NBSP;31};D ate d (* This);d. _day -= day;d.getnowdate (); return d;} date& operator+= (int day) {_day += day;this->getnowdate (); return *this;} date& operator-= (int day) {_day -= day;this->getnowdate (); return *this;} int operator-(const date& d) //date-date = days {int day = 0;if (*this &NBSP;>&NBSP;D) {date d1 = *this;*this = d;} while (*this != d) {*this += 1;day++;} Return day;} private:int _year;int _month;int _day;};o stream& operator<< (ostream& os ,const date& d) //output operator overloading {os << d._year << "-" << d._month < < "-" &NBSP;<<D._DAY&NBSP;<<&NBSP;ENDL;&NBSP;&NBSP;&NBSP;&NBSP;RETURN&NBSP;OS;} Int main () { date d (2016, 3, 2);D ate d1 (1995, 7, 14); Int ret = d-d1;cout << "by the end of " << d << "you have already lived for " << ret << " days" << endl;system ("pause"); return 0;}
Results:
650) this.width=650; "src=" Http://s4.51cto.com/wyfs02/M00/7C/BB/wKiom1bWwyniBmg_AAATelSQaQc829.png "title=" ( 4h7np9ukj[) 71s%5[bk2{g.png "alt=" Wkiom1bwwynibmg_aaatelsqaqc829.png "/>
This article is from the "Pzd Chuan Feng" blog, please make sure to keep this source http://xujiafan.blog.51cto.com/10778767/1746869
Simple Date Class