In: Computer Science
C++
Define a MyDate class to represent a data that has 3 data members:
day, month, year. The class should have: A default constructor to
initialize the day, month, and year to 0. A constructor that
accepts three integers as arguments to initialize the day, month,
and year if the three arguments are valid. A copy constructor that
accepts a reference to a MyDate object as argument. This
constructor use the argument's three data fields to initialize the
day, month, and year. Overload the operator + : this function adds
a number of days to the calling object and return the result date.
Overload the operator -: this function accepts a MyDate object as
argument. It calculates the number of days difference between the
calling object and the argument object. Overload the operator -:
this function subtract a number of days to the calling object and
return the result date. Overload the opertor ==: this function
accepts a MyDate object as argument. It compares the calling object
with the argument object and returns true if they are the same
date, otherwise, the function returns false. Friend function to
overload operator >>: this function accepts an istream object
and a MyDate object as argument. In this function, it get the day,
month, and year as input and returns a MyDate object. Friend
function to overload operator <<: this function accepts an
ostream object and a MyDate object as argument. In this function,
it displays the day, month, and year of the MyDate object in
mm/dd/yyyy format.
// C++ program to create MyDate class
#include <iostream>
#include <iomanip>
using namespace std;
class MyDate
{
private:
int day, month, year;
static bool validDate(int day, int month, int year);
static int daysInMonth(int month, int year);
public:
MyDate();
MyDate(int day, int month, int year);
MyDate(const MyDate &other);
MyDate operator+(int days);
int operator-(const MyDate &other);
MyDate operator-(int days);
bool operator==(const MyDate &other);
friend ostream& operator<<(ostream &out, const MyDate &other);
friend istream& operator>>(istream &in, MyDate &other);
};
// helper function to validate date
bool MyDate::validDate(int day, int month, int year)
{
if(month == 1 || month == 3 || month == 5 || month == 7 || month == 8 || month == 10 || month == 12)
{
if(day < 1 || day > 31)
return false;
else
return true;
}else if(month == 4 || month == 6 || month == 9 || month == 11)
{
if(day < 1 || day > 30)
return false;
else
return true;
}else if(month == 2)
{
if((year%400 == 0 ) || ((year%4 == 0) && (year%100) != 0))
{
if(day < 1 || day > 29)
return false;
else
return true;
}else
{
if(day < 1 || day > 28)
return false;
else
return true;
}
}else
return false;
}
// helper function to return the days in the month
int MyDate::daysInMonth(int month, int year)
{
if(month == 2)
{
if((year%400 == 0) || ((year % 4 == 0) && (year % 100 != 0)))
return 29;
else
return 28;
}else if(month == 1 || month == 3 || month == 5 || month == 7 || month == 8 || month ==10 || month==12)
return 31;
else
return 30;
}
// default constructor
MyDate ::MyDate() : day(0), month(0), year(0)
{}
// parameterized constructor
MyDate ::MyDate(int day, int month, int year)
{
this->day = day;
this->month = month;
this->year = year;
if(!validDate(day,month,year))
{
this->day = 0;
this->month = 0;
this->year = 0;
}
}
// copy constructor
MyDate::MyDate(const MyDate &other) : day(other.day), month(other.month), year(other.year)
{}
// method to add days to this data and return the resultant date
MyDate MyDate:: operator+(int days)
{
MyDate newDate(month,day,year);
newDate.day += days;
while(newDate.day > daysInMonth(newDate.month, newDate.year))
{
newDate.day -= daysInMonth(newDate.month,newDate.year);
newDate.month++;
if(newDate.month > 12)
{
newDate.year++;
newDate.month=1;
}
}
return newDate;
}
// method to return the number of days between this date and other date (other date - this date)
int MyDate:: operator-(const MyDate &other)
{
int days = 0;
for(int i=year+1;i<other.year;i++)
{
if((i%400 == 0) || ((i%4 == 0) && (i%100 != 0)))
days += 366;
else
days += 365;
}
if(year == other.year)
{
for(int i=month+1;i<other.month;i++)
days += daysInMonth(i,year);
days += daysInMonth(month,year) - day;
days += other.day;
}else
{
for(int i=month+1;i<=12;i++)
days += daysInMonth(i,year);
days += daysInMonth(month,year) - day;
for(int i=1;i<other.month;i++)
days += daysInMonth(i,other.year);
days += other.day;
}
return days;
}
// method to subtract days from this date and return the resultant date
MyDate MyDate:: operator-(int days)
{
MyDate newDate(month,day,year);
while(days > 0)
{
if(days < newDate.day)
{
newDate.day -= days;
days = 0;
}else
{
newDate.month--;
if(newDate.month < 1)
{
newDate.month = 12;
newDate.year--;
}
days -= newDate.day;
newDate.day = daysInMonth(newDate.month, newDate.year);
}
}
return newDate;
}
// method to check if the this date is equal to other date
bool MyDate:: operator==(const MyDate &other)
{
return((year == other.year) && (month == other.month) && (day == other.day));
}
// method to output the date as mm/dd/yyyy
ostream& operator<<(ostream &out, const MyDate &other)
{
out<<setw(2)<<setfill('0')<<other.month<<"/"<<setw(2)<<setfill('0')<<other.day<<"/"<<other.year;
return out;
}
// method to read the date in the format mm/dd/yyyy
istream& operator>>(istream &in, MyDate &other)
{
char ch;
in>>other.month>>ch>>other.day>>ch>>other.year;
return in;
}
//end of MyDate class