Overloading Operators

Working on a program that takes a number 1-12 as input and returns the string month name (January, February, March, etc.).

I need to overload the increment/decrement operators as well as the ostream istream operators.


After I return the month name from the integer 1-12 I need to demonstrate the overloaded increment/decrement operators by printing out the next eight and previous eight months from the one they entered.

My issue is getting it to increment from December to January, and decrement from January to December. I thought I set it up correctly but I'm stuck.

Any hints or advice would be much appreciated. Thank you!


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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
  
#include<iostream>
#include<string>
using namespace std;

class Month
{
    private:
        string monthName;
        static string monthNames[12];
        int monthNumber;

    public:
        Month()
        {
            monthNumber = 1;
        }

        Month(int monthNum)
        {
            setName(monthNum);
            cout << monthName << " is month number " << monthNum << endl;
        }

        void setName(int number)
        {
               if (number == 1)
                  monthName = monthNames[0];
              else if (number == 2)
                   monthName = monthNames[1];
              else if (number == 3)
                  monthName = monthNames[2];
              else if (number == 4)
                  monthName = monthNames[3];
              else if (number == 5)
                  monthName = monthNames[4];
              else if (number == 6)
                  monthName = monthNames[5];
              else if (number == 7)
                  monthName = monthNames[6];
              else if (number == 8)
                  monthName = monthNames[7];
              else if (number == 9)
                  monthName = monthNames[8];
              else if (number == 10)
                  monthName = monthNames[9];
              else if (number == 11)
                  monthName = monthNames[10];
              else if (number == 12)
                  monthName = monthNames[11];
               else
                    cout<<"Invalid number. Only numbers 1-12 accepted."<<endl;
        }

        void setNumber(string monthName)
        {
            if( monthName==monthNames[0])
                monthNumber=1;
            else if(monthName==monthNames[1])
                monthNumber=2;
            else if(monthName==monthNames[2])
                monthNumber=3;
            else if(monthName==monthNames[3])
                monthNumber=4;
            else if(monthName==monthNames[4])
                monthNumber=5;
            else if (monthName==monthNames[5])
                monthNumber=6;
            else if(monthName==monthNames[6])
                monthNumber=7;
            else if(monthName==monthNames[7])
                monthNumber=8;
            else if(monthName==monthNames[8])
                monthNumber=9;
            else if(monthName==monthNames[9])
                monthNumber=10;
            else if (monthName==monthNames[10])
                monthNumber=11;
            else if(monthName==monthNames[11])
                monthNumber=12;
            else
                cout<<"Invalid month name."<<endl;
        }

        int getNumber()
        { return monthNumber;}

        string getName()
        { return monthName; }

        friend ostream &operator << (ostream &, const Month &);
        friend istream &operator >> (istream &,Month &);

        Month& operator++();
        Month operator++(int);
        Month& operator--();
        Month operator--(int);

};

string Month::monthNames[12] =
        {"January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"};

//Overloaded Operators
Month& Month::operator++()
{
	if (this->monthNumber == 12)
		this->monthName = monthNames[0];
	else
		this->monthNumber++;
		setName(monthNumber);
	return *this;
}

Month Month::operator++(int)
{
	Month temp(*this);
	operator++();
	return temp;
}

Month& Month::operator--()
{
	if (this->monthNumber == 1)
		this->monthName = monthNames[11];
	else
		this->monthNumber--;
		setName(monthNumber);
	return *this;
}

Month Month::operator--(int)
{
	Month temp(*this);
	operator--();
	return temp;
}

ostream &operator<<(ostream &strm, const Month &obj)
{

 strm << Month::monthNames[obj.monthNumber-1]<< endl;
   return strm;
}

istream  &operator >> (istream &strm, Month &obj)
{
    cout<<"Enter in month number: ";
    strm >> obj.monthNumber;
    cout<<endl;

   return strm;
}

int main()
{
     Month * month2 = new Month();

    Month m1;
    cout<<"Input a month number 1-12 to return the month name. \n";
    cin>>m1;
    cout << m1;

    Month temp;
    temp = m1;

    cout<<"The next eight months are: " << endl;
    for (int i=0; i < 8; i++)
    {
        temp++;
        cout << temp << endl;
    }


     cout<<"The previous eight months are: " << endl;
    for (int i=0; i < 8; i++)
    {
        m1--;
        cout << m1 << endl;
    }

}
Topic archived. No new replies allowed.