IN C++ PLEASE>>> Design and code a class that represents a single month of the y
ID: 3797371 • Letter: I
Question
IN C++ PLEASE>>> Design and code a class that represents a single month of the year. It should have a single integer-typed data member to represent the month. Make sure to provide proper construction, access, and mutation for this member as well as input and output methods. In addition, methods for comparing two months chronologically for ordering would be useful as would a method to advance to the next month (note that January follows December and begins the cycle anew) or previous month. In fact, thinking about it, it might be good to be able to advance to a month an arbitrary number of whole months in the future or past: January '+' 5 = June; March '-' 5 = November. Place your Month class in a library. Write a test application (driver) for your class. Oh, and the programmers who commissioned this class wanted to make sure it could handle not only integer-valued representation of a Month but also either whole-word names or three-letter abbreviations for the Months. That means that they should be able to construct a Month object from either an integer value or a string -- which could be either the whole month name or a 3-letter version there-of. Similarly, they'd like to be able to retrieve the Month's information as either an integer value or a string (of either form), mutate the Month by any of these means, display by any of the three forms, and even allow their user to enter any of the three forms at the console. But, to keep things as simple as possible, they'd not like a whole lot of different names to remember. Please use the following names for all functions that perform such tasks: set_month, get_month, input, output, advance, before, after, same. (There may be only one of some methods, but others may need at least two methods to offer all requested functionality.)
Explanation / Answer
// Month.h header file
#include <iostream>
#include <string>
using namespace std;
class Month
{
private:
int month;
public:
//construct if month is given in number;
Month(int mon);
//construct if month is given as a whole word or 3 letter word
Month(string mon);
//get the month valu
int get_month();
//set the month value
void set_month(int mon);
//compare the months
int compare(int m2);
//advance month by 1
void advance();
//get the previous month
void previous();
//advance month by n
void after(int n);
//get n months backwards
void before(int n);
//output month by number
void output();
//output month by whole word
void output_string();
//output month by 3 letter
void output_str();
};
//Month.cpp file
Month::Month(int mon)
{
month = mon;
}
Month::Month(string mon)
{
if((!mon.compare("jan"))||(!mon.compare("january")))
month = 1;
if((!mon.compare("feb"))||(!mon.compare("february")))
month = 2;
if((!mon.compare("mar"))||(!mon.compare("march")))
month = 3;
if((!mon.compare("apr"))||(!mon.compare("april")))
month = 4;
if((!mon.compare("may")))
month = 5;
if((!mon.compare("jun"))||(!mon.compare("june")))
month = 6;
if((!mon.compare("jul"))||(!mon.compare("july")))
month = 7;
if((!mon.compare("aug"))||(!mon.compare("august")))
month = 8;
if((!mon.compare("sep"))||(!mon.compare("september")))
month = 9;
if((!mon.compare("oct"))||(!mon.compare("october")))
month = 10;
if((!mon.compare("nov"))||(!mon.compare("november")))
month = 11;
if((!mon.compare("dec"))||(!mon.compare("december")))
month = 12;
}
int Month::get_month()
{
return month;
}
void Month::set_month(int mon)
{
month = mon;
}
int Month::compare(int m2)
{
if(get_month() > m2)
return 1;
else
return 0;
}
void Month::advance()
{
//if month is december next month will be (12+1)%12 == 1 (jan)
this->set_month(((this->get_month()+1)%12));
}
void Month::previous()
{
int mon = this->get_month()-1;
//if month is jan set december
if(mon == 0)
this->set_month(12);
else
this->set_month(mon);
}
void Month::after(int n)
{
this->set_month(((this->get_month()+n)%12));
}
void Month::before(int n)
{
int mon = this->get_month() - n;
//if month is feb and n = 3 then 2-3 = -1... by adding 12(-1+12 = 11) we get november
if(mon < 0 )
this->set_month(mon + 12);
else
this->set_month(mon);
}
void Month::output()
{
cout<<get_month()<<endl;
}
void Month::output_string()
{
switch(this->get_month())
{
case 1: cout<<"janauary";
break;
case 2: cout<<"february";
break;
case 3: cout<<"march";
break;
case 4: cout<<"april";
break;
case 5: cout<<"may";
break;
case 6: cout<<"june";
break;
case 7: cout<<"july";
break;
case 8: cout<<"august";
break;
case 9: cout<<"september";
break;
case 10: cout<<"october";
break;
case 11: cout<<"november";
break;
case 12:cout<<"december";
break;
}
cout<<endl;
}
void Month::output_str()
{
switch(this->get_month())
{
case 1: cout<<"jan";
break;
case 2: cout<<"feb";
break;
case 3: cout<<"mar";
break;
case 4: cout<<"apr";
break;
case 5: cout<<"may";
break;
case 6: cout<<"jun";
break;
case 7: cout<<"jul";
break;
case 8: cout<<"aug";
break;
case 9: cout<<"sep";
break;
case 10: cout<<"oct";
break;
case 11: cout<<"nov";
break;
case 12:cout<<"dec";
break;
}
cout<<endl;
}
int main()
{
//input number
Month m1(1);
//input 3 leter word
Month m2("jan");
//input whole word
Month m3("february");
m1.advance();
m2.previous();
m3.after(5);
m3.output();
m3.before(5);
m3.output_str();
m2.output_string();
m1.output();
return 0;
}