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

Write a C++ program: Programming Projects 2.23. Implement a simple Date class.Yo

ID: 3841721 • Letter: W

Question

Write a C++ program:

Programming Projects 2.23. Implement a simple Date class.You should be able to represent any date from January 1, 1800, through December 31, 2500, subtract two dates, increment a date by a number of days, and compare two dates by using A Date is represented internally as the number of days since some starting time, which here is the first day of 1800 making most operations except for lyo trivial. The rule for leap years is: A year is a leap year if it is divisible by 4, and not divisible by 100 unless it is also divisible by 400. Thus 1800, 1900, and 2100 are not leap years, but 2000 is. The input operation must check the validity of the input. The output operation must check the validity of the Date. The Date could be bad if a or operator caused it to go out of range Figure 2.29 gives a class specification skeleton for the Date class. Several items are missing, including public and private keywords, const and &, and I/o interfaces. Before you begin cod ing the interface, you must make some decisions: 93 Exercises 1 class Date 3 enum FIRST YEAR 1800, IMAX YEAR. 2500 int total Days: Days since 1/1/1800 Constructor. Date int y FIRST YEAR int m 1, int d 1 10 Assignment operator instead of 11 Date operator int days 12 13 Binary operators 14 int operator Date right 15 bool operator Date right 16 Figure 2.29 Class specification skeleton for Date (Exercise 2.23).

Explanation / Answer

Class Date header file implementation:

#ifndef DATE_H_

#define DATE_H_ // Prevent multiple-inclusion of header file

#include <iostream>

using namespace std;

class Date

{

protected:

int year ;

int month ;

int day ;

int totalDays;

public:

enum { FIRST_YEAR=1800 , MAX_YEAR=2500 };

Date1(const int& y, const int& m, const int& d);

Date(int y=FIRST_YEAR,int m=1,int d=1); //Constructor

bool valid(void) const; //check if date valid

int day() const;

int month() const;

int year() const;

void set day (const int& day );

void set month (const int& month );

void set year (const int& year );

//date operators

Date operator ++(); // prefix operator

Date operator ++(int); // postfix operator

Date operator+=(int days); //assignment operator instead of +

};

//comparison operators

bool operator < (const Date&, const Date&);

//Binary operators

int operator- (Date right);

bool operator < (Date right);

//ouput operator

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

#endif

Code implementation

#include "date.h"

Date::Date1(const int& y, const int& m, const int& d) {

year = y;

month = m;

day = d;

}

Date::Date(int y=FIRST_YEAR,int m=1,int d=1)//Constructor

{

year = FIRST_YEAR;

month = 1;

day = 1;

}

// inline definitions //

int Date::day() const {

return day ;

};

int Date::month() const { return month ; };

int Date::year() const { return year ; };

void Date::set day (const int& day) { Date::day = day; };

void Date::set month(const int& month) { Date::month = month; };

void Date::set year (const int& year) { Date::year = year; };

bool date::valid() const {

// This function will check the given date is valid or not.

if (year <0) return false;

if (month >12 | | month <1) return false;

if (day >31 | | day <1) return false;

if (year <1800 || year >2500) return false;

if ( year >1800 && year <2500)

{

if ( (year)%4==0) && !((year)%100==0)) {

if ( (year)%400==0) )

cout<<"valid leap year";

return true;

}

}

}

inline date next_Date(const date& d) //inline defintiton

{

date ndat;

if (!d.valid())

{

return ndat;

};

ndat=date((d.day()+1),d.month(),d.year());

if (ndat.valid()) return ndat;

ndat=date(1,(d.month()+1),d.year());

if (ndat.valid()) return ndat;

ndat = date(1,1,(d.year()+1));

return ndat;

}

Date Date::operator ++()

{

// prefix operator

*this = next_Date(*this);

return *this;

}

Date Date::operator ++(int)

{

// postfix operator

Date d = *this;

*this = next_Date(d);

return d;

}

Date operator+=(int days)

{

//assignment operator instead of +

{

date d = *this;

if (d.valid())

{

ndat=date((d.day()+days),d.month(),d.year());

return ndat;

}

}

//comparison operators

bool operator < (const Date&, const Date&)

{

if (d1==d2) { return true; }
return (d1<d2);
}

//Binary operators

int operator- (Date right)

{

int datediff;

date d = *this;

date d1=right;

if (d.valid() && d1.valid())

{

ndat1 = date((d.day()),d.month(),d.year());

ndat2 = date((d.day()),d.month(),d.year());

}

datediff=ndat1-ndat2;

return datediff;

}

bool operator < (Date right)

{

int datediff;

date d = *this;

date d1=right;

if (d.valid() && d1.valid())

{

ndat1 = date((d.day()),d.month(),d.year());

ndat2 = date((d.day()),d.month(),d.year());

}

if(ndat1<ndat2)

{return true;}

else

return false;

}

inline long long_Date(const Date& d)

{

if (d.valid())

{

return d.year() * 10000 + d.month() * 100 + d.day();

};

return 1;

};

//ouput operator

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

{

{

if (d.valid())

{

os << " " << long_Date(d) << " " ;

}

else

{

os << " invalid date ";

};

return os;

}

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