Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

I need help with this C++ problem. 14.7: ===== Design a class named Month. The c

ID: 3918659 • Letter: I

Question

I need help with this C++ problem.

14.7:

=====

Design a class named Month. The class must have a private member of type
int to hold the month number and a static member of type array of strings to
hold the month names in the fashion of a lookup table.
The class will have a default constructor that sets the month number to 1,
as well as a constructor that accepts an int argument representing the month
number.
In addition, write the appropriate mutator functions to set the month
based on month number and month name.
Overload both the prefix and postfix versions of ++ and -- operators, such
that they increment or decrement the month number respectively. Note: if month
number is 12 when the increment operator is called, the month number will
become 1. By the same token, if month number is 1 and the decrement operator
is called, the month number becomes 12.
Also overload the << and >> operators such that the << operator displays a
month name and >> operator allows the user to input a month number.
Write a test program that would demonstrate the Month class. The program
would allow the user input two month numbers and display the next 8 months and
the previous 8 months (including the month that the user entered).
You may assume the user will always input an integer between 1 and 12.

Explanation / Answer

#include<iostream>
#include<string>

using namespace std;

class Month{

   private:
     int n;
    
   public:
     static string names[12];
     Month(){
        n = 1;
     }
     Month(int a){
        n = a;
     }
     void setN(int a){
        n = a;
     }
     void setN(string a){
        for (int i = 0; i<12; i++){
            if (Month::names[i] == a){
               n = i+1;
               break;
            }
        }
       
     }
     void operator ++(){
        ++n;
        if (n >= 12)
           n = 1;
     }
     void operator ++(int){
       
        n++;
        if (n >= 12)
           n = 1;
     }
     void operator --(){
        --n;
        if (n <= 0)
           n = 12;
     }
     void operator --(int){
        n--;
        if (n <= 0)
           n = 12;
     }
     friend ostream & operator << (ostream &out, const Month &c);
     friend istream & operator >> (istream &in, Month &c);
   
};

ostream & operator << (ostream &out, const Month &c)
{
    //string a[] = c.getNames();
    out << Month::names[c.n-1] << endl;
    return out;
}

istream & operator >> (istream &in, Month &c)
{
    cout << "Enter month number :";
    in >> c.n;
    return in;
}

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

int main(){


   Month m;
   cin >> m;
   for (int i = 0; i<8; i++){
      cout << m << endl;
      m++;
    
   }
   cin >> m;
   for (int i = 0; i<8; i++){
      cout << m << endl;
      m--;
    
   }
   return 0;
}