Date Class with Constructors

 

File Date.h:

#ifndef DATE_H
#define DATE_H

class Date
{
public:
   Date();
   Date(int,int,int);
   Date(Date&);
   void setDate(int,int,int);
   void showDate();
   void showWithName();
   int sameDate(Date);
private:
   int month;
   int day;
   int year;
};

#endif

File Date.C:

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

//  default constructor
Date::Date()
{
   month = 1;
   day = 1;
   year = 2000;
}

//  constructor that initializes all data members
Date::Date(int mo, int da, int yr)
{
   month = mo;
   day = da;
   year = yr;
}

//  copy constructor
Date::Date(Date& old)
{
   month = old.month;
   day = old.day;
   year = old.year;
}

//  set the data members of the invoking object
void Date::setDate(int mo, int da, int yr)
{
   month = mo;
   day = da;
   year = yr;
}

//  print the invoking object in the format mm/dd/yyyy
void Date::showDate()
{
   cout << month << '/' << day << '/' << year;
}

//  print the invoking object in the format monthname dd, yyyy
void Date::showWithName()
{
   switch (month)
   {
      case 1  : cout << "January ";
                break;
      case 2  : cout << "February ";
                break;
      case 3  : cout << "March ";
                break;
      case 4  : cout << "April ";
                break;
      case 5  : cout << "May ";
                break;
      case 6  : cout << "June ";
                break;
      case 7  : cout << "July ";
                break;
      case 8  : cout << "August ";
                break;
      case 9  : cout << "September ";
                break;
      case 10 : cout << "October ";
                break;
      case 11 : cout << "November ";
                break;
      case 12 : cout << "December ";
                break;
   }
   cout << day << ", " << year;
}

//  check whether two Dates are the same
int Date::sameDate(Date second)
{
   if (month == second.month && day == second.day && year == second.year)
      return 1;
   return 0;
}

File useDate.C:

//  test the date class

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

int main()
{
   Date today;
   Date birthday(1,21,1991);

   cout << "today contains the date ";
   today.showDate();
   cout << " (";
   today.showWithName();
   cout << ")" << endl << endl;
   cout << "birthday contains the date ";
   birthday.showDate();
   cout << " (";
   birthday.showWithName();
   cout << ")" << endl << endl;

   today.setDate(9,16,2003);
   cout << "now today contains the date ";
   today.showDate();
   cout << " (";
   today.showWithName();
   cout << ")" << endl << endl;

   Date another(today);
   cout << "another contains the date ";
   another.showDate();
   cout << " (";
   another.showWithName();
   cout << ")" << endl;

   int eq = today.sameDate(another);
   if (eq)
      cout << "another and today are the same\n";
   else
      cout << "another and today are not the same\n";
}

To Compile and Execute:

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

Output:

today contains the date 1/1/2000 (January 1, 2000)

birthday contains the date 1/21/1991 (January 21, 1991)

now today contains the date 9/16/2003 (September 16, 2003)

another contains the date 9/16/2003 (September 16, 2003)

another and today are the same

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

© Copyright Emmi Schatz 2002