C++ program help This lab will help you practice with classes. Design and code a
ID: 3883409 • Letter: C
Question
C++ program help
This lab will help you practice with classes. 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 Month s. 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.) (Perhaps if you had a previous library that did stuff with times and/or dates, it might prove useful here?) (Or was the library over here?) (Oh! I know! I saw it right under this) (Ooo! Here it is!)Explanation / Answer
#include<iostream>
#include<string.h>
#include<stdlib.h>
using namespace std;
//Class Month definition
class Month
{
//Data member to store month number
int monthNumber;
public:
//Member functions
void set_month(int);
int get_month();
void input();
void output();
void advance();
void before();
void after();
void same();
};//End of class
//Function to check month is same or not
void Month::same()
{
//To store month number
int mo;
//Variable month - to store month in character form.
//Variable monthPart - to store month first three letters
string month, monthPart;
//Accept the month
cout<<" Enter either Month Number / Month Name / First three letter of month: ";
cin>>month;
//Checks whether the first character is number or not
if(isdigit(month[0]))
{
//Checks whether double digit
if(month.length()== 2)
{
//Converts to integer
mo = month[0] - '0';
//Multiply by 10 and adds the next digit to make it double digit
mo = mo * 10;
int next = month[1] - '0';
mo = mo + next;
}//End of inner if
//If single digit
else
//Converts to integer
mo = month[0] - '0';
//Checks for invalid entry
if(mo < 0 && mo > 12)
cout<<" Invalid month number. ";
}//End of outer if
//If in character form
else
{
//Subtracts the first three characters from the entry
monthPart = month.substr(0,3);
//Converts to upper case
for(int x = 0; x < 3; x++)
monthPart[x] = toupper(monthPart[x]);
//Compares the month and stores its corresponding number form
if(monthPart.compare("JAN") == 0)
mo = 1;
else if(monthPart.compare("FEB") == 0)
mo = 2;
else if(monthPart.compare("MAR") == 0)
mo = 3;
else if(monthPart.compare("APR") == 0)
mo = 4;
else if(monthPart.compare("MAY") == 0)
mo = 5;
else if(monthPart.compare("JUN") == 0)
mo = 6;
else if(monthPart.compare("JUL") == 0)
mo = 7;
else if(monthPart.compare("AUG") == 0)
mo = 8;
else if(monthPart.compare("SEP") == 0)
mo = 9;
else if(monthPart.compare("OCT") == 0)
mo = 10;
else if(monthPart.compare("NOV") == 0)
mo = 11;
else if(monthPart.compare("DEC") == 0)
mo = 12;
else
cout<<" Invalid month name. ";
}//End of else
//Checks for similarity
if(mo == monthNumber)
cout<<" Same month ";
else
cout<<" Not Same month ";
}//End of function
void Month::input()
{
//Variable month - to store month in character form.
//Variable monthPart - to store month first three letters
string month, monthPart;
//To store month number
int mo;
//Accept the month
cout<<" Enter either Month Number / Month Name / First three letter of month: ";
cin>>month;
//Checks whether the first character is number or not
if(isdigit(month[0]))
{
//Checks whether double digit
if(month.length()== 2)
{
//Converts to integer
mo = month[0] - '0';
//Multiply by 10 and adds the next digit to make it double digit
mo = mo * 10;
int next = month[1] - '0';
mo = mo + next;
}//End of inner if
//If single digit
else
//Converts to integer
mo = month[0] - '0';
//Checks for invalid entry
if(mo < 0 && mo > 12)
cout<<" Invalid month number. ";
//Set the month number
set_month(mo);
}//End of outer if
//If in character form
else
{
//Subtracts the first three characters from the entry
monthPart = month.substr(0,3);
//Converts to upper case
for(int x = 0; x < 3; x++)
monthPart[x] = toupper(monthPart[x]);
//Compares the month and stores its corresponding number form
if(monthPart.compare("JAN") == 0)
set_month(1);
else if(monthPart.compare("FEB") == 0)
set_month(2);
else if(monthPart.compare("MAR") == 0)
set_month(3);
else if(monthPart.compare("APR") == 0)
set_month(4);
else if(monthPart.compare("MAY") == 0)
set_month(5);
else if(monthPart.compare("JUN") == 0)
set_month(6);
else if(monthPart.compare("JUL") == 0)
set_month(7);
else if(monthPart.compare("AUG") == 0)
set_month(8);
else if(monthPart.compare("SEP") == 0)
set_month(9);
else if(monthPart.compare("OCT") == 0)
set_month(10);
else if(monthPart.compare("NOV") == 0)
set_month(11);
else if(monthPart.compare("DEC") == 0)
set_month(12);
else
cout<<" Invalid month name. ";
}//End of outer else
}//End of function
//Function to display the next month
void Month::after()
{
//Adds one to month number
monthNumber++;
//Checks if it exceeds 12 then set it to one for January
if(monthNumber > 12)
{
monthNumber = 1;
}//End of if
//Display the month name
output();
}//End of function
//Function to display the previous month
void Month::before()
{
//Subtracts one to month number
monthNumber--;
//Checks if it is zero then set it to 12 for December
if(monthNumber == 0)
{
monthNumber = 12;
}//End of if
//Displays the output
output();
}//End of function
//Function to advance the month by inputed number
void Month::advance()
{
int no, diff;
//Accept the advance number
cout<<" Enter the advance value: ";
cin>>no;
//Adds the advance month number to month number
monthNumber = no + monthNumber;
//If it exceeds 12 then subtract it from 12 and store it in month number
if(monthNumber > 12)
{
monthNumber = monthNumber - 12;
}//End of if
//Displays the output
output();
}//End of function
//Function to display the month name
void Month::output()
{
//Month number is passed in switch to match the case
switch(monthNumber)
{
case 1:
cout<<"January";
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;
}//End of switch
}//End of function
//Function to return the month number
int Month::get_month()
{
return monthNumber;
}//End of function
//Function to set the month number
void Month::set_month(int no)
{
monthNumber = no;
}//End of function
//Function to return the user choice
int menu()
{
//To store user choice
int ch;
//Displays menu
cout<<" 1 Advance ";
cout<<" 2 Before ";
cout<<" 3 After ";
cout<<" 4 Same ";
cout<<" 5 Exit ";
//Accepts user choice
cout<<" Enter your choice: ";
cin>>ch;
//Returns user choice
return ch;
}//End of function
//Main function definition
int main()
{
//Month class object created
Month m;
//Accepts the user month number or name
m.input();
//To store user choice returned by the menu function
int ch;
//Loops till user choice is 5
do
{
//Accept user choice
ch = menu();
//Calls user function based on user choice
switch(ch)
{
case 1:
m.advance();
break;
case 2:
m.before();
break;
case 3:
m.after();
break;
case 4:
m.same();
break;
case 5:
exit(0);
default:
cout<<" Invalid choice. ";
}//End of switch
}while(1);// End of while
return 0;
}//End of main function
Sample Run:
Enter either Month Number / Month Name / First three letter of month: January
1 Advance
2 Before
3 After
4 Same
5 Exit
Enter your choice: 1
Enter the advance value: 3
April
1 Advance
2 Before
3 After
4 Same
5 Exit
Enter your choice: 2
March
1 Advance
2 Before
3 After
4 Same
5 Exit
Enter your choice: 2
February
1 Advance
2 Before
3 After
4 Same
5 Exit
Enter your choice: 3
March
1 Advance
2 Before
3 After
4 Same
5 Exit
Enter your choice: 4
Enter either Month Number / Month Name / First three letter of month: Mar
Same month
1 Advance
2 Before
3 After
4 Same
5 Exit
Enter your choice: 4
Enter either Month Number / Month Name / First three letter of month: 5
Not Same month
1 Advance
2 Before
3 After
4 Same
5 Exit
Enter your choice: 5
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.