Can anyone help me with this C++ problem using a .cpp file with Classes? I am co
ID: 3708565 • Letter: C
Question
Can anyone help me with this C++ problem using a .cpp file with Classes? I am confused.
***It is REQUIRED to use proper headerfiles “.h” and “.cpp”. Show work for both standard file, the header file, and .cpp file. ***
Make one class called Month containing each of these private members:
1.) One string object to store the title of a given month (i.e. January, March, etc.). - Title this object NAME.
2.) Create a single integer variable that will contain the Month Number with valid numbers of 1-12 only (i.e. January would be 1, February would be 2). - Title this object NUMBMONTH
Also, design the member functions listed below:
1.) A single default constructor to start the object NUMBMONTH as a value of 1 and NAME assigned to January.
2.) One constructor that receives the month name as one argument. You must initialize the name toward the value passed as the argument and set the object NUMBMONTH to the correct value.
3.) Another constructor that receives the number of the month as one argument. It should set the object NUMBMONTH to the value passed as the argument and set name to the correct name of the month.
4.) Make sure that both your overloaded constructors tries to validate all info passed toward the function
5.) Create one copy constructor to receive another month object set to an argument. You need to prepare NAME and NUMBMONTH to each of the values held inside the argument object passed through.
6.) Make a set and get functions in order to properly receive NAME and NUMBMONTH member variables.
***Keep in mind that member variables have to be linked logically. If any change happens to NUMBMONTH. The object must automatically update NAME and vice versa.***
The prefix and postfix ++ operator functions to increment NUMBMONTH and initialize NAME to the name of next month. If NUMBMONTH object is equivalent to a value of 12 when functions perform, they must set NUMBMONTH as 1 and NAME as January.
Include both prefix and postfix overloaded functions to decrement NUMBMONTH and initialize NAME as the name of previous month. If NUMBMONTH is equivalent to a value of 1 when functions perform, they must have NUMBMONTH as 12 and NAME as December.
Set all overloaded cout's << operator and cin's >> operator to opperate with your class named Month.
Your << operator must display objects NAME and NUMBMONTH.
Your >> operator needs to ask for user input for both NAME and NUMBMONTH and then hold it inside the object
Finally, demonstrate the class in a program to ensure that each class function operates accordingly.
Explanation / Answer
#include<iostream>
#include<string>
using namespace std;
class Month{
private:
string NAME;
int NUMBMONTH;
public:
Month(){
NAME = "January";
NUMBMONTH = 1;
}
Month(string nm, int n){
NAME = nm;
NUMBMONTH = n;
}
Month (const Month &a){
NAME = a.NAME;
NUMBMONTH = a.NUMBMONTH;
}
string getName(){
return NAME;
}
int getNumber(){
return NUMBMONTH;
}
void setName(string a){
NAME = a;
if (a == "January")
NUMBMONTH = 1;
if (a == "February")
NUMBMONTH = 2;
if (a == "March")
NUMBMONTH = 3;
if (a == "April")
NUMBMONTH = 4;
if (a == "May")
NUMBMONTH = 5;
if (a == "June")
NUMBMONTH = 6;
if (a == "July")
NUMBMONTH = 7;
if (a == "August")
NUMBMONTH = 8;
if (a == "Spetember")
NUMBMONTH = 9;
if (a == "October")
NUMBMONTH = 10;
if (a == "November")
NUMBMONTH = 11;
if (a == "December")
NUMBMONTH = 12;
}
void setNumber(int a){
NUMBMONTH = a;
switch(a){
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;
}
}
friend ostream & operator << (ostream &out, const Month &c);
friend istream & operator >> (istream &in, Month &c);
void operator ++(){
++NUMBMONTH;
}
void operator ++(int){
NUMBMONTH++;
}
};
ostream & operator << (ostream &out, const Month &c)
{
out << c.NAME << " " << c.NUMBMONTH;
return out;
}
istream & operator >> (istream &in, Month &c)
{
string nm;
int n;
cout << "Enter name ";
in >> nm;
c.setName(nm);
cout << "Enter number ";
in >> n;
c.setNumber(n);
return in;
}
int main(){
Month m1;
Month m2;
cin >> m1;
cin >> m2;
cout << m1 << endl;
cout << m2 << endl;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.