Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

C++ need help with fixing class Month. Define a class named Month that does the following:Uses a private variable of type int to represent a

C++ need help with fixing class Month.

Define a class named Month that does the following:Uses a private variable of type int to represent a month (1-12). This variable should be the only private instance variable for the class. A default constructor that sets the month to 1. A second constructor that sets the month using an integer as an argument. A third constructor that sets the month using a String as an argument, where the string contains the first three letters of the month (e.g. "Jan", "Feb", etc.) The constructor should compare the String value to "Jan", "Feb", etc. and set the month to the corresponding integer value. For example, if "Jan" is passed in then the variable should be set to 1. A method that returns the month as an integer. A method that returns the month as a three letter string. A method that returns the next month as a Month object. This means the return type will have to be of type Month. Make sure that the next month of December is January. Use the Month class in a test program that exercises all of your methods and constructors. Write your class with separate compilation with a separate .h for the header and .cpp file for the implementation. Below is my code for the problem.

class Month {

private:

int month;

public:

Month() {

int month = 1;

}

void setMonthNumber(int x) {

month = x;

}

void setMonthString(string y) {

if (y == "Jan")

month = 1;

else

if (y == "Feb")

month = 2;

else

if (y == "Mar")

month = 3;

else

if (y == "Apr")

month = 4;

else if (y == "May")

month = 5;

else if (y == "Jun")

month = 6;

else if (y == "Jul")

month = 7;

else if (y == "Aug")

month = 8;

else

if (y == "Sep")

month = 9;

else if (y == "Oct")

month = 10;

else if (y == "Nov")

month = 11;

else if (y == "Dec")

month = 12;

else cout << "Invalid Month " << endl;

}

string getMonthString() {

switch (month) {

case 1:

return "Jan";

break;

case 2:

return "Feb";

break;

case 3:

return "Mar";

break;

case 4:

return "Apr";

break;

case 5:

return "May";

break;

case 6:

return "Jun";

break;

case 7:

return "Jul";

break;

case 8:

return "Aug";

break;

case 9:

return "Sep";

break;

case 10:

return "Oct";

break;

case 11:

return "Nov";

break;

case 12:

return "Dec";

break;

}

}

int getMonthNumber() {

return month;

}

int nextMonth() {

if (month = 12)

return month = 1;

else

return month + 1;

}

};

#include

#include "Month.h"

using namespace std;

int main() {

Month m1;

Month m2(3);

Month m3("Apr");

Month m4 = m3.nextMonth();

cout << m1.getMonthNumber() << endl;

cout << m2.getMonthString() << endl;

cout << m3.getMonthNumber() << endl;

cout << m4.getMonthString() << endl;

}

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Modern Database Management

Authors: Jeffrey A. Hoffer Fred R. McFadden

4th Edition

0805360476, 978-0805360479

More Books

Students also viewed these Databases questions

Question

Question Can life insurance be used in a Keogh (HR 10) plan?

Answered: 1 week ago