-
Notifications
You must be signed in to change notification settings - Fork 0
/
12thQuestion.cpp
95 lines (82 loc) · 1.95 KB
/
12thQuestion.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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
#include <iostream>
#include <string>
using namespace std;
class Month
{
public:
Month() : monthNumber(1), name("January") {}
Month(string monthName)
{
setMonthByName(monthName);
}
void setMonthByName(string monthName)
{
static string months[] = {"January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"};
for (int i = 0; i < 12; ++i)
{
if (months[i] == monthName)
{
monthNumber = i + 1;
name = monthName;
break;
}
}
}
void setMonthByNumber(int monthNum)
{
static string months[] = {"January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"};
if (monthNum >= 1 && monthNum <= 12)
{
monthNumber = monthNum;
name = months[monthNum - 1];
}
}
string getMonthName() const
{
return name;
}
int getMonthNumber() const
{
return monthNumber;
}
Month &operator++()
{
monthNumber = monthNumber % 12 + 1;
setMonthByNumber(monthNumber);
return *this;
}
Month operator++(int)
{
Month temp = *this;
++*this;
return temp;
}
Month &operator--()
{
monthNumber = (monthNumber - 2 + 12) % 12 + 1;
setMonthByNumber(monthNumber);
return *this;
}
Month operator--(int)
{
Month temp = *this;
--*this;
return temp;
}
private:
int monthNumber;
string name;
};
int main()
{
Month m1("April");
cout << m1.getMonthName() << endl;
cout << m1.getMonthNumber() << endl;
m1++;
cout << m1.getMonthName() << endl;
cout << m1.getMonthNumber() << endl;
m1--;
cout << m1.getMonthName() << endl;
cout << m1.getMonthNumber() << endl;
return 0;
}