Program Requirements Modify the class Month from homework #6 (If you wish, you c
ID: 3630954 • Letter: P
Question
Program RequirementsModify the class Month from homework #6 (If you wish, you can use the partial solution posted in Georgia View). 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.
• rainFall A double variable that holds the amount of rainfall for that month.
In addition, provide the following public 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, monthNumber and rainFall member variables.
In the main function create the following:
• rain An array of integers of size 3. Initialize the array with data of your choice (do not prompt the user, or use cin).
• create an instance of Month, for the first three months of the year. You may use an array, if you wish.
Then, set the rain fall data for each month instance using data from the array rain.
Display the rainfall for each month using the getName and getRainFall function of each month instance.
Explanation / Answer
#include<iostream>
#include<string>
using namespace std;
class Month
{
private:
string name[12]={"January","February","March","April","May","June","Jully","Augest","September","October", "November","December"};
int monthNumber[12]={1,2,3,4,5,6,7,8,9,10,11,12};
double rainFall[12] = {10.11,12.10,14.21,6.5,3.4,1.1,2.3,19.0,34.5,23.5,22.2,11.2};
public:
Month
{ name = "Janaury";
monthNumber = 1;
}
Month(char *monthnum)
{
name = monthnum;
if (monthnum == "January")
monthNumber = 1;
else if(monthnum == "February")
monthNumber = 2;
else if(monthnum == "March")
monthNumber = 3;
else if(monthnum == "April")
monthNumber = 4;
else if(monthnum == "May")
monthNumber = 5;
else if(monthnum == "June")
monthNumber = 6;
else if(monthnum == "July")
monthNumber = 7;
else if(monthnum == "August")
monthNumber = 8;
else if(monthnum == "September")
monthNumber = 9;
else if(monthnum == "October")
monthNumber = 10;
else if(monthnum == "November")
monthNumber = 11;
else if(monthnum == "December")
monthNumber = 12;
}
Month(int n)
{
monthNumber = n;
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 = "August";
break;
case 9:
name = "September";
break;
case 10:
name = "October";
break;
case 11:
name = "November";
break;
case 12:
name = "December";
break;
}
void setName(char *monthnum)
{ name = monthnum;}
void setmonthNumber(int x)
{ monthNumber = x;}
void setrainFall(double y)
{ rainFall = y;}
void getName()
{ return name;}
int getmonthNumber()
{ return monthNumber;}
void getrainFall()
{ return rainFall;}
};
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.