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
|
#include <iostream>
#include <string>
using namespace std;
namespace Chrono{
enum class Month{ // Month class
jan = 1, feb, mar, apr, may, jun, jul, aug, sep, oct, nov, dec
};
class Date{ // Date class
public:
class Invalid{};
Date(int y, Month m, int d); // constructor
Date(); // defaut constructor
//nonmodifying operations
int day() const { return d; }
Month month() const { return m; }
int year() const { return y; }
//modifying operations
void add_day(int n);
void add_month(int n);
void add_year(int n);
private:
int y;
Month m;
int d;
};
Date::Date(int yy, Month mm, int dd) :y{ yy }, m{ mm }, d{ dd } {} // constructor
const Date& default_date(){ //default date for default constructor
static Date dd{ 2001, Month::jan, 1 };
return dd;
}
Date::Date() // default constructor
:y{ default_date().year() },
m{ default_date().month() },
d{ default_date().day() }
{}
std::ostream& operator<<(std::ostream& os, const Date& d){ // << operator
return os << '(' << d.year() << ',' << int(d.month()) << ',' << d.day() << ')';
}
std::istream& operator>>(std::istream& is, Date&& dd){ // >> operator
int y, m, d;
char ch1, ch2, ch3, ch4;
is >> ch1 >> y >> ch2 >> m >> ch3 >> d >> ch4;
if (!is) return is;
if (ch1 != '(' || ch2 != ',' || ch3 != ',' || ch4 != ')'){
is.clear(std::ios_base::failbit);
return is;
}
dd = Date(y, Month(m), d); // update dd
return is;
}
}
int main(){
Chrono::Date my_date;
cout << "enter date ";
cin >> my_date;
cout << "date you entered - " << my_date << endl;
}
|