Design a class named Month. The class should have the following private members:
ID: 3568016 • Letter: D
Question
Design a class named Month. The class should have the following private members:
name - A character pointer 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 copy constructor that takes an object and copies all member variables into the current object.
An overloaded = operator that takes an object and copies all member variables into the current object
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.
Prefix and postfix overloaded -- operator functions that decrement monthNumber and set name to the name of previous month. If monthNumber is set to 1 when these functions execute, they should set monthNumber to 12 and name to December.
Also, you should overload cout's << operator and cin's >> operator to work with the Month class. The << operator should display the monthNumber followed by a dash and the name of the month. The >> operator should prompt the user to enter the name of the month, store it in the object and set the monthNumber to the appropriate value.
Explanation / Answer
Program Code:
// Month.cpp : Defines the entry point for the console application.
//
#include <iostream>
#include <string>
using namespace std;
class Month
{
private:
string name;
int monthNumber;
public:
Month()
{
name="January";
monthNumber=0;
}
Month(const Month &m)
{
name=m.name;
monthNumber=m.monthNumber;
}
void operator=(const Month &m )
{
name=m.name;
monthNumber=m.monthNumber;
}
void setMonthNumber(int n)
{
monthNumber=n;
}
void setMonthName(string mname)
{
name=mname;
}
void setMonthName1(int num);
int getMonthNumber()
{return monthNumber;}
string getMonthName()
{return name;}
Month operator++()
{
++monthNumber; // increment this object
if(monthNumber>12)
{
setMonthNumber(monthNumber=1);
setMonthName(name="January");
}
else
{
setMonthName1(monthNumber);
}
return *this;
}
Month& operator++(int)
{
monthNumber++;
if(monthNumber>12)
{
setMonthNumber(monthNumber=1);
setMonthName(name="January");
}
else
{
setMonthName1(monthNumber);
}
return *this;
}
Month& operator--()
{
--monthNumber; // increment this object
if(monthNumber<1)
{
setMonthNumber(monthNumber=12);
setMonthName(name="December");
}
else
{
setMonthName1(monthNumber);
}
return *this;
}
Month& operator--(int)
{
monthNumber--;
if(monthNumber<1)
{
setMonthNumber(monthNumber=12);
setMonthName(name="December");
}
else
{
setMonthName1(monthNumber);
}
return *this;
}
friend ostream &operator<<(ostream &out, Month m)
{
out<<m.getMonthNumber()<<" - "<<m.getMonthName()<<" ";
return out;
}
friend istream &operator>>(istream &in, Month &m)
{
cout<<"Enter the name of the month: ";
in>>m.name;
if(m.name=="January")
{
m.monthNumber=1;
}
else if(m.name=="February")
{
m.monthNumber=2;
}
else if(m.name=="March")
{
m.monthNumber=3;
}
else if(m.name=="April")
{
m.monthNumber=4;
}
else if(m.name=="May")
{
m.monthNumber=5;
}
else if(m.name=="June")
{
m.monthNumber=6;
}
else if(m.name=="July")
{
m.monthNumber=7;
}
else if(m.name=="August")
{
m.monthNumber=8;
}
else if(m.name=="September")
{
m.monthNumber=9;
}
else if(m.name=="October")
{
m.monthNumber=10;
}
else if(m.name=="November")
{
m.monthNumber=11;
}
else if(m.name=="December")
{
m.monthNumber=12;
}
else
{
m.monthNumber=1;
}
return in;
}
};
void Month::setMonthName1(int num)
{
if(num==1)
{
name="January";
}
else if(num==2)
{
name=="February";
}
else if(num==3)
{
name=="March";
}
else if(num==4)
{
name="April";
}
else if(num==5)
{
name="May";
}
else if(num==6)
{
name="June";
}
else if(num==7)
{
name="July";
}
else if(num==8)
{
name="August";
}
else if(num==9)
{
name="September";
}
else if(num==10)
{
name="October";
}
else if(num==11)
{
name="November";
}
else if(num==12)
{
name="December";
}
else
{
name="";
}
}
int main()
{
Month mon;
cout<<"Enter the required data. ";
cin>>mon;
cout<<mon;
mon++;
cout<<mon;
--mon;
cout<<mon;
system("pause");
return 0;
}
Sample Output:
Enter the required data.
Enter the name of the month:
April
4 - April
5 - May
4 - April
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.