C++. Design a class named Month. The class should have the following private mem
ID: 3802633 • Letter: C
Question
C++. Design a class named Month. 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. In addition, provide the following member functions:
1. A default constructor that sets monthNumber to 1 and name to January.
2. 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.
3. 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.
4. Appropriate set and get functions for the name and monthNumber member variables.
5. 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.
6. Overload the = operator. It should set both the monthName and monthNumber variables.
7. Overload cout s << operator and cin s >> operator to work with the Month class. Demonstrate the class in a program. o << cout s stream insertion operator. This operator should cause the date to be displayed in the form shown in the sample output below. o >> cin s stream extraction operator. This operator should prompt the user for the name of a month. In the same operation, it should also set the monthName and monthNumber variables.
https://github.com/selango2017/Months/tree/master
I'm getting errors in the Months.cpp file in the setMonthName method as well as the cout << lines in the main file.
Any help is appreciated. Thank you
Explanation / Answer
Here is the code for Month.h:
#pragma once
#ifndef MONTH_H
#define MONTH_H
#include <iostream>
#include <fstream>
#include <iomanip>
#include <string>
using namespace std;
class Month
{
private:
//Name: A string object that holds the name of a month, such as January, February, etc.
string monthName;
//monthNumber: An integer variable that holds the number of the month.
int monthNumber; // monthNumber will accept from 1 to 12
public:
//1. A default constructor that sets monthNumber to 1 and name to January.
Month()
{
monthName = "January";
monthNumber = 1;
}
//2. 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.
Month(string name)
{
monthName = name;
setMonthNumber();
}
//3. 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.
Month(const int num)
{
monthNumber = num;
setMonthName();
}
//4. Appropriate set and get functions for the name and monthNumber member variables.
const string getMonthName()
{
return monthName;
}
const int getMonthNumber()
{
return monthNumber;
}
void setMonthName(const string name)
{
monthName = name;
setMonthNumber();
}
void setMonthNumber(const int num)
{
monthNumber = num;
setMonthName();
}
void setMonthName();
void setMonthNumber();
Month operator++();
Month operator++(int);
Month operator--();
Month operator--(int);
friend ostream &operator <<(ostream &strm, const Month &obj);
friend istream &operator >> (istream &strm, Month &monthObj);
};
#endif
Here is the code for Month.cpp:
#include "Months.h"
#include<iostream>
#include <string>
using namespace std;
void Month::setMonthNumber()
{
string name = monthName;
if (name == "January" || name == "Jan")
monthNumber = 1;
else if (name == "February" || name == "Feb")
{
monthNumber = 2;
}
else if (name == "March" || name == "Mar")
{
monthNumber = 3;
}
else if (name == "April" || name == "Apr")
{
monthNumber = 4;
}
else if (name == "May")
{
monthNumber = 5;
}
else if (name == "June" || name == "Jun")
{
monthNumber = 6;
}
else if (name == "July" || name == "Jul")
{
monthNumber = 7;
}
else if (name == "August" || name == "Aug")
{
monthNumber = 8;
}
else if (name == "September" || name == "Sept")
{
monthNumber = 9;
}
else if (name == "October" || name == "Oct")
{
monthNumber = 10;
}
else if (name == "November" || name == "Nov")
{
monthNumber = 11;
}
else if (name == "December" || name == "Dec")
{
monthNumber = 12;
}
else
{
monthNumber = 1;
}
}
void Month:: setMonthName()
{
int num = monthNumber;
if (num < 1 || num > 12)
monthNumber = 1;
else
monthNumber = num;
if (monthNumber == 1)
monthName = "January";
else if (monthNumber == 2)
monthName = "February";
else if (monthNumber == 3)
monthName = "March";
else if (monthNumber == 4)
monthName = "April";
else if (monthNumber == 5)
monthName = "May";
else if (monthNumber == 6)
monthName = "June";
else if (monthNumber == 7)
monthName = "July";
else if (monthNumber == 8)
monthName = "August";
else if (monthNumber == 9)
monthName = "September";
else if (monthNumber == 10)
monthName = "October";
else if (monthNumber == 11)
monthName = "November";
else
monthName = "December";
}
Month Month::operator++()
{
if (monthNumber == 12)
monthNumber = 1;
else
++monthNumber;
setMonthNumber(monthNumber);
return *this;
}
Month Month::operator++(int)
{
Month temp(monthNumber);
setMonthNumber(++monthNumber);
return temp;
}
Month Month::operator--()
{
if (monthNumber == 1)
monthNumber = 12;
else --monthNumber;
setMonthNumber(monthNumber);
return *this;
}
Month Month::operator--(int)
{
Month temp(monthNumber);
setMonthNumber(--monthNumber);
return temp;
}
ostream &operator << (ostream &strm, const Month &obj)
{
return strm << obj.monthName << "(" << obj.monthNumber << ")";
}
istream &operator >> (istream &strm, Month &monthObj)
{
string m_Name;
cout << endl << "Please enter the month name: " << endl;
strm >> m_Name;
monthObj.setMonthName(m_Name);
return strm;
}
And the code for Demo.cpp:
//#include "myString.cpp"
#include "Months.cpp"
#include<iostream>
using namespace std;
/*ostream & operator<<(ostream & O, Month a) {
O << a.monthNumber << " " << a.monthName;
return O;
}
istream &operator >> (istream &is, Month &a) //cin operator overload
{
int num;
char name[12];
is >> num;
is >> name;
a.SetMonthNumber(num);
a.SetMonthName(name);
return is;
}*/
int main()
{
Month m1("February");
//cout << m1.getMonthName() << " " << m1.getMonthNumber() << endl;
Month m2, m3, m4;
cout << m1.getMonthName() << endl;
cout << m1.getMonthNumber() << endl;
cout << endl;
cout << m1;
cout << endl;
m2 = ++m1;
cout << m1;
cout << m2;
cout << endl;
m3 = m1++;
cout << m1;
cout << m3;
cout << endl;
cout << "Enter the name of a month: ";
cin >> m4;
cout << m4;
//system ("Pause");
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.