-
Notifications
You must be signed in to change notification settings - Fork 23
/
12.1 Date_PrefixOperatorsIncDec.cpp
63 lines (50 loc) · 1.14 KB
/
12.1 Date_PrefixOperatorsIncDec.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
// also contains postfix increment and decrement
#include <iostream>
using namespace std;
class Date
{
private:
int day, month, year;
public:
Date (int inMonth, int inDay, int inYear)
: month (inMonth), day(inDay), year (inYear) {};
Date& operator ++ () // prefix increment
{
++day;
return *this;
}
Date& operator -- () // prefix decrement
{
--day;
return *this;
}
Date operator ++ (int) // postfix increment
{
Date copy(month, day, year);
++day;
return copy;
}
Date operator -- (int) // postfix decrement
{
Date copy(month, day, year);
--day;
return copy;
}
void DisplayDate()
{
cout << month << " / " << day << " / " << year << endl;
}
};
int main ()
{
Date holiday (12, 25, 2021); // Dec 25, 2021
cout << "The date object is initialized to: ";
holiday.DisplayDate ();
++holiday; // move date ahead by a day
cout << "Date after prefix-increment is: ";
holiday.DisplayDate ();
--holiday; // move date backwards by a day
cout << "Date after a prefix-decrement is: ";
holiday.DisplayDate ();
return 0;
}