Month Class Design a class named Month. The class should have the following priv
ID: 3801598 • Letter: M
Question
Month Class
Design a class named Month. The class should have the following private members:
Name: A string object that holds the name of a month, such as January, February, etc.
monthNumber: An integer variable that holds the number of the month. For example, January would be 1, February would be 2, etc. Valid values for this variable are 1 through 12.
In addition, provide the following member functions:
A default constructor that sets monthNumber to 1 and name to January.
A constructor that accepts the name of the month as an argument. It should set name to the value passed as the argument and set monthNumber to the correct value.
A constructor that accepts the number of the month as an argument. It should set monthNumber to the value passed as the argument and set name to the correct month name.
Appropriate set and get functions for the name and monthNumber member variables.
Prefix and postfix overloaded ++ operator functions that increment monthNumber and set name to the name of next month. If monthNumber is set to 12 when these functions execute, they should set monthNumber to 1 and name to January.
Overload the = operator. It should set both the monthName and monthNumber variables.
Overload cout s << operator and cin s >> operator to work with the Month class. Demonstrate the class in a program.
<< cout s stream insertion operator. This operator should cause the date to be displayed in the form shown in the sample output below.
o >> cin s stream extraction operator. This operator should prompt the user for the name of a month. In the same operation, it should also set the monthName and monthNumber variables.
Requirements
You can assume the name of the month will be typed in with the first letter capitalized and all other letters lowercase.
The cin’s >> operator should read the month’s name (make sure you update the number).
Write the code for assigning to the month and monthNumber attributes in the set functions, and just call the appropriate set function from the constructors.
Use the main function below.
int main()
{
Month m1("February");
Month m2,m3, m4;
cout <<m1.getMonthName() << endl; cout <<m1.getMonthNumber()<< endl;
cout << endl;
cout << m1;
cout << endl; m2 = ++m1; cout << m1; cout << m2;
cout << endl; m3 = m1++; cout << m1; cout << m3;
cout << endl;
cout << "Enter the name of a month: ";
cin >> m4;
cout << m4;
system ("Pause") return 0;
}
Figure 1 - Sample Output
February The name is February and the number is 2 The name is March and the number is 3 The name is March and the number is 3 The name is April and the number is 4 The name is March and the number is 3 Enter the name of a month: April The name is April and the number is 4 Press any key to continueExplanation / Answer
#include<bits/stdc++.h>
using namespace std;
class Month
{
string name;
int monthNumber;
public:
Month()
{
name="January";
monthNumber=1;
}
Month(string n)
{
name=n;
if(n.compare("January")==0)
{
monthNumber=1;
}
else if(n.compare("February")==0)
{
monthNumber=2;
}else if(n.compare("March")==0)
{
monthNumber=3;
}else if(n.compare("April"))
{
monthNumber=4;
}else if(n.compare("May"))
{
monthNumber=5;
}else if(n.compare("June"))
{
monthNumber=6;
}else if(n.compare("July"))
{
monthNumber=7;
}else if(n.compare("Augest"))
{
monthNumber=8;
}else if(n.compare("September"))
{
monthNumber=9;
}else if(n.compare("October"))
{
monthNumber=10;
}else if(n.compare("November"))
{
monthNumber=11;
}
else
{
monthNumber=12;
}
}
void setMonthName(string n)
{
name=n;
}
void setMonthNumber(int n)
{
monthNumber=n;
}
string getMonthName()
{
return name;
}
int getMonthNumber()
{
return monthNumber;
}
Month operator ++()
{
if(monthNumber==12)
{
monthNumber=1;
}
else
{
monthNumber++;
}
switch(monthNumber)
{
case 1:name="January";break;
case 2:name="February";break;
case 3:name="March";break;
case 4:name="April";break;
case 5:name="May";break;
case 6:name="June";break;
case 7:name="July";break;
case 8:name="Augest";break;
case 9:name="September";break;
case 10:name="October";break;
case 11:name="November";break;
case 12:name="December";break;
}
return *this;
}
Month operator ++(int)
{
Month M(name);
if(monthNumber==12)
{
monthNumber=1;
}
else
{
monthNumber++;
}
switch(monthNumber)
{
case 1:name="January";break;
case 2:name="February";break;
case 3:name="March";break;
case 4:name="April";break;
case 5:name="May";break;
case 6:name="June";break;
case 7:name="July";break;
case 8:name="Augest";break;
case 9:name="September";break;
case 10:name="October";break;
case 11:name="November";break;
case 12:name="December";break;
}
return M;
}
void operator = (const Month &m) {
name = m.name;
monthNumber = m.monthNumber;
}
friend istream & operator >> (istream &in, Month &c);
friend ostream & operator << (ostream &out, Month &c);
};
ostream & operator << (ostream &out, Month &c)
{
out<<"The name is "<<c.name<<" and the number is "<<c.monthNumber<<endl;
return out;
}
istream & operator >> (istream &in, Month &c)
{
in >> c.name;
string n=c.name;
if(!n.compare("January"))
{
c.monthNumber=1;
}
if(!n.compare("February"))
{
c.monthNumber=2;
}if(!n.compare("March"))
{
c.monthNumber=3;
}if(!n.compare("April"))
{
c.monthNumber=4;
}if(!n.compare("May"))
{
c.monthNumber=5;
}if(!n.compare("June"))
{
c.monthNumber=6;
}if(!n.compare("July"))
{
c.monthNumber=7;
}if(!n.compare("Augest"))
{
c.monthNumber=8;
}if(!n.compare("September"))
{
c.monthNumber=9;
}if(!n.compare("October"))
{
c.monthNumber=10;
}if(!n.compare("November"))
{
c.monthNumber=11;
}if(!n.compare("December"))
{
c.monthNumber=12;
}
return in;
}
int main()
{
Month m1("February");
Month m2,m3, m4;
cout <<m1.getMonthName() << endl;
cout <<m1.getMonthNumber()<< endl;
cout << endl;
cout << m1;
cout << endl;
m2 = ++m1;
cout << m1;
cout << m2;
cout << endl; m3 = m1++; cout << m1; cout << m3;
cout << endl;
cout << "Enter the name of a month: ";
cin >> m4;
cout << m4;
}
================================================================
Output:
akshay@akshay-Inspiron-3537:~/Chegg$ g++ month.cpp
akshay@akshay-Inspiron-3537:~/Chegg$ ./a.out
February
2
The name is February and the number is 2
The name is March and the number is 3
The name is March and the number is 3
The name is April and the number is 4
The name is March and the number is 3
Enter the name of a month: April
The name is April and the number is 4