1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91
|
#include <iostream>
#include <cmath>
enum class month{
Jan = 1, Feb, Mar, Apr, May, Jun, Jul, Aug, Sep, Oct, Nov, Dec
};
class Year{
static const int min = 1800;
static const int max = 2200;
public:
class Invalid{};
Year(int x) : y{x}{if(x<min||max<=x) throw Invalid{};}
int year();
private:
int y;
};
int Year::year()
{
return y;
}
class Date{
int d;
Year y;
month m;
public:
Date(int dd, month mm, Year yy);
void add_day(Date& day, int n);
};
month operator++(month& m)
{
m =(m==month::Dec)? month::Jan : month(int(m)+1);
return m;
}
void Date::add_day(Date& day, int n)
{
d = d + n;
if(d > 31)
{
d = 1;
++m;
}
}
Date::Date(int dd, month mm ,Year yy)
{
if( dd >= 1 && dd <= 31) d = dd;
else d = 0;
m = mm;
y = yy;
}
using namespace std;
int main()
{
Date today{25,month::Jun,Year{1978}};
if(today.d() !=0 )
cout<<"Today's date is "<<today.d()<<"/"<<today.m()<<"/"<<today.y()<<endl;
else cout<<"Invalid date."<<endl;
add_day(today,1);
cout<<"Tomorrow's date is "<<today.d()<<"/"<<today.m()<<"/"<<today.y()<<endl;
return 0;
}
|