//********************************************************************* // Driver program to test the date class // // Written by : Rex Forsyth // May 15, 2006 // An example for CS2660 //********************************************************************* #include using namespace std; //#include //using namespace std::rel_ops; #include "date.h" const Date JAN01 = Date(2006,01,01); int main() { // get valid user input using prompts Date day0; day0.read(); cout << "Day 0 is "; day0.print(); cout << endl; // get invalid user input without prompts cout << "Enter an invalid date -- "; cin >> day0; cout << "After invalid input day 0 is "<< day0 << endl; // initialize date with no parameters -- result should be the default Date day1; cout << "after declaration with no parameters " << "day1 is " << day1 << endl; // check assignment of a date day1 = day0; cout << "after assignment to day 0, day 1 is " << day1 << endl; // create a valid date Date day2(2000,2,29); cout << "After Date day(2000,2,29), day 2 is " << day2 << endl; // creating an invalid date Date day3(1997,9,31); // not 31 days in 9th month cout << "After Date day3(1997,9,31), day 3 is " << day3 << " since Sept 31 is invalid. " << endl; // subtracting 4 years from Feb 29 -- should still be Feb 29, 1996 day2.addYear(-4); cout << "after subtracting 4 years from day2, it is " << day2 << endl; // subtracting 365 days from Feb 29 -- should be Mar 1, 1995 day2.addDay(-365); cout << "after subtracting 365 days from day2, it is " << day2 << endl; // subtracting 37 months -- should be 1 Feb 1992 day2.addMonth(-37); cout << "after subtracting 37 months from day2, it is "; day2.print(cout); cout << endl; Date day4(1996,2,29); cout << "day4 is " << day4 << endl; // subtracting 366 days -- should be 28 Feb 1995 day4.addDay(-366); cout << "after subtracting 366 days from day4, it is " << day4 << endl; // subtracting 28 days to get to the 31st of Jan 1995 day4.addDay(-28); cout << "after subtracting 28 days from day4, it is " << day4 << endl; // checking that subtracting 2 months from 31 gives end of prev month // subtracting 2 months -- should be 30 Nov 1994 day4.addMonth(-2); cout << "after subtracting 2 months from day4, it is " << day4 << endl; // testing the leapYear function day4.print(cout); cout << " is"; if (!Date::leapYear(day4.year())) cout << " not"; cout << " in a leap year." << endl; // testing the today function Date day5; day5 = Date::today(); cout << "Today is " << day5<< endl; // testing comparison operators cout << day2; if (day2 == day2) cout << " is the same as "; else cout << " is not the same as "; cout << day2 << endl; cout << day3; if (day3 == day2) cout << " is the same as "; else cout << " is not the same as "; cout << day2 << endl; cout << day4; if (day4 < day3) cout << " comes before "; else cout << " does not come before "; cout << day3 << endl; cout << day3; if (day3 > day4) cout << " comes after "; else cout << " does not come after "; cout << day4 << endl; // showing need for const on the parameter if (day3 < Date::today()) cout << "In the past"; else cout << "In the future."; cout << endl; if (day3 < JAN01) cout << "before 2006"; else cout << "after"; cout << endl; // showing need for const on the function if (JAN01 < day3) cout << "before"; else cout << "after"; cout << endl; return 0; }