110 likes | 239 Views
Problem Session. Working in pairs of two, solve the following problem. Problem. Design a Date class to represent calender dates (e.g., May 1, 2000). Identify the function members needed to operate on Date objects; the data members needed to represent dates.
E N D
Problem Session Working in pairs of two, solve the following problem...
Problem Design a Date class to represent calender dates (e.g., May 1, 2000). Identify • the function members needed to operate on Date objects; • the data members needed to represent dates. Implement as much of your design as time permits. You may restrict dates to those since 1 AD.
Coding // Date.h declares a ‘bare bones’ Date class // #include directives have been omitted ... class Date { public: Date(); Date(string month, int day, int year); string Month() const; int Day() const; int Year() const; friend istream & operator>>(istream & in, Date & aDate); friend ostream & operator<<(ostream & out, const Date & aDate); private: string myMonth; // January..December int myDay, // 1..31 myYear; // 1..? };
Coding (Ct’d) // ... still in Date.h inline Date::Date() { myMonth = “January”; myDay = 1; myYear = 2000; } bool ValidDate(string month, int day, int year); inline Date::Date(string month, int day, int year) { assert(ValidDate(month, day, year)); myMonth = month; myDay = day; myYear = year; }
Coding (Ct’d) // ... still in Date.h inline string Date::Month() const { return myMonth; } inline int Date::Day() const { return myDay; } inline int Date::Year() const { return myYear; }
Coding (Ct’d) // ... still in Date.h inline ostream & operator<<(ostream & out, const Date & aDate) { out << aDate.myMonth << ‘ ‘ // January <space> << aDate.myDay << “, “ // 1 <comma> << aDate.myYear; // 2000 return out; // allow chaining }
Coding (Ct’d) // Date.cpp defines non-trivial Date function members. // ... #include “Date.h” // class Date bool ValidYear(int year); bool ValidMonth(string month); bool ValidDay(int day); int DaysIn(string month, int year); bool ValidDate(string month, int day, int year) { return ValidMonth(month) && ValidDay(day) && day <= DaysIn(month, year) && ValidYear(year); } bool ValidYear(int year) { return year > 0; }
Coding (Ct’d) // ... Date.cpp continued bool ValidMonth(string month) { return month == “January” || month == “Jan” || month == “February” || month == “Feb” || month == “March” || month == “Mar” || month == “April” || month == “Apr” || month == “May” || month == “June” || month == “Jun” || month == “July” || month == “Jul” || month == “August” || month == “Aug” || month == “September” || month == “Sep” || month == “October” || month == “Oct” || month == “November” || month == “Nov” || month == “December” || month == “Dec”; }
Coding (Ct’d) // ... Date.cpp continued bool ValidDay(int day) { return day >= 1 && day <= 31; } bool LeapYear(int year) { if (year % 4 == 0) if (year % 100 == 0) if (year % 400) == 0) return true; else return false; else return true; else return false; }
Coding (Ct’d) // ... Date.cpp continued int DaysIn(string month, int year) { if (month == “January” || month == “Jan”) return 31; else if (month == “February” || month == “Feb”) if (LeapYear(year)) return 29; else return 28; else if (month == “March” || month == “Mar”) return 31; else if (month == “April” || month == “Apr”) return 30; else if (month == “May”) return 31; else if (month == “June” || month == “Jun”) return 30; else if (month == “July” || month == “Jul”) return 31; else if (month == “August” || month == “Aug”) return 31; else if (month == “September” || month == “Sep”) return 30; else if (month == “October” || month == “Oct”) return 31; else if (month == “November” || month == “Nov”) return 30; else if (month == “December” || month == “Dec”) return 31; }
Coding (Ct’d) // ... Date.cpp continued istream & operator>>(istream & in, Date & aDate) { string month; int day, year; char ch; in >> month >> day; // January 1 in.get(ch); // either comma or space in >> year; // 2000 if (ValidDate(month, day, year)) // if date is ok aDate = Date(month, day, year); // passback values else // otherwise in.setstate(ios::failbit); // set stream’s failbit return in; // allow chaining }