-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMonth.cpp
More file actions
109 lines (95 loc) · 2.63 KB
/
Month.cpp
File metadata and controls
109 lines (95 loc) · 2.63 KB
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
//imported class
#include "Month.h"
//preprocessor directives
#include <string>
#include <iostream>
using namespace std;
//default constructor initializing month name and number
Month::Month() {
name = "January";
monthNumber = 1;
}
//constructor that takes one string parameter and sets name and month number accordingly
Month::Month(string n) {
setMonthName(n);
}
//constructor that takes one int parameter and sets name and month number accordingly
Month::Month(int num) {
setMonthNumber(num);
}
//setter method for the month name, also sets corresponding number
void Month::setMonthName(string n) {
name = n;
for (int i{ 0 }; i < 12; i++) {
if (name == months[i]) {
monthNumber = i + 1;
}
}
}
//setter method for the month number, also sets corresponding month name
void Month::setMonthNumber(int num) {
monthNumber = num;
name = months[monthNumber - 1];
}
//getter method for the month name
string Month::getMonthName() const {
return name;
}
//getter method for the month number
int Month::getMonthNumber() const {
return monthNumber;
}
//overloaded prefix ++ operator
//increments month number, and name accordingly
//returns current state of obejct
Month Month::operator++() {
++monthNumber;
name = months[monthNumber - 1];
if (monthNumber == 12) {
name = "January";
monthNumber = 1;
}
return *this;
}
//overloaded postfix ++ operator
//increments month number, and name accordingly
//dummy parameter is used, and temporary instance of class is declared before incrementation actually takes place
Month Month::operator++(int) {
Month m(monthNumber);
monthNumber++;
name = months[monthNumber - 1];
if (monthNumber == 12) {
name = "January";
monthNumber = 1;
}
return m;
}
//overloaded assignment operator from right to left
//returns current state of object
const Month Month::operator=(const Month &right) {
name = right.name;
monthNumber = right.monthNumber;
return *this;
}
//overloaded stream extraction operator
//displays the following sentence when called
ostream& operator<< (ostream& strm, const Month & obj)
{
strm << "The name is " << obj.getMonthName() << " and the number is " << obj.getMonthNumber() << "." << endl;
return strm;
}
//overloaded stream insertion operator
//stores month name in memory and sets corresponding month name using setter method for the month name
istream& operator>>(istream& strm, Month & obj)
{
string n;
strm >> n;
while (strm.fail()) {
strm.clear();
strm.ignore(32767, '\n');
cout << "Invalid entry. Try again:";
strm >> n;
}
obj.setMonthName(n);
return strm;
}