//****************************************************************** // IMPLEMENTATION FILE (date.cpp) // This file implements the Date class member functions //****************************************************************** #include "date.h" #include <iostream> #include <cstring> // For strcpy() and strlen() using namespace std; // Private members of class: // int mo; // int day; // int yr; // char* msg; //****************************************************************** Date::Date( /* in */ int initMo, /* in */ int initDay, /* in */ int initYr, /* in */ const char* msgStr ) // Constructor // Precondition: // 1 <= initMo <= 12 // && 1 <= initDay <= maximum number of days in month initMo // && 1582 < initYr // && msgStr is assigned // Postcondition: // mo == initMo && day == initDay && yr == initYr // && msg points to a duplicate of the msgStr string on the // free store { mo = initMo; day = initDay; yr = initYr; msg = new char[strlen(msgStr) + 1]; // Assert: // Storage for dynamic C string is now on free store // and its base address is in msg strcpy(msg, msgStr); // Assert: // Incoming string has been copied to free store } //****************************************************************** Date::~Date() // Destructor // Postcondition: // Array pointed to by msg is no longer on the free store { delete [] msg; } //****************************************************************** Date::Date( const Date& otherDate ) // Copy-constructor // Postcondition: // mo == otherDate.mo // && day == otherDate.day // && yr == otherDate.yr // && msg points to a duplicate of otherDate's message string // on the free store { mo = otherDate.mo; day = otherDate.day; yr = otherDate.yr; msg = new char[strlen(otherDate.msg) + 1]; strcpy(msg, otherDate.msg); } //****************************************************************** void Date::CopyFrom( /* in */ Date otherDate ) // Postcondition: // mo == otherDate.mo // && day == otherDate.day // && yr == otherDate.yr // && msg points to a duplicate of otherDate's message string // on the free store { mo = otherDate.mo; day = otherDate.day; yr = otherDate.yr; delete [] msg; // Deallocate the // original array msg = new char[strlen(otherDate.msg) + 1]; // Allocate a new // array strcpy(msg, otherDate.msg); // Copy the chars } //****************************************************************** void Date::Print() const // Postcondition: // Date and message have been output in the form // month day, year message // where the name of the month is printed as a string { switch (mo) { 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"; } cout << ' ' << day << ", " << yr << " " << msg; }