Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Modify the DayofYear class, written in the earlierprogramming challenging, to ad

ID: 3536679 • Letter: M

Question

Modify the DayofYear class, written in the earlierprogramming challenging, to add a constructor that take twoparameter: a string representing a month and an integer in therange o through 31 representing the day of the month. Theconstructor should then initialize the integer member of the classto represent the day specified by the month and day of monthparameters. The constructor should terminate the program with anappropriate error message if the number enter for a day is outsidethe range of days for the month given.


Add the following overload operators:



++ prefix and postfixincrement operators. These operators should modifythe DayofYear object so that it represent the next day. If the dayis already the end of the year, the new value of the object willrespresent the first day of the year.





-- prefix and postfix decrement operators.These operator should modify the DayofYear object sothat it represent the previous day. If the day is already thefirst day of the year, the new value of the object will representthe last dayof the year.



Here is my code so far:


}

Explanation / Answer

This code has been tested, just run it and check


#include <iostream>

#include <string>

using namespace std;


class DayOfYear

{

private:

int day;


public:

static int daysAtEndOfMonth[];

static string monthName[];

void print();

void setDay(int day){this->day = day;}

DayOfYear operator++(int);

DayOfYear operator--(int);

};


int DayOfYear::daysAtEndOfMonth[] = { 31, 59, 90,

120, 151, 181,

212, 243, 273,

304, 334, 365 };


string DayOfYear::monthName[]= { "January", "February",

"March", "April",

"May", "June",

"July", "August",

"September", "October",

"November", "December" };


//***************************************

// DayOfYear::print. *

// Convert and print day of year *

//***************************************   

void DayOfYear::print()

{

int month = 0;


while (daysAtEndOfMonth[month] < day)

month = (month + 1) % 12;


// DaysAtEndOfMonth >= day

if (month == 0)

cout << " January " << day << endl <<endl;

else

{

cout << endl << monthName[month] << " "

<< day - daysAtEndOfMonth[month-1]

<< endl << endl;

}

}


DayOfYear DayOfYear::operator++(int)


{

DayOfYear doy;

doy.day = day;

day = (day++)%365;

return doy;

}






DayOfYear DayOfYear::operator--(int)


{

DayOfYear doy;

doy.day = day;

day = (day--)%365;

return doy;

}


int main()

{

// Create an instance of the DayOfYear class

DayOfYear dayOfYearObj, x;


int day; // To hold the day


// Display the purpose of the program.

cout << "This program converts a number "

<< "into a string representing the "

<< "month and day. ";


// Get the day as input from the user.   

cout << " Enter a whole number between 1 and 365: ";

cin >> day;


// Set the day.

dayOfYearObj.setDay(day);


// Display the object.

dayOfYearObj.print();

x = dayOfYearObj++;

dayOfYearObj.print();

x--;

x.print();

return 0;

}


Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote