// File: date.cpp. C++ source code for date objects. #include #include "date.h" // // Function to assign a month, day, and year to the date object. // There is no value checking--the function assumes that the caller // passes a legitimate date. // Pre: mo is an integer between 1 and 12. // dy is an integer between 1 and 31. // yr is an integer between 0 and 9999. // Post: The date object has been assigned a date with month mo, // day dy, and year yr. // void date::AssignDate(int mo, int dy, int yr) { month = mo; day = dy; year = yr; } // // Function to display the date in MM/DD/YY format // Pre: The date object has a value. // Post: The date has been displayed in format MM/DD/YY. // void date::DisplayShort( void ) { cout << month << '/' << day << '/' << (year % 100); } // // Function to print the date to standard output in // verbose form, i.e., similar to October 31, 1998. // Pre: The date object has a value. // Post: The date has been displayed in verbose format. // void date::DisplayVerbose( void ) { switch (month) { case 1: cout << "January "; break; case 2: cout << "February "; 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 "; } cout << day << ", " << year; } // // Function to increment the date to the next day. // Pre: The date object has a value. // Post: The date has been incremented to the next day. // void date::NextDay( void ) { int DaysInMonth; // number of days in date's month switch (month) { case 4: case 6: case 9: case 11: DaysInMonth = 30; break; case 1: case 3: case 5: case 7: case 8: case 10: case 12: DaysInMonth = 31; break; case 2: if (IsLeapYear() == true) DaysInMonth = 29; else DaysInMonth = 28; } if (day < DaysInMonth) // not a new month day = day + 1; else // new month { day = 1; if (month < 12) month = month + 1; else // new year { month = 1; year = year + 1; } } } // Function to check if the current year is a leap year // Pre: the year has a positive value // Post: return a boolean value, true if leap year, false // otherwise. bool date::IsLeapYear() { if ((year % 4 == 0) && (year % 100 != 0) || (year % 400 == 0)) return true; else return false; }