20-CS-122-001 Computer Science II Spring 2012
A Date Class

Virtual functions, classes, inheritance, lists, queues, stacks, applications

Prev      Next      All Lectures      Source      Java version

//A Date Class 
#include <iostream>
#include <math.h>
#include <stdlib.h>
using namespace std;

int max (int a, int b) { if (a > b) return(a); else return (b); }

int min (int a, int b) { if (a > b) return(b); else return (a); }

class Date {
    int month, day, year;
   
 public:
    Date ();                           // Constructor
    Date ( int mn, int day, int yr);   // Constructor
    void display ();                   // Method to display date
    int  getMonth ();                  // Get month of date
    void setMonth (int mn);            // Set month of date
    int  daysSoFar ();                 // Number of days since first day of year
    ~Date ();                          // Destructor
};

// Constructor definitions
Date::Date () {
   month = day = year = 1;
   cout << "Creating " << this << " ("; display(); cout << ")\n";
}

Date::Date (int mn, int dy, int yr) {
   static int length[] = { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };

   month = max(1, mn);
   month = min(month,12);

   day = max(1,dy);
   day = min(day, length[month]);

   year = max(1, yr);
   cout << "Creating " << this << " ("; display(); cout << ")\n";
}

// Destructor definition
Date::~Date() {
   cout << "Terminating " << this << " ("; display(); cout << ")\n";
}

// Other methods
void Date::display() {
   static char *name[] = {"nothing", "January", "February", "March", "April",
            "May", "June", "July", "August", "September", "October",
            "November", "December" };

   cout << name[month] << ' ' << day << ", " << year ;
}

int Date::daysSoFar() {
   int total = 0;

   static int length[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };

   for (int i=1 ; i < month ; i++) total += length[i];
   total += day;
   return total;
}

int Date::getMonth() {
  return month;
}

void Date::setMonth(int mn) {
   month = max(1, mn);
   month = min(month, 12);
}

int main() {
   cout << "Declare date1 01/02/1999:\t";
   Date date1(1,  2, 1999);
   cout << "Declare date2 04/12/1999:\t";
   Date date2(4, 12, 1999);
   cout << "Declare date3 01/01/0001:\t";
   Date date3(1,  1,    1);
   cout << "Declare date4  (no date):\t";
   Date date4;
   Date *date5;
   Date *date6;

   cout << "Create  date5 45/45/4455:\t";
   date5 = new Date(45, 45, 4455);

   cout << "Show date1:\t";
   date1.display();  cout << " (" << date1.daysSoFar()  << ")\n";
   cout << "Show date2:\t";
   date2.display();  cout << " (" << date2.daysSoFar()  << ")\n";
   cout << "Show date5:\t";
   date5->display(); cout << " (" << date5->daysSoFar() << ")\n";

   cout << "Show Month of date5:\t" << date5->getMonth() << '\n';
   cout << "Do date5->setMonth(4):\n";
   date5->setMonth(4);
   cout << "Show Month of date5:\t" << date5->getMonth() << '\n';

   cout << "Delete date5:\t";
   delete(date5);
   cout << "Create  date6 01/01/0001:\t";
   date6 = new Date(1,1,1);

   cout << "Show Month of date5:\t" << date5->getMonth() << '\n';
   cout << "Do date5->setMonth(4):\n";
   date5->setMonth(4);
   cout << "Show Month of date5:\t" << date5->getMonth() << '\n';

   cout << "Show date5:\t";
   date5->display(); cout << " (" << date5->daysSoFar() << ")\n";
   cout << "Show date4:\t";
   date4.display();  cout << " (" << date4.daysSoFar()  << ")\n";

   cout << "Do date3.setMonth(3):\n";
   date3.setMonth(3);
   cout << "Delete date6:\t";
   delete(date6);
}

// Sample Output:
//   Declare date1 01/02/1999:       Creating 0xbffff80c (January 2, 1999)
//   Declare date2 04/12/1999:       Creating 0xbffff800 (April 12, 1999)
//   Declare date3 01/01/0001:       Creating 0xbffff7f4 (January 1, 1)
//   Declare date4  (no date):       Creating 0xbffff7e8 (January 1, 1)
//   Create  date5 45/45/4455:       Creating 0x804a990 (December 31, 4455)
//   Show date1:     January 2, 1999 (2)
//   Show date2:     April 12, 1999 (102)
//   Show date5:     December 31, 4455 (365)
//   Show Month of date5:    12
//   Do date5->setMonth(4):
//   Show Month of date5:    4
//   Delete date5:   Terminating 0x804a990 (April 31, 4455)
//   Create  date6 01/01/0001:       Creating 0x804a990 (January 1, 1)
//   Show Month of date5:    1
//   Do date5->setMonth(4):
//   Show Month of date5:    4
//   Show date5:     April 1, 1 (91)
//   Show date4:     January 1, 1 (1)
//   Do date3.setMonth(3):
//   Delete date6:   Terminating 0x804a990 (April 1, 1)
//   Terminating 0xbffff7e8 (January 1, 1)
//   Terminating 0xbffff7f4 (March 1, 1)
//   Terminating 0xbffff800 (April 12, 1999)
//   Terminating 0xbffff80c (January 2, 1999)