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

Tasks: Write a program that defines a class Date and implement t as required. Th

ID: 3590344 • Letter: T

Question

Tasks: Write a program that defines a class Date and implement t as required. The class Date should consist of three private member variables: year of type int, month of type int, and day of type int The class Date should also include the following public member functions: . print to output the year, month, and day in the format of MM-DD-YYYY 2. getYear to return the year 3. getMonth to return the month. 4. getDay to return the day 5. setDate to set the year, month and day according to the parameters. Data validation should be provided. 1) A valid year should be between 1900 and 2017, inclusive, otherwise using 2001 for the year 2) A valid month should be between 1 and 12, inclusive, otherwise using I for the month. ) A valid day should be between 1 and 28 when the month is 2, i.e., simply assume there are 28 days in February 1 and 31 when the month is 1, 3, 5,7, 8, 10, or 12, (which means there are 31 days in January, March, May, July, August, October, and December.) l and 30 when the month is 4, 6.9, or 1 Use 1 for the day if the value is invalid. 6. A constructor to initialize year, month, and day with default parameters. The default value of year, month, and day is 2001, 1, and1, respectively. (See Classes and Objects slides page 29 for constructor with default parameters.) 7. A copy constructor to initialize year, month, and day using a Date object. 8. equals to compare two Date objects' values. Return true if they are the same, otherwise return false. The equals function has a formal parameter which is a Date object.

Explanation / Answer

#include <iostream>

#include <iomanip>

#include <string>

using namespace std;

class Date

{

private:

int month, day, year;

public:

Date() //default constructor

{

month = 1, day = 1, year = 2001;

}

Date(int month, int day, int year) //parameterized constructor

{

this->month = month;

this->day = day;

this->year = year;

}

  

Date(const Date &d) //copy constructor

{

this->month = d.month;

this->day = d.day;

this->year = d.year;

}

  

int getYear()

{

return year;

}

  

int getMonth()

{

return month;

}

int getDay()

{

return day;

}

void setDate(int day,int month,int year) //set date with validation

{

if(month == 2 && day < 1 || day > 28) //february

cout << "The day is invalid" << endl;

else if (day < 1 && day > 31)

cout << "The day is invalid" << endl;

else

this->day = day;

  

  

if (month < 1 && month > 12)

cout << "The month is invalid" << endl;

else

this->month = month;

if(year >=1900 && year <= 2017)

this->year = year;

else

this->year = 2001;

}

void print()

{

cout << setfill('0')<<setw(2);

cout<<month << "-" ;

cout << setfill('0')<<setw(2);

cout<<day << "-";

cout<<setw(4)<< year << endl;

}

bool equals(Date d)

{

if(this->day == d.day && this->month == d.month && this->year == d.year)

return true;

else

return false;

}

};

int main()

{

int Month, Day, Year;

cout << "Please enter a month (between 1 - 12) " << endl;

cin >> Month;

cout << "Please enter a day (between 1 - 31) " << endl;

cin >> Day;

cout << "Please enter a year " << endl;

cin >> Year;

Date date1(Month, Day, Year);

date1.print();

  

Date date2(date1); //copy constructor used

  

if(date1.equals(date2))

cout<<" Both dates are same";

else

cout<<" The two dates are different";

return 0;

}

Output:

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