I have this one homework that has been bugging me for about week now, and I still can't figure it out. Its based on an earlier problem I did, but I just can't get it, I must be missing something. Well if you can help me figure this out I'd very much appreciate it.
Modify the Date class in the chapter before. The new version shoudl 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, 2000 is subtracted from April 18, 2000, the result will be 8.
<< cout's stream insertion operator. This operator should cause the date to be dsiplayed in the form: April 18, 2000
>> cin's extraction operator. This operator shoudl prompts the user for a date to be stored in the Date object.
The class should etect the following conditions and handle them accordingly:
when a date is et 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 et 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
I figured out the first part of this problem and here is the Date.h header file
Code:
#ifndef DATE_H
#define DATE_H
#include <string>
#include <cctype>
using namespace std;
class Date
{
private:
int month;
int day;
int year;
char date[9];
char name[10];
public:
Date() // empty constructor
{
month = 1;
day = 1;
year = 1900;
strcpy(date,"");
strcpy(name, "");
}
Date(int m, int d, int y) { setMonth(m);
setYear(y);
setDay(d);
strcpy(date,"");
strcpy(name, "");
}
void setMonth (int m) //checks if m is a valid month
{ if (m>0 && m < 13)
month = m;
else
month=1;
}
void setYear(int y) //checks if y is a valid year
{
if (y>=1900)
year=y;
else
year=1900;
}
void setDay(int d) //checks if d is a valid day
{
if (d>0 && d<=31)
switch(month)
{
case 1: day=d;
break;
case 3: day=d;
break;
case 5: day=d;
break;
case 7: day=d;
break;
case 8: day=d;
break;
case 10: day=d;
break;
case 12: day=d;
break;
case 4: if (d<=30)
day=d;
else
{
day=1;
break;
}
case 6: if (d<=30)
day=d;
else
{
day=1;
break;
}
case 9: if (d<=30)
day=d;
else
{
day=1;
break;
}
case 11: if (d<=30)
day=d;
else
{
day=1;
break;
}
case 2: if (year%100==0)
{
if (year%400==0)
{
if (d<=29)
day=d;
else
day=1;
}
else
{
if (d<= 28)
day=d;
else
day=1;
}
}
else
{
if (year%4==0)
{
if (d<=29)
day=d;
else
day=1;
}
else
{
if (d<= 28)
day=d;
else
day=1;
}
break;
}
}
}
char * getShortDate()
{
char strDay[3] = { '\0' };
char strMon[3] = { '\0' };
char strYr[3] = { '\0' };
itoa(month, strMon, 10);
strcpy(date, strMon);
strcat(date, "/");
itoa(day, strDay, 10);
strcat(date, strDay);
strcat(date, "/");
itoa((year%100), strYr, 10);
strcat(date, strYr);
return date;
}
char * getMonthName(int m)
{
switch (m)
{
case 1: strcpy(name, "January");
break;
case 2: strcpy(name, "February");
break;
case 3: strcpy(name, "March");
break;
case 4: strcpy(name, "April");
break;
case 5: strcpy(name, "May");
break;
case 6: strcpy(name, "June");
break;
case 7: strcpy(name, "July");
break;
case 8: strcpy(name, "August");
break;
case 9: strcpy(name, "September");
break;
case 10: strcpy(name, "October");
break;
case 11: strcpy(name, "November");
break;
case 12: strcpy(name, "December");
break;
}
return name;
}
};
#endif
And here is the Date.cpp file:
Code:
#include <iostream>
#include "Date.h"
using namespace std;
int main()
{
Date date;
int days, months, years;
cout << "Which month would you like to display? " << endl;
cin >> months;
date.setMonth(months);
cout << "Which year would you like to display? " << endl;
cin >> years;
date.setYear(years);
cout << "Which day would you like to display? " << endl;
cin >> days;
date.setDay(days);
cout << endl;
cout << "Here is the information: " << endl;
cout << endl;
cout << date.getMonthName(months) << " " << days << ", " << years << endl;
return 0;
}
Any help is very much appreciated