Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

2. Exception Handling Design a class called Date. The class should store a date

ID: 3567405 • Letter: 2

Question

2. Exception Handling

Design a class called Date. The class should store a date in three integers: month, day, and year. Include the relevant get/set methods. There should be separate member functions to print the date in the following forms:
12/25/13

December 25, 2013 25 December 2013

The class should implement the following exceptions 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.

Demonstrate the working of this class in the main function. Make sure to ask the user for appropriate data and test the exception cases.

Filename: date.cpp

Explanation / Answer

#include <iostream>
#include <stdexcept>
#include <stdlib.h>
using namespace std;
class InvalidDate: public exception {
public:
virtual const char* what() const throw() {return ("Invalid date.");};
};
class InvalidDay: public InvalidDate {
public:
virtual const char* what() const throw() {return "Invalid day. Expected a value between [1,31].";};
};
class InvalidMonth: public InvalidDate {
public:
virtual const char* what() const throw() {return "Invalid month. Expected a value between [1,12].";}
};

class Date {
public:
Date();
Date(const int &month, const int &day, const int &year) throw(InvalidDate);
int getMonth() const {return this->month;};
int getDay() const {return this->day;};
int getYear() const {return this->year;};
void setMonth(const int &month) throw(InvalidMonth);
void setDay(const int &day) throw(InvalidDay);
void setYear(const int &year);
void printFormatA() const;
void printFormatB() const;
void printFormatC() const;
private:
int month;
int day;
int year;
const static string monthNames[12];
};
const string Date::monthNames[12] = {"January",
"February",
"March",
"April",
"May",
"June",
"July",
"August",
"September",
"October",
"November",
"December"};
Date::Date():
month(1),
day(1),
year(1)
{}
Date::Date(const int &month, const int &day, const int &year) throw(InvalidDate) {
setMonth(month);
setDay(day);
setYear(year);
}
void Date::setMonth(const int &month) throw(InvalidMonth) {
if (month < 1 || month > 12)
throw InvalidMonth();
else
this->month = month;
}
void Date::setDay(const int &day) throw(InvalidDay) {
if (day < 1 || day > 31)
throw InvalidDay();
else
this->day = day;
}
void Date::setYear(const int &year) {
this->year = year;
}
void Date::printFormatA() const {
cout << this->month << "/" << this->day << "/" << this->year << endl;
}
void Date::printFormatB() const {
cout << this->monthNames[this->month - 1] << " " << this->day << ", " << this->year << endl;
}
void Date::printFormatC() const {
cout << this->day << " " << this->monthNames[this->month -1] << " " << this->year << endl;
}
string getString(string message);
int getNum(string message);
string getString(string message) {
string userInput;
cout << message;
getline(cin, userInput);
return userInput;
}
int getNum(string message) {
return strtol(getString(message).c_str(), NULL, 0);
}
int main() {
bool success = false;
Date date;
while (!success) {
try {
date.setMonth(getNum("Enter the month number: "));
date.setDay(getNum("Enter the day number: "));
date.setYear(getNum("Enter the year: "));
success = true;
}
catch (InvalidDate &e) {
cout << "Caught: " << e.what() << endl;
}
}
date.printFormatA();
date.printFormatB();
date.printFormatC();
return 0;
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote