-
Notifications
You must be signed in to change notification settings - Fork 0
/
Special_Date.cpp
67 lines (58 loc) · 1.02 KB
/
Special_Date.cpp
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
#include "Special_Date.h"
#include <stdexcept>
namespace Special_Date
{
Date::Date(int l)
:days(l)
{
if(!is_date(l))
{
throw std::runtime_error("invalid date");
}
}
int Date::get_date() const
{
return days;
}
void Date::add_day(int n)
{
if(n < 0)
{
throw std::runtime_error("you can add a negative day");
}
days+=n;
}
bool is_date(int l)
{
if(l < 0)
{
return false;
}
return true;
}
bool operator==(const Date& l, const Date& r)
{
return l.get_date() == r.get_date();
}
bool operator!=(const Date& l, const Date& r)
{
return !(l==r);
}
std::ostream& operator<<(std::ostream& os,const Date& d)
{
std::cout << d.get_date() << '\n';
return os;
}
std::istream& operator>>(std::istream& is,Date& d)
{
int n;
is >> n;
if(n < 0)
{
is.clear(std::ios_base::failbit); // set the fail bit
return is;
}
d = Date(n);
return is;
}
}