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

Date Class Modification Modify the Date class in Programming Challenge 1 of Chap

ID: 3778031 • Letter: D

Question

Date Class Modification


Modify the Date class in Programming Challenge 1 of Chapter 13. The new version

should have the following overloaded operators:
++ Prefix and postfix increment operators. These operators should increment the

object's day member.
Prefix and postfix decrement operators. These operators should decrement the

object's day member.

Subtraction operator. If one Date object is subtracted from another, the operator should give the number of days between the two dates. For example, if April 10, 2014 is subtracted from April 18, 2014, the result will be 8.

<< cout’s stream insertion operator. This operator should cause the date to be dis- played in the form

April 18, 2014
>> cin’s stream extraction operator. This operator should prompt the user for a date

to be stored in a Date object.

The class should detect the following conditions and handle them accordingly:

When a date is set to the last day of the month and incremented, it should become the first day of the following month.

When a date is set to December 31 and incremented, it should become January 1 of the following year.

When a day is set to the first day of the month and decremented, it should become the last day of the previous month.

When a date is set to January 1 and decremented, it should become December 31 of the previous year.

Demonstrate the class’s capabilities in a simple program.

Input Validation: The overloaded >> operator should not accept invalid dates. For example, the date 13/45/2014 should not be accepted.

Explanation / Answer

#include<iostream>
#include<string.h>
using namespace std;
//Date class defined
class MyDate
{
//dt for Date
int dt, month, year;
public:
//Default constructor
MyDate()
{
dt = month = year = 0;
}
//Prefix Increment
void operator ++ ()
{
//Checks for December 31 then date is 1 month is January and year is next year
if(dt == 31 && month == 12)
{
dt = 1; month = 1; year++;
}
else
{
//Checks if date is 31 then date is changed to 1 and month is incremented
if((month == 1 || month == 3 || month == 5 || month == 7 || month == 8 || month == 10 || month == 12) && dt == 31)
{
dt = 1; month++;
}
//Checks if date is 30 then date is changed to 1 and month is incremented
else if((month == 4 || month == 6 || month == 9 || month == 11) && dt == 30)
{
dt = 1; month++;
}
//Checks if the month is February
else if(month == 2 && dt == 28)
{
dt = 1; month++;
}
//Otherwise date is incremented
else
dt++;
}
}
//Postfix Increment
void operator ++ (int)
{
//Checks for December 31 then date is 1 month is January and year is next year
if(dt == 31 && month == 12)
{
dt = 1; month = 1; year++;
}
else
{
//Checks if date is 31 then date is changed to 1 and month is incremented
if((month == 1 || month == 3 || month == 5 || month == 7 || month == 8 || month == 10 || month == 12) && dt == 31)
{
dt = 1; month++;
}
//Checks if date is 30 then date is changed to 1 and month is incremented
else if((month == 4 || month == 6 || month == 9 || month == 11) && dt == 30)
{
dt = 1; month++;
}
//Checks if the month is February
else if(month == 2 && dt == 28)
{
dt = 1; month++;
}
//Checks if the month is February
else
dt++;
}
}
//Prefix Decrement
void operator -- ()
{
//IF month is January and date is one Date is set to 31 and month is December and year is decremented
if(dt == 1 && month == 1)
{
dt = 31; month = 12; year--;
}
else
{
//If date is 1 on the specified month then date is set to 30 and month is decremented to previous month
if((month == 2 || month == 5 || month == 7 || month == 8 || month == 10 || month == 12) && dt == 1)
{
dt = 30; month--;
}
//If date is 1 on the specified month then date is set to 31 and month is decremented to previous month
else if((month == 4 || month == 6 || month == 9 || month == 11) && dt == 1)
{
dt = 31; month--;
}
//If month is march and date is 1 then set date to 29 and month is decremented to previous month
else if(month == 3 && dt == 1)
{
dt = 29; month--;
}
//Otherwise date is decremented
else
dt--;
}
}
//Postfix Decrement
void operator -- (int)
{
//IF month is January and date is one Date is set to 31 and month is December and year is decremented
if(dt == 1 && month == 1)
{
dt = 31; month = 12; year--;
}
else
{
//If date is 1 on the specified month then date is set to 30 and month is decremented to previous month
if((month == 2 || month == 5 || month == 7 || month == 8 || month == 10 || month == 12) && dt == 1)
{
dt = 30; month--;
}
//If date is 1 on the specified month then date is set to 31 and month is decremented to previous month
else if((month == 4 || month == 6 || month == 9 || month == 11) && dt == 1)
{
dt = 31; month--;
}
//If month is march and date is 1 then set date to 29 and month is decremented to previous month
else if(month == 3 && dt == 1)
{
dt = 29; month--;
}
//Otherwise date is decremented
else
dt--;
}
}
//Overloading Subtraction operator
int operator - (MyDate dd)
{
return dt - dd.dt;
}
//Overloading << operator
friend ostream & operator << (ostream &out, MyDate &m)
{
string ss;
//Sets the month in character format
switch(m.month)
{
case 1:
ss = "January";
break;
case 2:
ss = "February";
break;
case 3:
ss = "March";
break;
case 4:
ss = "April";
break;
case 5:
ss = "May";
break;
case 6:
ss = "June";
break;
case 7:
ss = "July";
break;
case 8:
ss = "August";
break;
case 9:
ss = "September";
break;
case 10:
ss = "October";
break;
case 11:
ss = "November";
break;
case 12:
ss = "December";
break;
}
out<<ss<<" "<<m.dt<<" "<<m.year;
return out;
}
//Overloading >> operator
friend istream & operator >> (istream &in, MyDate &md)
{

cout<<" Enter Month: ";
in>>md.month;
cout<<" Enter Date: ";
in>>md.dt;
cout<<" Enter Year: ";
in>>md.year;
//Validates month
if(md.month >= 1 && md.month <= 12)
{
//Validates Date
if(md.dt >= 1 && md.dt <= 31)
{
return in;
}
else
cout<<" Invalid Date: ";
}
else
cout<<" Invalid Month: ";
}
};
int main()
{
MyDate st, en;
cout<<" Enter Date: ";
cin>>st;
cout<<" Date: ";
cout<<st;

--st;
cout<<" After Prefix Decrement: ";
cout<<st;

st--;
cout<<" After Postfix Decrement: ";
cout<<st;

++st;
cout<<" After Prefix Increment: ";
cout<<st;

st++;
cout<<" After Postfix Increment: ";
cout<<st;

cout<<" Enter the Ending Date: ";
cin>>en;

cout<<" Starting Date: ";
cout<<st;
cout<<" Ending Date: ";
cout<<en;
cout<<" After Subtraction: ";
cout<<en - st;
}

Output 1:

Enter Date:
Enter Month: 12

Enter Date: 1

Enter Year: 2005


Date: December 1 2005
After Prefix Decrement: November 30 2005
After Postfix Decrement: November 29 2005
After Prefix Increment: November 30 2005
After Postfix Increment: December 1 2005
Enter the Ending Date:
Enter Month: 12

Enter Date: 12

Enter Year: 2005


Starting Date: December 1 2005
Ending Date: December 12 2005

After Subtraction: 11

Output 2:

Enter Date:
Enter Month: 30

Enter Date: 1

Enter Year: 2005

Invalid Month:

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