Date Class with Operators and Friends

 

File Date.h:

#ifndef DATE_H
#define DATE_H

#include <iostream.h>

class Date
{
public:
//  friend functions
   friend ostream& operator<<(ostream&,const Date&);
   friend istream& operator>>(istream&,Date&);
   friend Date operator+(const Date&,int);
   friend Date operator+(int,const Date&);
//  constructors
   Date() :  month(1), day(1), year(2000) { }
   Date(int mm,int dd,int yy=2000) : month(mm), day(dd), year(yy) { }
   Date(const Date &old) : month(old.month), day(old.day),
                                             year(old.year) { }
//  mutators
   void setDate(int mm,int dd,int yy=2003) { month = mm; day = dd;
                                             year = yy; }
//  accessors
   void print() const;
   int end_of_month(int) const;
//  comparison operators (accessors)
   int operator==(const Date &) const;
   int operator!=(const Date &) const;
   int operator<(const Date &) const;
   int operator<=(const Date &) const;
   int operator>(const Date &) const;
   int operator>=(const Date &) const;
private:
   int month;
   int day;
   int year;
   static int days[];
   void increment();
};

#endif

File Date.C:

#include "Date.h"
#include <iostream.h>

int leap_year(const int);

//******************************************************************//
//  initialize array that holds the number of days each month

int Date::days[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};

//******************************************************************//
//  return true if the date is the last day of the month

int Date::end_of_month(int dd) const
{
   if (month == 2 && leap_year(year))
      return dd == 29;
   else
      return dd == days[month];
}

//******************************************************************//
//  move the date to the following day

void Date::increment()
{
   if (end_of_month(day) && month == 12)
   {
      day = 1;
      month = 1;
      year++;
   }
   else
      if (end_of_month(day))
      {
	 day = 1;
	 month++;
      }
      else
	 day++;
}

//******************************************************************//
//  test whether two dates are equal

int Date::operator==(const Date& second) const
{
   if (day == second.day && month == second.month
                                      && year == second.year)
      return 1;
   else
      return 0;
}

//******************************************************************//
//  test whether two dates are unequal

int Date::operator!=(const Date& second) const
{
   if (day != second.day || month != second.month
                                      || year != second.year)
      return 1;
   else
      return 0;
}

//******************************************************************//
//  test whether the first date is less than the second date

int Date::operator<(const Date& second) const
{
   if (year < second.year)
      return 1;
   if (year == second.year && month < second.month)
      return 1;
   if (year == second.year && month == second.month
                                        && day < second.day)
      return 1;
   return 0;
}

//******************************************************************//
//  test whether first date is less than or equal to second date

int Date::operator<=(const Date& second) const
{
   if (year < second.year)
      return 1;
   if (year == second.year && month < second.month)
      return 1;
   if (year == second.year && month == second.month
                                        && day <= second.day)
      return 1;
   return 0;
}

//******************************************************************//
//  test whether the first date is greater than the second date

int Date::operator>(const Date& second) const
{
   if (year > second.year)
      return 1;
   if (year == second.year && month > second.month)
      return 1;
   if (year == second.year && month == second.month
                                        && day > second.day)
      return 1;
   return 0;
}

//******************************************************************//
//  test whether first date is greater than or equal to second date

int Date::operator>=(const Date& second) const
{
   if (year > second.year)
      return 1;
   if (year == second.year && month > second.month)
      return 1;
   if (year == second.year && month == second.month
                                        && day >= second.day)
      return 1;
   return 0;
}

//******************************************************************//
//  print the invoking object

void Date::print() const
{
   cout << month << "/" << day << "/" << year;
}

//******************************************************************//
//  determine whether the given year is a leap year

int leap_year(const int yy)
{
   if (yy % 400 == 0 || (yy % 100 != 0 && yy % 4 == 0))
      return 1;     //  leap year
   else
      return 0;     //  not a leap year
}

//******************************************************************//
//  overloaded operator << to allow output of dates with ostream object

ostream& operator<<(ostream& ostr, const Date& d)
{
   ostr << d.month << "/" << d.day << "/" << d.year;
   return ostr;
}

//******************************************************************//
//  overloaded operator >> to allow input of dates with istream object

istream& operator>>(istream& istr, Date& d)
{
   istr >> d.month;
   istr.ignore(1);
   istr >> d.day;
   istr.ignore(1);
   istr >> d.year;
   return istr;
}

//******************************************************************//
//  add a specific number of days and the invoking object

Date operator+(int numdays,const Date& dd)
{
  Date newdate = dd;
  for (int i = 1 ; i <= numdays ; i++)
    newdate.increment();
  return newdate;
}

//******************************************************************//
//  add the invoking object and a specific number of days

Date operator+(const Date& dd,int numdays)
{
  Date newdate = dd;
  for (int i = 1 ; i <= numdays ; i++)
    newdate.increment();
  return newdate;
}

File main.C:

#include <iostream.h>
#include "Date.h"

//******************************************************************//
//  test the date class

void main()
{
   Date d1,d2(10,26,2003),d3;
   d3.setDate(12,19,2003);
   cout << endl << endl << "d1 = ";
   d1.print();
   cout << endl << "d2 = ";
   d2.print();
   cout << endl << "d3 = ";
   d3.print();
   d2 = d2 + 7;
   cout << endl << "Add 7 days to d2:" << endl << "d2 = " << d2;
   d2 = 7 + d2;
   cout << endl << "Add 7 days to d2:" << endl << "d2 = " << d2;
   Date d4(10,31,2003);
   cout << endl << "d4 = " << d4 << endl;
   if (d2 == d4)
      cout << "d2 and d4 are equal" << endl;
   if (d4 < d2)
      cout << "d4 is less than d2" << endl;
   if (d2 > d4)
      cout << "d2 is greater than d4" << endl;
   if (d4 <= d2)
      cout << "d4 is less than or equal to d2" << endl;
   if (d2 >= d4)
      cout << "d2 is greater than or equal to d4" << endl;
   if (d4 == d4)
      cout << "d4 is equal to itself" << endl;
   if (d2 != d4)
      cout << "d2 and d4 are not equal" << endl;

   cout << "Enter two dates: ";
   cin >> d2 >> d4;
   cout << "The dates were: " << d2 << " and " << d4 << endl;
}

To Compile and Execute:

>g++ Date.C main.C
>a.out

Output:


> a.out

d1 = 1/1/2000
d2 = 10/26/2003
d3 = 12/19/2003
Add 7 days to d2:
d2 = 11/2/2003
Add 7 days to d2:
d2 = 11/9/2003
d4 = 10/31/2003
d4 is less than d2
d2 is greater than d4
d4 is less than or equal to d2
d2 is greater than or equal to d4
d4 is equal to itself
d2 and d4 are not equal
Enter two dates: 12/19/2003 11 29 2003
The dates were: 12/19/2003 and 11/29/2003


Email Me | Office Hours | My Home Page | Department Home | MCC Home Page

© Copyright Emmi Schatz 2002