Define a class called Month that is an abstract data type for a month. Your clas
ID: 3722789 • Letter: D
Question
Define a class called Month that is an abstract data type for a month. Your
class should have a private intager variable that represents the month (1
for January, 2 for February, etc.). Include all the following member functions:
- A default constructor that set's the month to January.
- A constructor to set the month using the first three letters in the name
of the month as three arguments.
- A constructor to set the month using an integer as an argument.
- A mutator function that sets the month by passing in an integer.
- An accessor function that returns the value of the month as an integer.
- An accessor function that returns the first three letters in the name of the month.
- A Member function that returns the next month as a value of type Month.
Make sure your class validates user input to verify that a valid month was entered.
In main(), test your program by creating 3 different Month objects. The first month object
should use an integer value entered by the user as the argment to the constructor. Then,
use the Month class member functions to print out the first three letters of the corresponding
month and the first three letters of the next month.
c++
The second month object should take 3 letters that the user enters as arguments to the constructor.
Then, use the Month class member functions to print out the value of that month as an integer, as
well as the first three letters of the next month.
The third month object should be created using the default constructor. Print out the first three letters
of that month and the next month.
Explanation / Answer
Hello, I have a solution for you. Implemented everything as per the requirements. Defined following things in this answer.
//code.cpp
#include<iostream>
#include<ctype.h>
using namespace std;
class Month{
int month;
public:
/*default constructor, will set month to january*/
Month(){
month=1;
}
/*constructor with integer argument, will set month to month number specified*/
Month(int i){
setMonth(i);
}
/*constructor to set month using first 3 letters*/
Month(char a, char b, char c){
month=1;/*setting default value first, so that if the a,b,c doesn't form a valid month,
this value will be used*/
a=toupper(a); /*converting each characters to upper case*/
b=toupper(b);
c=toupper(c);
/*finding the month using first 3 chars*/
switch(a){
case 'J':
if(b=='A' && c=='N'){
month=1; //January
}else if(b=='U' && c=='N'){
month=6; //June
}else if(b=='U' && c=='L'){
month=7; //July
}
break;
case 'F':
if(b=='E' && c=='B'){
month=2; //February
}
break;
case 'M':
if(b=='A' && c=='R'){
month=3; //March
}else if(b=='A' && c=='Y'){
month=5; //May
}
break;
case 'A':
if(b=='P' && c=='R'){
month=4; //April
}else if(b=='U' && c=='G'){
month=8; //August
}
break;
case 'S':
if(b=='E' && c=='P'){
month=9; //September
}
break;
case 'O':
if(b=='C' && c=='T'){
month=10; //october
}
break;
case 'N':
if(b=='O' && c=='V'){
month=11; //November
}
break;
case 'D':
if(b=='E' && c=='C'){
month=12; //December
}
break;
}
}
/*method to set a month using month number*/
void setMonth(int i){
/**checking if number is in valid range */
if(i>=1 && i<=12){
month=i;
}else{
/*setting default value */
month=1;
}
}
//returns the month as integer
int getMonth(){
return month;
}
//returns the month's first 3 letters
char* getMonthAsLetters(){
char* MON;
/*finding the first 3 letters of the month*/
switch(month){
case 1:
MON="JAN";
break;
case 2:
MON="FEB";
break;
case 3:
MON="MAR";
break;
case 4:
MON="APR";
break;
case 5:
MON="MAY";
break;
case 6:
MON="JUN";
break;
case 7:
MON="JUL";
break;
case 8:
MON="AUG";
break;
case 9:
MON="SEP";
break;
case 10:
MON="OCT";
break;
case 11:
MON="NOV";
break;
case 12:
MON="DEC";
break;
}
return MON;
}
//returns a Month object holding next month
Month nextMonth(){
/* defining a Month object for next month.
will work even if current month is december, as it will be handled by the constructor */
Month m(month+1);
/*returning it*/
return m;
}
};
int main(){
int n;
cout<<"Enter the month number (will be set to January if wrong input is given) : ";
cin>>n;
/*creating a month from integer n*/
Month month1(n);
/*displaying month and next month*/
cout<<"Month is : "<<month1.getMonthAsLetters()<<endl;
cout<<"Next month: "<<month1.nextMonth().getMonthAsLetters()<<endl;
char a,b,c;
cout<<"Enter first three letters of the month (will be set to January if wrong input is given) : ";
cin>>a;
cin>>b;
cin>>c;
/*creating a month from first 3 characters*/
Month month2(a,b,c);
/*displaying month and next month*/
cout<<"Month is : "<<month2.getMonthAsLetters()<<endl;
cout<<"Next month: "<<month2.nextMonth().getMonthAsLetters()<<endl;
/*creating a month using default constructor*/
Month month3;
/*displaying month and next month*/
cout<<"Month (using default constructor) is : "<<month3.getMonthAsLetters()<<endl;
cout<<"Next month: "<<month3.nextMonth().getMonthAsLetters()<<endl;
return 0;
}
/*OUTPUT*/
Enter the month number (will be set to January if wrong input is given) : 8
Month is : AUG
Next month: SEP
Enter first three letters of the month (will be set to January if wrong input is given) : APR
Month is : APR
Next month: MAY
Month (using default constructor) is : JAN
Next month: FEB
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.