Warning: foreach() argument must be of type array|object, bool given in /var/www/html/web/app/themes/studypress-core-theme/template-parts/header/mobile-offcanvas.php on line 20

The private member function DayofYear: :check_date in Display 10.4 allows some illegal dates to get through, such as February \(30 .\) Redefine the member function DayofYear: :check_date so that it ends the program whenever it finds any illegal date. Allow February to contain 29 days, so you account for leap years. (Hint: This is a bit tedious and the function definition is a bit long, but it is not very difficult.

Short Answer

Expert verified
Answer: The redefined DayofYear::check_date function handles illegal dates by first checking if the given year is a leap year (divisible by 4, not divisible by 100, or divisible by 400). Based on the month, it then checks if the day value is within the valid range for that month, considering leap years for February. If the day is out of the valid range, the function outputs an error message and terminates the program.

Step by step solution

01

Identify rules for validating dates

To validate the date, we need to consider the days range for each month in any year: - January: 1-31 - February: 1-28 (or 1-29 in leap years) - March: 1-31 - April: 1-30 - May: 1-31 - June: 1-30 - July: 1-31 - August: 1-31 - September: 1-30 - October: 1-31 - November: 1-30 - December: 1-31 And be aware of the leap year rule: - Leap year occurs when the year is divisible by 4, not divisible by 100, or divisible by 400.
02

Create the DayofYear::check_date function

Before we continue, make sure you understand the code from Display 10.4, including the class definition and the implementation of check_date function. Now, redefine the DayofYear::check_date function to follow these basic rules: 1. Check if the year is a leap year. If it meets the aforementioned conditions, set a variable flag (e.g., isLeapYear) to true. 2. Based on the month, check if the day value is within the valid range for that month. If the day is out of the range, output an error message and end the program. 3. If the function has not ended the program, it proceeds through the Days without any issue. ```cpp //Definition of member function check_date void DayofYear::check_date() { bool isLeapYear = ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0)); int daysInMonth; if(month == 2) daysInMonth = (isLeapYear) ? 29 : 28; else if(month == 4 || month == 6 || month == 9 || month == 11) daysInMonth = 30; else daysInMonth = 31; if(day <= 0 || day > daysInMonth) { std::cout << "Illegal date! Program will terminate." << std::endl; exit(1); } } ``` Now, the DayofYear::check_date function will correctly handle illegal dates, including the leap years, and terminate the program when it encounters an illegal date.

Unlock Step-by-Step Solutions & Ace Your Exams!

  • Full Textbook Solutions

    Get detailed explanations and key concepts

  • Unlimited Al creation

    Al flashcards, explanations, exams and more...

  • Ads-free access

    To over 500 millions flashcards

  • Money-back guarantee

    We refund you if you fail your exam.

Over 30 million students worldwide already upgrade their learning with Vaia!

One App. One Place for Learning.

All the tools & learning materials you need for study success - in one app.

Get started for free

Most popular questions from this chapter

What is the type of the stream cin? What is the type of the stream cout?

a. How many public: sections are required in a class for the class to be useful? b. How many private: sections are required in a class? c. What kind of section do you have between the opening \(\\{\) and the first public: or private: section label of a class? d. What kind of section do you have between the opening \(\\{\) and the first public: or private: section label of a structure?

Define a function called copy_char that takes one argument that is an input stream. When called, copy_char will read one character of input from the input stream given as its argument and will write that character to the screen. You should be able to call your function using either cin or an input-file stream as the argument to your function copy_char. (If the argument is an input-file stream, then the stream is connected to a file before the function is called, so copy_char will not open or close any files.) For example, the first of the following two calls to copy_char will copy a character from the file stuff. dat to the screen, and the second will copy a character from the keyboard to the screen: ifstream fin; fin.open("stuff.dat") copy_char(fin) copy_char(cin)

How does inheritance support code reuse and make code easier to main\(\operatorname{tain} ?\)

Suppose your program contains the following class definition (along with definitions of the member functions): class Yourclass \\{ public: YourClass(int new_info, char more_new_info); Yourclass(); void do_stuff( \()\) private: int information; char more_information; \\} Which of the following are legal? YourClass an_object(42, 'A'); YourClass another_object; YourClass yet_another_object() \\[ \begin{array}{l} a n_{-} \text {object }=\text { Yourclass }\left(99, \quad^{\prime} B^{\prime}\right) ; \\ a n_{-} \text {object }=\text { Yourclass }() ; \\ \text { an_object }=\text { YourClass } \end{array} \\]

See all solutions

Recommended explanations on Computer Science Textbooks

View all explanations

What do you think about this solution?

We value your feedback to improve our textbook solutions.

Study anywhere. Anytime. Across all devices.

Sign-up for free