/
Item18_Date.cpp
79 lines (62 loc) · 1.36 KB
/
Item18_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
68
69
70
71
72
73
74
75
76
77
78
// Item 18: Make interfaces easy to use correctly and hard to use incorrectly
#include <iostream>
using namespace std;
struct Day {
explicit Day (int d):
val(d){}
int val;
};
class Month
{
public:
static Month Jan() { return Month(1); }
static Month Feb() { return Month(2); }
static Month Mar() { return Month(3); }
static Month Apr() { return Month(4); }
static Month May() { return Month(5); }
static Month Jun() { return Month(6); }
static Month Jul() { return Month(7); }
static Month Aug() { return Month(8); }
static Month Sep() { return Month(9); }
static Month Oct() { return Month(10); }
static Month Nov() { return Month(11); }
static Month Dec() { return Month(12); }
int getVal () {return val;}
private:
explicit Month (int m):
val(m){}
int val;
};
struct Year {
explicit Year (int y):
val(y){}
int val;
};
class Date
{
public:
Date (const Month& m, const Day& d, const Year& y):
iMonth(m),
iDay(d),
iYear(y)
{}
void ToString( );
private:
Month iMonth;
Day iDay;
Year iYear;
};
void Date::ToString( )
{
cout << iDay.val << "/" <<iMonth.getVal() << "/" << iYear.val << endl;
}
int main ( )
{
Date d ( Month::Jul(), Day(12), Year(1984));
d.ToString();
}
/*
Notes:
a). tr1::shared_ptr has a deleter feature that is not present in auto_ptr.
b). tr1::shared_ptr eliminates "cross-DLL problem".
*/