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

Add a constructor to the Date class using the versions of Chrono.cpp and Chrono.

ID: 3788322 • Letter: A

Question

Add a constructor to the Date class using the versions of Chrono.cpp and Chrono.h found below that initializes the Date with format "month day year" where month is type Month, day is type int, and year is type int. Be sure to check (in the constructor) that a valid Date was entered. Create a program that uses the Date class in your modified Chrono.cpp to allow the user to enter a date using any of the three Date constructor formats (year-month-day constructor, default-date contructor, and the month-day-year constructor you added) and then print out the result (using the Date class << operator). Don't forget to have a way for the user to exit the program. And don't forget to check for errors. Please show what was modified below and the new program.

__________________________________________Chrono.h________________________________________________________________

#include <iostream>

using namespace std;

//------------------------------------------------------------------------------

namespace Chrono {

//------------------------------------------------------------------------------

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

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

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

// 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, Date::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 definitions:

//------------------------------------------------------------------------------

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 const Date dd(2001,Date::jan,1); // start of 21st century
return dd;
}

//------------------------------------------------------------------------------

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

//------------------------------------------------------------------------------

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

//------------------------------------------------------------------------------

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

//------------------------------------------------------------------------------

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

//------------------------------------------------------------------------------

// helper functions:

bool is_date(int y, Date::Month m, int d)
{
// check that y is valid
if (y<0) return false; // y must be positive

// check that m is valid
int mint = m; // mint must be 1 to 12
if ((mint<1) || (mint>12)) return false;

if (d<=0) return false; // d must be positive

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

switch (m) {
case Date::feb: // the length of February varies
days_in_month = (leapyear(y))?29:28;
break;
case Date::apr: case Date::jun: case Date::sep: case Date::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 ???
return false;
}

//------------------------------------------------------------------------------

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()
<< ',' << 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!=')') { // oops: format error
is.clear(ios_base::failbit); // set the fail bit
return is;
}
dd = Date(y,Date::Month(m),d); // update dd
return is;
}

//------------------------------------------------------------------------------

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

//------------------------------------------------------------------------------

Day day_of_week(const Date& d)
{
// ...
return sunday;
}

//------------------------------------------------------------------------------

Date next_Sunday(const Date& d)
{
// ...
return d;
}

//------------------------------------------------------------------------------

Date next_weekday(const Date& d)
{
// ...
return d;
}

//------------------------------------------------------------------------------

} // Chrono

Explanation / Answer

//main.cpp

#include "chrono.h"
#include<iostream>
using namespace Chrono;

int main()
{
  
   Date d1;
   Date d2(2016,d1.jan, 21);
   Date d3(d1.mar, 31, 2016);
   //test default constructor
   cout << "default date: " << d1.day() << d1.month() << d1.year() << endl;
   //test year - month - day constructor
   cout << "date : year month-day: " << d2.year() << d2.month() << d2.day() << endl;
   //test month - day - yearconstructor
   cout << "date : month - day - year: " << d3.month() << d3.day() << d3.year() << endl;
   //test day_of_week for d1
   cout << "day of the week for d1: " << day_of_week(d1)<<endl;
   //test day_of_week for d2
   cout << "day of the week for d2: " << day_of_week(d2) << endl;
   //test day_of_week for d3
   cout << "day of the week for d3: " << day_of_week(d3) << endl;
}

----------------------------------------

//changed Chrono.cpp,find my changes under //added by chegg EA

#include "Chrono.h"
namespace Chrono {
   // member function definitions:
   //------------------------------------------------------------------------------
   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 const Date dd(2001, Date::jan, 1); // start of 21st century
       return dd;
   }
   //constructor for month-day-year
   Date::Date(Month mm, int dd, int yy)
       : y(yy), m(mm), d(dd)
   {
       if (!is_date(yy, mm, dd)) throw Invalid();
   }
   //------------------------------------------------------------------------------
   Date::Date()
       :y(default_date().year()),
       m(default_date().month()),
       d(default_date().day())
   {
   }
   //------------------------------------------------------------------------------
   void Date::add_day(int n)
   {
       // ...
       //added by chegg EA
       //check if day is less than 31 for particular months
       if (m == 1 || m == 3 || m == 5 || m == 7 || m == 8 || m == 10 || m == 12)
       {
           if (n <= 31)
               d = n;
          
       }
       //check if day is less than 30 for particular months
       if (m == 4 || m == 6 || m == 9 || m == 11 )
       {
           if (n <= 30)
               d = n;

       }
       //check if day is less than 29 for leap year for feb month otherwise 28 for feb
       if (leapyear(n))
       {
           if (n <= 29)
               d = n;
       }
       else
       {
           if (n <= 28)
               d = n;
       }

   }
   //------------------------------------------------------------------------------
   void Date::add_month(int n)
   {
       // ...
       //added by chegg EA
       /*check if month is greater than 0 and less than equal to 12*/
       if (n > 0 && n <= 12)
           m = (Month)n;
   }
   //------------------------------------------------------------------------------
   void Date::add_year(int n)
   {
       if (m == feb && d == 29 && !leapyear(y + n)) { // beware of leap years!
           m = mar; // use March 1 instead of February 29
           d = 1;
       }
       y += n;
   }
   //------------------------------------------------------------------------------
   // helper functions:
   bool is_date(int y, Date::Month m, int d)
   {
       // check that y is valid
       if (y<0) return false; // y must be positive
       // check that m is valid
       int mint = m; // mint must be 1 to 12
       if ((mint<1) || (mint>12)) return false;
       if (d <= 0) return false; // d must be positive
       int days_in_month = 31; // most months have 31 days
       switch (m) {
       case Date::feb: // the length of February varies
           days_in_month = (leapyear(y)) ? 29 : 28;
           break;
       case Date::apr: case Date::jun: case Date::sep: case Date::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 ???
       //added by chegg EA
       if (y % 4 == 0)
       {
           return true;
       }
       return false;
   }
   //------------------------------------------------------------------------------
   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()
           << ',' << 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 != ')') { // oops: format error
           is.clear(ios_base::failbit); // set the fail bit
           return is;
       }
       dd = Date(y, Date::Month(m), d); // update dd
       return is;
   }
   //------------------------------------------------------------------------------
   /*enum Day {
       sunday, monday, tuesday, wednesday, thursday, friday, saturday
   };*/
   //------------------------------------------------------------------------------
   Day day_of_week(const Date& d)
   {
       // ...
       //added by chegg EA
      
       //return sunday;
       enum Day tmp = (enum Day)d.day();
       return tmp;
   }
   //------------------------------------------------------------------------------
   Date next_Sunday(const Date& d)
   {
       // ...
       //added by chegg EA
       //add day to next sunday
       Date tmp;
       int count = 0;
       //get sunday date when is sunday
       while (d.day() != sunday)
           count++;
       tmp.add_day(d.day() + count);
       tmp.add_month(d.month());
       tmp.add_year(d.year());
       return tmp;
   }
   //------------------------------------------------------------------------------
   Date next_weekday(const Date& d)
   {
       // ...
       //added by chegg EA
       //added by chegg EA
       //add day to next sunday
       Date tmp;
       //add 7 to present day to get next week day
       tmp.add_day(d.day() + 7);
       tmp.add_month(d.month());
       tmp.add_year(d.year());
       return tmp;
       return d;
   }
   //------------------------------------------------------------------------------
} // Chrono

----------------------------------------------------------------------------

//changed Chrono.cpp,find my changes under //added by chegg EA

#include<fstream>
using namespace std;

namespace Chrono {
   //------------------------------------------------------------------------------
   class Date {
   public:
       enum Month {
           jan = 1, feb, mar, apr, may, jun, jul, aug, sep, oct, nov, dec
       };
       class Invalid { }; // to throw as exception
       Date(int y, Month m, int d); // check for valid date and initialize
       Date(); // default constructor
       //added by chegg EA, constructor to take month-day-year
       Date(Month mm, int dd, int yy);
       // the default copy operations are fine
       // non-modifying operations:
       int day() const { return d; }
       Month month() const { return m; }
       int year() const { return y; }
       // 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, Date::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);
   //------------------------------------------------------------------------------
   enum Day {
       sunday, monday, tuesday, wednesday, thursday, friday, saturday
   };
   //------------------------------------------------------------------------------
   Day day_of_week(const Date& d);
   Date next_weekday(const Date& d);
   Date next_Sunday(const Date& d);
} // Chrono

------------------------------------------------------------------------

//output

default date: 112001
date : year month-day: 2016121
date : month - day - year: 3312016
day of the week for d1: 1
day of the week for d2: 21
day of the week for d3: 31

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote