Date class CDate, consists of three members of the private data year, month, day (both are integers), represent the year, month, day,
Before the member function declaration is as follows: (2) are private
Int IsLeapYear (int y);//return y year is a leap year, return 1, otherwise it returns 0
Int DaysOfMonth (int y, int m);//return the number of days from m y
CDate ();//the constructor, the Default for May 7, 2015, at the same time the output information "Default constructor is called!"
CDate (int y, int m, int d);//Constructor, (date) (month) (year) respectively, y, m, d, at the same time the output information "Constructor is called!"
Void SetDate (int y, int m, int d);//set the modification Date to on d m y years, at the same time the output information "Date is reset!"
Int GetYear ();//return the year
Int GetMonth ();//return in
Int GetDay ();//return day
Void the Show ();//in accordance with the specified format (see Sample Output) Output date information
CDate IncOneDay ();//return the date behind one day corresponds to (date) (month) (year)
Design the above class, and adopts the following main function test (only when the submit to submit in addition to the main function of the code section)
Int main ()
{
CDate d1;
D1. The Show ();
D1. SetDate (2015,2,28);
D1. IncOneDay (.) the Show ();
D1. The Show ();
CDate d2,12,31 (2016);
D2. The Show ();
D1, d2=. IncOneDay ();
D1. The Show ();
D2. The Show ();
D1. SetDate (d2) GetYear (), d2, GetMonth (), d2. GetDay ());
D1. The Show ();
return 0;
}
Sample Output
The Default constructor is called.
The 2015-5-7
The Date is reset!
The 2015-3-1
The 2015-2-28
The Constructor is called.
The 2016-12-31
The 2017-1-1
The 2016-12-31
The Date is reset!
The 2016-12-31
[program]
#include
using namespace std;
The class CDate
{
Private:
Int year, month, day.
Int IsLeapYear (int y)
{
Int leap=0;
If ((y % 4==0 & amp; & Y %==0 100) | | y %==0 400)
Leap=1;
Return leap;
}
Int DaysOfMonth (int y, int m)
{
31,28,31,30,31,30,31,31,30,31,30,31 int a [12]={};
If (IsLeapYear (y))
A [2] +=1;
Return a, [m].
}
Public:
CDate () : the year (2015), the month (5), day (7)
{
Cout<" The Default constructor is called!"
CDate (int y, int m, int d) : the year (y), month (m), day (d)
{
Cout<" The Constructor is called!"
Void SetDate (int y, int m, int d)
{
Year=y;
The month=m;
Day=d;
Cout<" The Date is reset!"
Const int GetYear ()
{
During the return;
}
Const int GetMonth ()
{
Return the month;
}
Const int GetDay ()
{
The return day.
}
Void the Show () const
{
Cout
CDate IncOneDay ()
{
Day++;
If (IsLeapYear (year))//judge the leap year February
{
If (the month==2 & amp; & Day==30)
{
Day=1;
The month +=1;
}
}
Else if (the month==2 & amp; & Day==29)
{
Day=1;
Month++;
}
If (1 month==| | the month==3 | | the month==5 | | the month==7 | | the month==8 | | the month==10 | | the month==12)
{//monthly
the condition of theIf (day> 31)
{
Day -=31;
The month +=1;
}
If (month> 12)
{
Year +=1;
The month -=12;
}
}
Else if (the month==4 | | 6 month==| | 9 month==| | the month==11)
{//miscarriage
the condition of theIf (day> 30)
{
Day -=30;
The month +=1;
}
}
Return CDate (year, month, day);
}
};
Int main ()
{
CDate d1;
D1. The Show ();
D1. SetDate (2015,2,28);
D1. IncOneDay (.) the Show ();
D1. The Show ();
CDate d2,12,31 (2016);
D2. The Show ();
D1, d2=. IncOneDay ();
D1. The Show ();
D2. The Show ();
D1. SetDate (d2) GetYear (), d2, GetMonth (), d2. GetDay ());
D1. The Show ();
return 0;
}
[results]
The Default constructor is called.
The 2015-5-7
The Date is reset!
The Constructor is called.
The 2015-3-1
The 2015-3-1
The Constructor is called.
The 2016-12-31
The Constructor is called.
The 2017-1-1
The 2017-1-1
The Date is reset!
The 2017-1-1