Modify the DayOfYear class, to add a constructor that takes two parameters: a string object representing a month and an integer in the range 0 through 31 representing the day of the month. The constructor should then initialize the integer member of the class to represent the day specified by the month and day of month parameters. The constructor should terminate the program with an appropriate error message if the number entered for a day is outside the range of days for the month given. Add the following overload operators: ++ prefix and postfix increment operators. These operators should modify the DayOf Year object so that it represents the next day. If the day is already the end of the year, the new value of the object will represent the first day of the year. -- prefix and postfix decrement operators. These operators should modify the DayOf Year object so that it represents the previous day. If the day is already the first day of the year, the new value of the object will represent the last day of the year.

C++ Programming: From Problem Analysis to Program Design
8th Edition
ISBN:9781337102087
Author:D. S. Malik
Publisher:D. S. Malik
Chapter11: Inheritance And Composition
Section: Chapter Questions
Problem 9PE
icon
Related questions
Question

C++ compile a program to answer question in picture included.

Code to edit:

 

#include <iostream>

#include <string>

class DayOfYear
{

private:
  mutable int day; // Make 'day' mutable

  // Static arrays to hold month names and the number of days in each month

  static const std::string monthNames[];

  static const int daysInMonth[];

public:
  // Constructor

  DayOfYear (int day) : day (day) {}

  // Member function to print the day in month-day format

  void
  print () const
  {

    int tempDay = day; // Create a temporary variable

    int month = 0;

    // Find the month for the given day

    while (tempDay > daysInMonth[month])
      {

        tempDay -= daysInMonth[month];

        month++;
      }

    // Print the result

    std::cout << monthNames[month] << " " << tempDay << std::endl;
  }
};

// Static member variable initialization

const std::string DayOfYear::monthNames[] = {

  "January", "February", "March",     "April",   "May",      "June",

  "July",    "August",   "September", "October", "November", "December"

};

const int DayOfYear::daysInMonth[] = {

  31, 28, 31, 30, 31, 30,

  31, 31, 30, 31, 30, 31

};

int
main ()
{

  int userDay;

  // Get user input for the day of the year

  std::cout << "Enter the day of the year: ";

  std::cin >> userDay;

  // Validate user input

  if (userDay < 1 || userDay > 365)
    {

      std::cerr << "Invalid input. Day of the year should be between 1 and 365."
                << std::endl;

      return 1;
    }

  // Create an instance of the DayOfYear class with user input

  DayOfYear userDayOfYear (userDay);

  // Print the representation in month-day format

  std::cout << "Day " << userDay << ": ";

  userDayOfYear.print ();

  return 0;
}

Modify the DayOfYear class, to add a constructor that takes two parameters: a string
object representing a month and an integer in the range 0 through 31 representing
the day of the month. The constructor should then initialize the integer member of the
class to represent the day specified by the month and day of month parameters. The
constructor should terminate the program with an appropriate error message if the
number entered for a day is outside the range of days for the month given.
Add the following overload operators:
++ prefix and postfix increment operators. These operators should modify the
DayOfYear object so that it represents the next day. If the day is already the end of
the year, the new value of the object will represent the first day of the year.
prefix and postfix decrement operators. These operators should modify the
DayOfYear object so that it represents the previous day. If the day is already the first
day of the year, the new value of the object will represent the last day of the year.
Transcribed Image Text:Modify the DayOfYear class, to add a constructor that takes two parameters: a string object representing a month and an integer in the range 0 through 31 representing the day of the month. The constructor should then initialize the integer member of the class to represent the day specified by the month and day of month parameters. The constructor should terminate the program with an appropriate error message if the number entered for a day is outside the range of days for the month given. Add the following overload operators: ++ prefix and postfix increment operators. These operators should modify the DayOfYear object so that it represents the next day. If the day is already the end of the year, the new value of the object will represent the first day of the year. prefix and postfix decrement operators. These operators should modify the DayOfYear object so that it represents the previous day. If the day is already the first day of the year, the new value of the object will represent the last day of the year.
Expert Solution
steps

Step by step

Solved in 4 steps with 7 images

Blurred answer
Knowledge Booster
Developing computer interface
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
  • SEE MORE QUESTIONS
Recommended textbooks for you
C++ Programming: From Problem Analysis to Program…
C++ Programming: From Problem Analysis to Program…
Computer Science
ISBN:
9781337102087
Author:
D. S. Malik
Publisher:
Cengage Learning
EBK JAVA PROGRAMMING
EBK JAVA PROGRAMMING
Computer Science
ISBN:
9781337671385
Author:
FARRELL
Publisher:
CENGAGE LEARNING - CONSIGNMENT
Programming Logic & Design Comprehensive
Programming Logic & Design Comprehensive
Computer Science
ISBN:
9781337669405
Author:
FARRELL
Publisher:
Cengage