Date Class implementation

this is a follow up post of Date class header file code and have the actual implementation of Date class in C++:

#include “Date.h”

// initializing the no of days, take 0 for month zero.
const short Date::daysInMonth[] = {0,31,28,31,30,31,30,31,31,30,31,30,31};
// creating and initializing arrey for giving a alfabatical output.
const char *mArray [] = {“*”,”Jan”, “Feb”, “Mar”, “Apr”, “May”, “Jun”, “Jul”, “Aug”, “Sep”, “Oct”, “Nov”, “Dec”};

Date::Date(short d, short m , short y)
{
setDate(d,m,y);
}

void Date::display()
{
std::cout << “\nDate: ” << day << “-” << mArray[month] << “-” << year;
}

void Date::setDate (short d, short m, short y)
{
year = y;
//if month is wrong then set it to 1
if (month < 1 && month > 12)
month = 1;
else
month = m;
//if day is wrong then set it to 1
if (month == 2 && leapYear(y))
if (d >= 1 && d <=29)
day = d;
else
day = 1;
else
if (d >=1 && d <= daysInMonth[month])
day = d;
else
day = 1;
}

short Date::daysOfMonth(Date d)
{
if (d.month == 2 && leapYear(d.year)) //if leap year then Feb is 29 days.
return 29;
else
return daysInMonth [d.month];
}

bool Date::leapYear(short y)
{
if ((y%400 == 0)|| (y%100 !=0 && y%4 ==0))
return true;
else
return false;
}

Date Date::operator-(short numberOfDays)
{
for (short i = 1; i <= numberOfDays; i++)
{
–(*this);
}
return *this;
}

Date operator -(short num, Date rhs)
{
for (short i =1; i <= num ; i++)
{
–rhs;
}
return rhs;
}

short Date::operator – (Date a)
{
short numDays =0;
Date rhs, lhr;

if ((*this) > a)
{
rhs = (*this);
lhr = a;
}
else
{
rhs = a;
lhr = (*this);
}

while (lhr != rhs)
{
–rhs;
numDays++;
}

return numDays;
}

Date Date::operator+(short numberOfDays)
{
for (short i =1; i <= numberOfDays; i++)
{
++(*this);
}
return *this;
}

Date Date::operator++()
{
if (day== daysOfMonth(*this) && month == 12) //end of year
{
day = 1;
month = 1;
++year;
}
else if (day == daysOfMonth(*this)) //last day of month
{
day = 1;
++month;
}
else //not the last day of the month
{
day++;
}
return *this;
}

Date Date::operator–()
{
if (day == 1 && month == 1) //start of year
{
day = 31;
month = 12;
–year;
}
else if (day == 1) //first day of month
{
–month;
day = daysOfMonth(*this);
}
else // not the first day of month
{
day–;
}
return *this;
}

Date Date::operator+=(short a)
{
for (short i =0; i <=a; i ++)
{
++(*this);
}
return *this;
}

Date Date::operator -= (short a)
{
(*this)-a;
return *this;
}

short Date::operator -=(Date rhs)
{
short numDays= 0;
numDays = (*this) – rhs;
return numDays;
}

bool Date::operator <(Date rhs)
{
if (year < rhs.year)
{
return true;
}
else if (month < rhs.month)
{
return true;

if (day < rhs.day)
{
return true;
}else
return false;
}
else
{
return false;
}
}

bool Date::operator >(Date rhs)
{
if (year > rhs.year)
{
return true;
}
else if (month > rhs.month)
{
return true;

if (day > rhs.day)
{
return true;
}else
return false;
}
else
{
return false;
}
}

bool Date::operator <=(Date rhs)
{
if (year <= rhs.year)
{
return true;
}
else if (month <= rhs.month)
{
return true;

if (day <= rhs.day)
{
return true;
}else
return false;
}
else
{
return false;
}
}

bool Date::operator >=(Date rhs)
{
if (year >= rhs.year)
{
return true;
}
else if (month >= rhs.month)
{
return true;

if (day >= rhs.day)
{
return true;
}else
return false;
}
else
{
return false;
}
}

bool Date::operator ==(Date rhs)
{
if (year == rhs.year && month == rhs.month && day == rhs.day)
{
return true;
}
else
{
return false;
}
}

bool Date::operator !=(Date rhs)
{
if (year == rhs.year && month == rhs.month && day == rhs.day)
{
return false ;
}
else
{
return true;
}
}

std::ostream& operator << (std::ostream& leftOpr, Date & rhs)
{
leftOpr << rhs.day << “-” << mArray[rhs.month] << “-” << rhs.year;

return leftOpr;
}

std::istream& operator >> (std::istream& leftOpr, Date & rhs)
{
{
leftOpr >> rhs.day ;
leftOpr.ignore();
leftOpr >> rhs.month;
leftOpr.ignore();
leftOpr >> rhs.year;
}
return leftOpr;
}

Advertisements

About Khuram Ali

Programming... Programming and Programming...!!!

Posted on March 22, 2013, in C++, Programming. Bookmark the permalink. Leave a comment.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: