Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Programming language is C++. Show finished code and an example of the output on

ID: 3826562 • Letter: P

Question

Programming language is C++. Show finished code and an example of the output on PuTTY when done. Use g++ -std=c++17 to compile the code with the chrono.cpp and chrono.h file on PuTTY.

//chrono.h

//file chrono.h

#include "std_lib_facilities_4.h"

namespace Chrono {
   enum class Month {
       jan = 1, feb, mar, apr, may, jun, jul, aug, sep, oct, nov, dec
   };

   class Date {
   public:
       class Invalid {}; //to throw as exception

       Date(int y, Month m, int d); //check for valid date and initialize
       Date(); //default constructor
       // the default copy operations are fine

       //nonmodifying operations:
       int day() const { return d; }
       Month month() const { return m; }
       int year() const { return y; }

       Date &operator++();
       //modifying operations:
       void add_day(int n);
       void add_month(int n);
       void add_year(int n);

   private:
       int y;
       Month m;
       int d;

   };

   bool is_date(int y, Month m, int d); //true for valid date
   bool leapyear(int y); //true if y is a leap year

   bool operator==(const Date& a, const Date & b);
   bool operator!=(const Date& a, const Date& b);

   ostream& operator<<(ostream& os, const Date& d);

   istream& operator>>(istream& is, Date& dd);

} //Chrono

//chrono.cpp

#include "Chrono.h"

namespace Chrono {
   //member function defintions:

   Date::Date(int yy, Month mm, int dd)
       :y{ yy }, m{ mm }, d{ dd }
   {
       if (!is_date(yy, mm, dd)) throw Invalid{};

   }

   const Date& default_date()
   {
       static Date dd{ 2001,Month::jan,1 }; //start of 21st century
       return dd;
   }

   Date::Date()
       :y{ default_date().year() },
       m{ default_date().month() },
       d{ default_date().day() }
   {
   }

   Date& Date::operator++()
   {
       d = d + 1;
       if (is_date == false) {
           d = 1;
       }
       d = 1;
       if (m == Month::dec) {
           m == Month::jan;
           y = y + 1;
          
       }

       else {
           int oldM = (int)m;
           oldM = oldM + 1;
           m = static_cast<Month>(oldM);
       }

       return *this;
   }

   void Date::add_day(int n)
   {
       //
   }

   void Date::add_month(int n)
   {
       //
   }

   void Date::add_year(int n)
   {
       if (m == Month::feb&&d == 29 && !leapyear(y + n)) { //beware of leap years!
           m = Month::mar; //use March 1 instead of February 29
           d = 1;
       }
       y += n;
   }
   //helper functions:

   bool is_date(int y, Month m, int d)
   {
       //assume that y is valid

       if (d <= 0) return false; //d must be positive
       if (m < Month::jan || Month::dec < m) return false;

       int days_in_month = 31; //most months have 31 days

       switch (m) {
       case Month::feb: //the length of February varies
           days_in_month = (leapyear(y)) ? 29 : 28;
           break;
       case Month::apr:case Month::jun: case Month::sep:case Month::nov:
           days_in_month = 30; //the rest have 30 days
           break;
       }

       if (days_in_month < d) return false;

       return true;
   }
  
   bool leapyear(int y)
   {
       //see exercise 10
   }
       bool operator==(const Date& a, const Date& b)
       {
           return a.year() == b.year()
               && a.month() == b.month()
               && a.day() == b.day();
       }

       bool operator!=(const Date& a, const Date& b)
       {
           return !(a == b);
       }
       ostream& operator<<(ostream& os, const Date& d)
       {
           return os << '(' << d.year()
               << ',' << (int)d.month()
               << ',' << d.day() << ')';
       }

       istream& operator >> (istream& is, Date& dd)
       {
           int y, m, d;
           char ch1, ch2, ch3, ch4;
           is >> ch1 >> y >> ch2 >> m >> ch3 >> d >> ch4;
           if (!is) return is;
           if (ch1 != '(' || ch2 != ',' || ch3 != ',' || ch4 != ')') {
               is.clear(ios_base::failbit);
               return is;
           }

           dd = Date(y, Month(m), d);

           return is;
       }
      


       enum class Day {
           sunday, monday, tuesday, wednesday, thursday, friday, saturday
       };

       Day day_of_week(const Date& d)
       {
           return Day::sunday;
       }
       Day next_Sunday(const Date& d)
       {
           return Day:: sunday;
          
       }
       Day next_weekday(const Date& d)
       {
           return Day::sunday;
       }

   } //Chrono

3. (20 points) Start with Chrono,h and Chrono.cpp in section 9.8; for now put a dummy "return Sunday; or "return d in the last 3 functions on pages 337-338. Add a prefix operator to the Date class by putting the declaration in the Date class in Chrono,h and the definition in Chrono.cpp. The prefix operator should change a date to "tomorrow" using the following pseudocode: Date& operator ++0{ /addlto d //tomorrow, unless we were at the end ofthe month //if is date is false //need to change to first of next month set d to 1 if m is December Ineed to change to next year too set m to January increment y else increment m return this

Explanation / Answer

#include "std_lib_facilities_4.h"

namespace Chrono {
   enum class Month {
       jan = 1, feb, mar, apr, may, jun, jul, aug, sep, oct, nov, dec
   };

   class Date {
   public:
       class Invalid {}; //to throw as exception

       Date(int y, Month m, int d); //check for valid date and initialize
       Date(); //default constructor
       // the default copy operations are fine

       //nonmodifying operations:
       int day() const { return d; }
       Month month() const { return m; }
       int year() const { return y; }

       Date &operator++();
       //modifying operations:
       void add_day(int n);
       void add_month(int n);
       void add_year(int n);

   private:
       int y;
       Month m;
       int d;

   };

   bool is_date(int y, Month m, int d); //true for valid date
   bool leapyear(int y); //true if y is a leap year

   bool operator==(const Date& a, const Date & b);
   bool operator!=(const Date& a, const Date& b);

   ostream& operator<<(ostream& os, const Date& d);

   istream& operator>>(istream& is, Date& dd);

}