Date Class

 

File Date.h:

#ifndef DATE_H
#define DATE_H

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

#endif

File Date.C:

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

//  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;
}

File useDate.C:

//  test the date class

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

int main()
{
   int mon;
   int da;
   int yr;
   Date today;
   Date birthday;

   cout << "Enter today's month, day, and year: ";
   cin >> mon >> da >> yr;
   today.setDate(mon,da,yr);
   cout << "Enter your birthday (month, day, and year): ";
   cin >> mon >> da >> yr;
   birthday.setDate(mon,da,yr);

   cout << endl << "For today's date you entered ";
   today.showDate();
   cout << " (";
   today.showWithName();
   cout << ")" << endl << endl;
   cout << "For your birthday you entered ";
   birthday.showDate();
   cout << " (";
   birthday.showWithName();
   cout << ")" << endl;
}

To Compile and Execute:

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

Output:

Enter today's month, day, and year: 2 9 2006
Enter your birthday (month, day, and year): 4 23 1984

For today's date you entered 2/9/2006 (February 9, 2006)

For your birthday you entered 4/23/1984 (April 23, 1984)


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

© Copyright Emmi Schatz 2002