Design a class called Date. The class should store a date in three integers: mon
ID: 3535508 • Letter: D
Question
Design a class called Date. The class should store a date in three integers: month, day, and year.
Create a member function to display the date in the following format: December 25, 2005
Create both a default constructor and an overloaded constructor. The default one should initialize the date to January 1, 2000, and the overloaded one should take arguments for the month, day, and year.
Create accessors and mutators (gets and sets) for the member variables.
Overload the prefix and postfix ++ operators to increment the date. When a date is set to the last day of the month and incremented, it should become the first day of the following month. If the date is December 31, the incremented date should become January 1 of the following year.
Overload the prefix and postfix -- operators to decrement the date. When a date is set to the first day of the month, and decremented, it should become the last day of the previous month. If the date is January 1, the decremented date should become December 31 of the previous year.
Overload the stream insertion operator (<<) to display the date in the format: December 25, 2005.
Overload the stream extraction operator (>>) to prompt the user for a date to be stored.
Design a class called FutureDate that is derived from the Date class you have created. This class should not allow the user to store a date earlier than today's date. If the user enters a date earlier than today, display a message and let the user re-enter the date.
Create only one constructor, an overloaded constructor that accepts values for the month, day, and year, in that order, and initializes the object to that date. The constructor should prompt the user if the date is before today's date.
The decrement operators (prefix and postfix) should be overridden. Do not allow the date to be decremented so it is before today's date. Just keep the current date if decrementing it would put it in the past.
The following code snippet displays today's date.
Demonstrate the classes by using the chapter13_15.cpp driver program, located under content in Florida Online. This file should not need to be modified except where commented. If your classes don't work with the driver, modify your classes to work with it.
Input Validation: Do not accept values for the day greater than the number of days in the current month, or less than 1. Do not accept values for the month outside of the values 1-12.
Explanation / Answer
You may proceed as given below:
#include <ctime>
#include <iostream>
using namespace std;
class Date
{
int month;
int day;
int year;
public:
void display()
{
switch(month)
{
case 1:
cout<<"January";
break;
case 2:
cout<<"Febrary";
break;
case 3:
cout<<"March";
break;
case 4:
cout<<"April";
break;
case 5:
cout<<"May";
break;
case 6:
cout<<"June";
break;
case 7:
cout<<"July";
break;
case 8:
cout<<"August";
break;
case 9:
cout<<"September";
break;
case 10:
cout<<"October";
break;
case 11:
cout<<"November";
break;
case 12:
cout<<"December";
break;
default: break;
}
cout<<" "<<day<<", "<<year;
}
Date()
{
day = 1;
month = 1;
year = 2000;
}
Date(int mn, int dy, int yr)
{
day = dy;
month = mn;
year = yr;
}
void setDay(int dy)
{ day = dy;
}
int getDay()
{ return day;
}
void setMonth(int mn)
{ month = mn;
}
int getMonth()
{ return month;
}
void setYear(int yr)
{ year = yr;
}
int getYear()
{ return year;
}
Date& operator++()
{
if(month == 4 || month == 6 || month == 9 || month == 11)
{
if(day == 30)
{
month++;
day = 1;
}
else
day++;
}
else if(month == 2)
{
if(day == 28)
{
month++;
day = 1;
}
else
day++;
}
else
{
if(day == 31 && month!=12)
{
month++;
day = 1;
}
else if(day == 31 && month==12)
{
month = 1;
day = 1;
year++;
}
else
day++;
}
return *this;
}
Date operator++(int unused)
{
Date d;
d = *this;
if(month == 4 || month == 6 || month == 9 || month == 11)
{
if(day == 30)
{
month++;
day = 1;
}
else
day++;
}
else if(month == 2)
{
if(day == 28)
{
month++;
day = 1;
}
else
day++;
}
else
{
if(day == 31 && month!=12)
{
month++;
day = 1;
}
else if(day == 31 && month==12)
{
month = 1;
day = 1;
year++;
}
else
day++;
}
return d;
}
};
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.