Code:
#include <iostream>
#include <string>
using namespace std;
class Date {
public:
Date(int day = 1,int month = 1, int year = 2000);
void setDay(int day);
void setMonth(int month);
void setYear(int year);
int getDay() const;
int getMonth() const;
void getYear(int& year) const; // return by reference
void increment(); // sets values to the next day
void print() const; // print in dayMonYear format
private:
int thisDay;
int thisMonth;
int thisYear;
void checkDay(); // if day is out of range - reset to first of month
void checkMonth(); // if month is out of range - reset to january
static int daysInMonth[];
static string month[];
};
int main()
{
Date notToday;
cout << "notToday: ";
notToday.print();
notToday.increment();
cout << "\nnotToday after increment: ";
notToday.print();
// have user type in day/month/year - used to testing increment
int day, month, year;
cout << "\nenter date (day month year), e.g. 31 1 2000: ";
cin >> day >> month >> year;
Date someDay(day,month,year);
year = 0; // just to show change
someDay.getYear(year); // becuase return by reference
cout << "Day: " << someDay.getDay() << " month: " << someDay.getMonth() << " year:" << year;
cout << "\nprinting: ";
someDay.print();
someDay.increment();
cout << "\nafter increment: ";
someDay.print();
cout << "\nenter date (day month year), e.g. 31 1 2000: ";
cin >> day >> month >> year;
someDay.setDay(day);
someDay.setMonth(month);
someDay.setYear(year);
cout << "\nprinting: ";
someDay.print();
someDay.increment();
cout << "\nafter increment: ";
someDay.print();
return 0;
}
int Date::daysInMonth[] = {31,28,31,30,31,30,31,31,30,31,30,31};
string Date::month[]={"JAN","FEB","MAR","APR","MAY","JUN","JUL","AUG","SEP","OCT","NOV","DEC"};
Date::Date(int day,int month, int year)
{
thisDay = day;
thisMonth = month;
thisYear = year;
checkMonth();
checkDay(); // why check day second?
}
void Date::setDay(int day)
{
thisDay = day;
checkDay();
}
void Date::setMonth(int month)
{
thisMonth = month;
checkMonth();
checkDay(); // may become out of range due to change in month
}
void Date::setYear(int year)
{
thisYear = year;
}
int Date::getDay() const
{
return thisDay;
}
int Date::getMonth() const
{
return thisMonth;
}
void Date::getYear(int& year) const// return by reference
{
year = thisYear;
}
void Date::increment() // sets values to the next day
{
// first increment day
thisDay++;
if(thisDay > daysInMonth[thisMonth-1]) {
thisDay = 1; // first of the next month
thisMonth++;
if(thisMonth > 12) {
thisMonth = 1; // January next year
thisYear++;
}
}
}
void Date::print() const // print in dayMonYear format
{
cout << thisDay << month[thisMonth-1] << thisYear;
}
void Date::checkMonth()
{
if((thisMonth < 1) || (thisMonth > 12))
thisMonth = 1;
}
void Date::checkDay()
{
if((thisDay < 1) || (thisDay > daysInMonth[thisMonth-1])) {
thisDay = 1;
}
}
Ok,I have some code that gives the next day after you give it a date. I need help trying write a function that I will call "occursBefore", for this class I want to take two "Date" objects and return a "-1" if the first date is chronologically before the second date, return "1" if the first date is chronologically after the second date and return "0" if the two dates are the same.
And if someone feels really fresh, I am going to see if I can do a Days between, where it returns a integer for the number of days between.
Any help would be greatly appreciated
Aquaman
// edited by da for code tags... use em buddy, they really help...