Source of date4.cpp


  1: // Filename: DATE4.CPP
  2: // Purpose:  Implementation file corresponding to DATE4.H.

  4: #include "date4.h"
  5: #include <iostream>
  6: #include <string.h>   // For strcpy() and strlen()

  8: // Private members of class:
  9: //     int   month;
 10: //     int   day;
 11: //     int   year;
 12: //     char* messagePtr;


 15: //******************************************************************
 16: Date::Date()
 17: {
 18:     month = 1;
 19:     day = 1;
 20:     year = 1970;
 21:     messagePtr = new char[strlen("No message ...") + 1];
 22:     strcpy(messagePtr, "No message ...");
 23: }


 26: //******************************************************************
 27: Date::Date(/* in */ int         newMonth,
 28:            /* in */ int         newDay,
 29:            /* in */ int         newYear,
 30:            /* in */ const char* messageString)
 31: {
 32:     month = newMonth;
 33:     day = newDay;
 34:     year = newYear;
 35:     messagePtr = new char[strlen(messageString) + 1];
 36:     strcpy(messagePtr, messageString);
 37: }


 40: //******************************************************************
 41: void Date::Display() const
 42: {
 43:     static char monthString[12][10] =
 44:     {
 45:         "January", "February", "March", "April", "May", "June",
 46:         "July", "August", "September", "October", "November",
 47:         "December"
 48:     };

 50:     cout << monthString[month-1] << ' ' << day << ", " << year << endl
 51:          << messagePtr << endl;
 52: }


 55: //******************************************************************
 56: Date::Date(const Date& otherDate)
 57: {
 58:     month = otherDate.month;
 59:     day = otherDate.day;
 60:     year = otherDate.year;
 61:     messagePtr = new char[strlen(otherDate.messagePtr) + 1];
 62:     strcpy(messagePtr, otherDate.messagePtr);
 63:     cout << "Now in copy constructor for Date ..." << endl;
 64:     Pause();
 65: }


 68: //******************************************************************
 69: void Date::operator=(const Date& rightSide)
 70: {
 71:     month = rightSide.month;
 72:     day = rightSide.day;
 73:     year = rightSide.year;
 74:     int newLength = strlen(rightSide.messagePtr);
 75:     if (newLength > strlen(messagePtr))
 76:     {
 77:         delete [] messagePtr;
 78:         messagePtr = new char[newLength+1];
 79:     }
 80:     strcpy(messagePtr, rightSide.messagePtr);
 81:     cout << "Now in overloaded = operator for Date ..." << endl;
 82:     Pause();
 83: }


 86: //******************************************************************
 87: Date::~Date()
 88: {
 89:     delete [] messagePtr;
 90:     cout << "Now in the destructor for class Date ... " << endl;
 91:     Pause();  cout << endl;
 92: }


 95: //******************************************************************
 96: void Date::InsertXX()
 97: {
 98:     messagePtr[0] = 'X';
 99:     messagePtr[1] = 'X';
100: }