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

Suppose your program contains the following class definition: class Automobile \\{ public: void set_price(double new_price) void set_profit(double new_profit) double get_price(); private: double price double profit; double get_profit(); \\}; and suppose the main part of your program contains the following declaration and that the program somehow sets the values of all the member variables to some values: Automobile hyundai, jaguar; Which of the following statements are then allowed in the main part of your program? hyundai.price \(=4999.99\) jaguar.set_price(30000.97) double a_price, a_profit; \(a_{-}\) price \(=\) jaguar \(.\) get \(_{-}\) price () \(a_{-}\) profit \(=\) jaguar \(\cdot\) get \(_{-}\) profit () \(a_{-}\) profit \(=\) hyundai \(.\) get_profit () if (hyundai \(==\) jaguar) cout \( < < \) "Want to swap cars?"; hyundai = jaguar;

Write a definition for a structure type for records consisting of a person's wage rate, accrued vacation (which is some whole number of days), and status (which is either hourly or salaried). Represent the status as one of the two char values 'H' and 'S'. Call the type EmployeeRecord.

Consider the following type definition: struct ShoeType \\{ char style double price \\}; Given this structure type definition, what will be the output produced by the following code? ShoeType shoe1, shoe2; shoe1.style \(=^{\prime} A^{\prime}\) shoe1.price \(=9.99\) cout \( < < \) shoe \(1 .\) style \( < < " \$^{\prime \prime} < < \) shoe1.price \( < < \) endl shoe \(2=\) shoe 1 shoe2.price \(=\) shoe \(2 .\) price \(/ 9\) cout \( < < \) shoe \(2 .\) style \( < < " \$ " < < \) shoe \(2 .\) price \( < < \) end 1;

Given the following class definition, write an appropriate definition for the member function set: class Temperature \\{ public: void set(double new_degrees, char new_scale); //Sets the member variables to the values given as / / arguments. double degrees; char scale; //'F' for Fahrenheit or 'C' for Celsius. \\};

When you define an ADT as a C++ class, should you make the member variables public or private? Should you make the member functions public or private?

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