Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Modify the Date class you wrote for Programming Challenge 1 of Chapter 13. The class should implement the following exeption classes: invalidDay Throw when an

Modify the Date class you wrote for Programming Challenge 1 of Chapter 13. The class should implement the following exeption classes:

invalidDay Throw when an invalid day (<1 or> 31) is passed to the class.

invalidMonth Throw when an invalid month (<1 or>12) is passed to the class.

// Chapter 13, Programming Challenge 1: Date Class #include #include using namespace std;

// Constants const int NUM_MONTHS = 12;

// Declaration of the Date class

class Date { private: int month; // To hold the month int day; // To hold the day int year; // To hold the year // An array of char arrays to hold // the names of the months string names[NUM_MONTHS]; // Private member function to assign // the month names to the names array void setNames(); public: // Constructors Date(); Date(int, int, int); // Mutators void setMonth(int m); void setDay(int d); void setYear(int y);

// Functions to print the date void showDate1(); void showDate2(); void showDate3(); };

//********************************** // Default constructor * //**********************************

Date::Date() { setNames(); }

//********************************** // Overloaded constructor * // Parameters: m is the month * // d is the day * // y is the year * //**********************************

Date::Date(int m, int d, int y) { setMonth(m); setDay(d); setYear(y); setNames(); }

//********************************** // Member function setNames * // This function assigns the names * // of the months to the names * // array. * //**********************************

void Date::setNames() { names[0] = "January"; names[1] = "Febraury"; names[2] = "March"; names[3] = "April"; names[4] = "May"; names[5] = "June"; names[6] = "July"; names[7] = "August"; names[8] = "September"; names[9] = "October"; names[10] = "November"; names[11] = "December"; }

//********************************** // Member function setMonth * //**********************************

void Date::setMonth(int m) { if (m >= 1 && m <= 12) month = m; else { cout << m << " is not a valid " << "value for the month. "; exit (EXIT_FAILURE); } }

//********************************** // Member function setDay * //**********************************

void Date::setDay(int d) { if (d >= 1 && d <= 31) day = d; else { cout << d << " is not a valid " << "value for the day. "; exit(EXIT_FAILURE); } }

//********************************** // Member function setYear * //**********************************

void Date::setYear(int y) { year = y; }

//********************************** // Member function showDate1 * // Displays the date in the form * // MM/DD/YY * // Example: 12/25/2014 * //**********************************

void Date::showDate1() { cout << month << "/" << day << "/" << year << endl; }

//************************************* // Member function showDate2 * // Displays the date in the following * // form: December 25, 2014 * //*************************************

void Date::showDate2() { cout << names[month+1] << " " << day << ", " << year << endl; }

//************************************* // Member function showDate3 * // Displays the date in the following * // form: 25 December, 2014 * //*************************************

void Date::showDate3() { cout << day << " " << names[month+1] << " " << year << endl; }

//************************************* // Function main * //*************************************

int main() { // Create a Date object and initialize it // using the overloaded constructor. Date today(12, 25, 2014);

// Show the date in form #1. today.showDate1(); // Store a new month, day, and year // in the object. today.setMonth(6); today.setDay(16); today.setYear(2012); // Show the date in form #2. today.showDate2(); // Show the date in form #3. today.showDate3(); return 0; }

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Advances In Databases And Information Systems 23rd European Conference Adbis 2019 Bled Slovenia September 8 11 2019 Proceedings Lncs 11695

Authors: Tatjana Welzer ,Johann Eder ,Vili Podgorelec ,Aida Kamisalic Latific

1st Edition

3030287297, 978-3030287290

More Books

Students also viewed these Databases questions

Question

7. Discuss the advantages of embedded learning.

Answered: 1 week ago