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

See image: Design a class called Date. The class should store a date in three in

ID: 3546858 • Letter: S

Question

See image:



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 ( 31) is passed to the class. InvalidMonth - Throw when an invalid month ( 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.

Explanation / Answer

/************ Date.h *************************/

#pragma once


#include <iostream>

using namespace std;


class InvalidDate

{

public:

void printerror() {

cout << "Invalid Day: Should be between 1 and 31" << endl;

}

};


class InvalidMonth

{

public:

void printerror() {

cout << "Invalid Month: Should be between 1 and 12" << endl;

}

};


class Date

{

public:

Date(int day, int month, int year) {

if(day > 0 && day < 32) setDay(day);

else throw InvalidDate();

if(month > 0 && month < 13) setMonth(month);

else throw InvalidMonth();

setYear(year);

}


void print1() { cout << getMonth() << "/" << getDay() << "/" << getYear() % 100 << endl; }

void print2() { cout << convert(getMonth()) << " " << getDay() << "," << getYear() << endl; }

void print3() { cout << getDay() << " " << convert(getMonth()) << " " << getYear() << endl; }


void setDay(int day) { day_ = day; }

void setMonth(int month) { month_ = month; }

void setYear(int year) { year_ = year; }


int getDay() { return day_; }

int getMonth() { return month_; }

int getYear() { return year_; }

~Date() {}


private:

int day_;

int month_;

int year_;


char* convert(int month) {

switch (month) {

case 1: return "January";

case 2: return "February";

case 3: return "March";

case 4: return "April";

case 5: return "May";

case 6: return "June";

case 7: return "July";

case 8: return "August";

case 9: return "September";

case 10: return "October";

case 11: return "November";

case 12: return "December";

default: return NULL;

}

}

};



/**************** main.cpp *******************/

#include <iostream>

#include "Date.h"

using namespace std;


int main()

{

int day, month, year;

cin >> day >> month >> year;

try{

Date date(day, month, year);

date.print1();

date.print2();

date.print3();

} catch (InvalidDate &d) {

d.printerror();

} catch (InvalidMonth &m) {

m.printerror();

}


//system("pause");

return 0;

}