CPP   31

Date.h

Guest on 5th August 2022 01:09:57 AM

  1. //: C02:Date.h
  2. // From "Thinking in C++, Volume 2", by Bruce Eckel & Chuck Allison.
  3. // (c)  MindView, Inc. All Rights Reserved.
  4. // See source code use permissions stated in the file 'License.txt',
  5. // distributed with the code package available at www.MindView.net.
  6. #ifndef DATE_H
  7. #define DATE_H
  8. #include <string>
  9. #include <stdexcept>
  10. #include <iosfwd>
  11.  
  12. class Date {
  13.   int year, month, day;
  14.   int compare(const Date&) const;
  15.   static int daysInPrevMonth(int year, int mon);
  16. public:
  17.   // A class for date calculations
  18.   struct Duration {
  19.     int years, months, days;
  20.     Duration(int y, int m, int d)
  21.     : years(y), months(m) ,days(d) {}
  22.   };
  23.   // An exception class
  24.   struct DateError : public std::logic_error {
  25.     DateError(const std::string& msg = "")
  26.       : std::logic_error(msg) {}
  27.   };
  28.   Date();
  29.   Date(int, int, int) throw(DateError);
  30.   Date(const std::string&) throw(DateError);
  31.   int getYear() const;
  32.   int getMonth() const;
  33.   int getDay() const;
  34.   std::string toString() const;
  35.   friend Duration duration(const Date&, const Date&);
  36.   friend bool operator<(const Date&, const Date&);
  37.   friend bool operator<=(const Date&, const Date&);
  38.   friend bool operator>(const Date&, const Date&);
  39.   friend bool operator>=(const Date&, const Date&);
  40.   friend bool operator==(const Date&, const Date&);
  41.   friend bool operator!=(const Date&, const Date&);
  42.   friend std::ostream& operator<<(std::ostream&,
  43.                                   const Date&);
  44.   friend std::istream& operator>>(std::istream&, Date&);
  45. };
  46. #endif // DATE_H ///:~

Raw Paste


Login or Register to edit or fork this paste. It's free.