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
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.
Header Files: DayOfYear.h
*******************************************************************
#include
class DayOfYear { private: int day; string months[12]; int endOfMonth[13];
static string dayMonth; static int restDays;
public: DayOfYear(int d); void print(); void setEndOfMonth(); void setMonthName();
};
Main.cpp
*******************************************************************
#include
using namespace std;
int main() { int dayNum; cout << "Please enter the day number in a year: " << endl; cin >> dayNum;
DayOfYear d(dayNum); d.setEndOfMonth(); d.setMonthName(); d.print();
system("pause"); return 0; }
DayOfYear.cpp
**************************************************************
#include
using namespace std;
//Initialize of static variables string DayOfYear::dayMonth = ""; int DayOfYear::restDays = 0;
// constructor DayOfYear::DayOfYear(int d) { day = d; }
// Create a function to set the end of each month in a year void DayOfYear::setEndOfMonth() { endOfMonth[0] = 0; endOfMonth[1] = 31; endOfMonth[2] = 59; endOfMonth[3] = 90; endOfMonth[4] = 120; endOfMonth[5] = 151; endOfMonth[6] = 181; endOfMonth[7] = 212; endOfMonth[8] = 243; endOfMonth[9] = 273; endOfMonth[10] = 304; endOfMonth[11] = 334; endOfMonth[12] = 365; }
// Create a function to name each month in a year void DayOfYear::setMonthName() { months[0] = "January"; months[1] = "February"; months[2] = "March"; months[3] = "April"; months[4] = "May"; months[5] = "June"; months[6] = "July"; months[7] = "August"; months[8] = "September"; months[9] = "October"; months[10] = "November"; months[11] = "December"; }
void DayOfYear::print() { int month = 0; while (endOfMonth[month] dayMonth += months[month -1 ]; restDays += day- endOfMonth[month - 1]; cout << "The corresponding day is: " << dayMonth << " " << restDays << endl; }
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
