[8:41am] cat date.h // date.h // declaration of data class // member functions in data.cc // Problem 7.7 in D&D // prevent multiple inclusions of header file #ifndef date_h #define date_h class date { public: // NOTE: three different constructors // each with a different TYPE or number // of arguments // constructor for the ddd yyyy format // note only this one has defaults date(int = 1, int = 1996 ); // constructor for the mm dd yy format date(int, int , int ); // constructor for the char-mo dd yyyy format date(char*, int, int); // set functions void setmonth(int = 1); void setday(int = 1); void setyear(int = 96); // NOTE: const functions indicate that they // do NOT change values -- can be called with // constant objects of type date // some utility functions // returns number of days PRIOR // to the current month int monthToDays() const; // returns 4 digit representation of year int year4D() const; // returns character string representation // of current month char* monthString() const; // returns the number of days in the current month int DaysInMonth() const; // some printing functions void printdddyy() const; void printmmddyy() const; void printdate() const; private: int month; // between 1 and 12 (inclusive) int day; // between 1 and 31 (inclusive) int year; // two digit representation between // 70 (for 1970) and 5 (for 2005) }; #endif --------------------------------------------------------------- [8:42am] m date.cc // date.cc // Problem 7.7 in D&D // member functions definitions for date class #include #include #include "date.h" // constructor functions to initialize private data // default value for date is 1/1/96 // constructor for the ddd yyyy format date::date(int ddd, int yr) { int mo, moDays = 0; if (yr >= 2000) setyear(yr - 2000); else setyear(yr - 1900); if ((ddd < 1) || (ddd > 365)) { setmonth(ddd); setday(ddd); } else { for (mo = 1; (moDays < ddd); mo++) { setmonth(mo); moDays += DaysInMonth(); } moDays -= DaysInMonth(); setmonth(mo - 1); setday(ddd - moDays); } } // constructor for the mm dd yy format date::date(int mm, int dd, int yy) { setmonth(mm); setday(dd); setyear(yy); } // constructor for the char-mo dd yyyy format date::date(char* mo, int dy, int yr) { int m; if (yr >= 2000) setyear(yr - 2000); else setyear(yr - 1900); setmonth(m = 1); // if ((mo != "January") || (mo != "February") || (mo != "March") || // (mo != "April") || (mo != "May") || (mo != "June") || // (mo != "July") || (mo != "August") || (mo != "September") || // (mo != "October") || (mo != "November") || (mo != "December")) // mo = "January"; while (strcmp(monthString(), mo) != 0) { cout << "In while loop in constructor function, "; cout << "m = " << m; cout << " monthString() = " << monthString() << endl; cout << " mo = " << mo << endl; cout << "\t strcmp(monthString(), mo) = " << strcmp(monthString(), mo); cout << endl; if (m > 12) { m = 1; mo = "January"; } else setmonth(++m); } setday(dy); } // set functions void date::setmonth(int mo) { month = ((mo > 0) && (mo <= 12)) ? mo : 1; } void date::setday(int dy) { day = (dy > 0 && dy <= 31) ? dy : 1; } void date::setyear(int yr) { year = ((yr >= 70 && (yr < 100)) || (yr >= 0) && (yr <=5)) ? yr : 96; } // some utility functions // returns number of days PRIOR to the current month int date::monthToDays() const { int thismo; date temp; if (month == 1) return 0; switch(month) { case 2: thismo = 28; break; case 4: case 6: case 9: case 11: thismo = 30; break; default: thismo = 31; break; } temp.setmonth(month - 1); thismo += temp.monthToDays(); return thismo; } // returns 4 digit representation of year int date::year4D() const { if (year < 70) return (2000 + year); else return (1900 + year); } // returns character string representation of current month char* date::monthString() const { char* months[] = {" ", "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"}; return months[month]; } // returns the number of days in the current month int date::DaysInMonth() const { int days[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; return days[month]; } // some printing functions void date::printdddyy() const { int daytotal; daytotal = monthToDays() + day; if (daytotal < 10) cout << "00"; else if (daytotal < 100) cout << "0"; cout << daytotal; cout << " "; cout << year4D(); cout << endl; } void date::printmmddyy() const { cout << month << "/" << day << "/" << year << endl; } void date::printdate() const { cout << monthString() << " " << day << ", " << year4D() << endl; } ------------------------------------------------------------- [8:43am] m 22-date.cc // driver program to test date class // Problem 7.7 in D&D #include #include "date.h" main () { date d1; cout << "Date d1:" << endl; d1.printdddyy(); d1.printmmddyy(); d1.printdate(); cout << endl; date d2(345, 1987); cout << "Date d2:" << endl; d2.printdddyy(); d2.printmmddyy(); d2.printdate(); cout << endl; date d3(10, 12, 84); cout << "Date d3:" << endl; d3.printdddyy(); d3.printmmddyy(); d3.printdate(); cout << endl; date d4("May", 16, 87); cout << "Date d4:" << endl; d4.printdddyy(); d4.printmmddyy(); d4.printdate(); cout << endl; date d5(555,9999); cout << "Date d5:" << endl; d5.printdddyy(); d5.printmmddyy(); d5.printdate(); cout << endl; date d6("may", 555,9999); cout << "Date d6:" << endl; d6.printdddyy(); d6.printmmddyy(); d6.printdate(); cout << endl; return 0; } -------------------------------------------------------------- [8:44am] CC 22-date.cc date.cc 22-date.cc: date.cc: [8:44am] a.out Date d1: 001 1996 1/1/96 January 1, 1996 Date d2: 345 1987 12/11/87 December 11, 1987 Date d3: 285 1984 10/12/84 October 12, 1984 In while loop in constructor function, m = 1 monthString() = January mo = May strcmp(monthString(), mo) = -3 In while loop in constructor function, m = 2 monthString() = February mo = May strcmp(monthString(), mo) = -7 In while loop in constructor function, m = 3 monthString() = March mo = May strcmp(monthString(), mo) = -7 In while loop in constructor function, m = 4 monthString() = April mo = May strcmp(monthString(), mo) = -12 Date d4: 136 1996 5/16/96 May 16, 1996 Date d5: 001 1996 1/1/96 January 1, 1996 In while loop in constructor function, m = 1 monthString() = January mo = may strcmp(monthString(), mo) = -35 In while loop in constructor function, m = 2 monthString() = February mo = may strcmp(monthString(), mo) = -39 In while loop in constructor function, m = 3 monthString() = March mo = may strcmp(monthString(), mo) = -32 In while loop in constructor function, m = 4 monthString() = April mo = may strcmp(monthString(), mo) = -44 In while loop in constructor function, m = 5 monthString() = May mo = may strcmp(monthString(), mo) = -32 In while loop in constructor function, m = 6 monthString() = June mo = may strcmp(monthString(), mo) = -35 In while loop in constructor function, m = 7 monthString() = July mo = may strcmp(monthString(), mo) = -35 In while loop in constructor function, m = 8 monthString() = August mo = may strcmp(monthString(), mo) = -44 In while loop in constructor function, m = 9 monthString() = September mo = may strcmp(monthString(), mo) = -26 In while loop in constructor function, m = 10 monthString() = October mo = may strcmp(monthString(), mo) = -30 In while loop in constructor function, m = 11 monthString() = November mo = may strcmp(monthString(), mo) = -31 In while loop in constructor function, m = 12 monthString() = December mo = may strcmp(monthString(), mo) = -41 In while loop in constructor function, m = 13 monthString() = January mo = may strcmp(monthString(), mo) = -35 Date d6: 001 1996 1/1/96 January 1, 1996