Morgan Stanley C++ Homework #1

Overview
Your assignment is to create a basic 'date' class. The class name should by xxDate, where xx are your initials, but in this document I will refer to the class as MGDate. You should have 4 files that you will hand in, printed out:

 mgdate.h  Header file for the date class
 mgdate.cpp  Source file for the date class
 mgdatetst.cpp  Test program for the date class
 Makefile  Make file, for creating the executable.


Your executable should be called mgdatetst.

Function List
The class should provide the following public member functions:

MGDate();
Constructor. Should initialize the data members of the class to contain the current date.

bool Set( int M, int D, int Y );
Should set the date object's month to M, it's day to D, and it's year to Y. If the M, D, and Y represent an invalid date (like 1-45-1999), then the function should not set any data members, and should return false. If they represent a valid date, then it should set the data members, and return true.

Note: If you run out of time, data validation is optional, and this function can always return true.

bool Set( const char* Src );
The Src string parameter is assumed to have a string containing a date, such as "1/1/1999" or "1-1-1999". This function should parse out the three numeric values for month, day, and year, then pass those values off to the Set function described above. The return of this function should be the return value of the called Set function.

void MGDate::Get( char* Dest )
Formats the date that the object holds into a string, and stores it at Dest. You can assume that Dest is large enough to hold the string (atleast 11 bytes). Use of sprintf is recommended.

void Output();
Outputs the date object in string format, to the console. Use cout within the function, to output the date held by the object in the format "m-d-y".

bool SetMonth( int M );
bool SetDay( int D );
bool SetYear( int Y );

These functions permit you to change the month, day, or year of a date, without effecting the other pieces of the date. If the date would be valid by making the change, then the change should not take place, and the function should return false, otherwise it should return true.

int GetMonth();
int GetDay();
int GetYear();

These functions permit you to extract the month, day, or year portion from a date object.

Note: These functions does not perform any sort of user input or output.

Data Members
It is suggested that you simply store the data members in a protected section of your class definition (in the .h file), and that you use 3 integers: m_M, m_D, and m_Y.

Suggested Stages of Development

 

Test Program

You should be able to use the following program to test your class. Please note that the user is not prompted for any input during the run of this program. This would be the mgdatetst.cpp file:

#include <iostream.h>
#include "mgdate.h"
char TestDate[]="1/1/1999";
void main()
{
    MGDate A, B, C;
    cout << "Testing Output function: ";
    A.Output();
    cout << endl;
    cout << "Testing bad month setting: ";
    if( A.SetMonth( 53 ) )
        cout << "Something went wrong " << endl;
    else
        cout << "Tested ok." << endl;
    cout << "Testing bad day setting: ";
    if( A.SetDay( 53 ) )
        cout << "Something went wrong " << endl;
    else
        cout << "Tested ok." << endl;
    cout << "Testing bad month setting: ";
    if( A.SetYear( -6 ) )
        cout << "Something went wrong " << endl;
    else
        cout << "Tested ok." << endl;
    cout << "Testing good date setting: ";
    if( A.SetMonth( 11 ) )
        cout << "Tested ok." << endl;
    else
        cout << "Something went wrong " << endl;
    cout << "Testing date set from string: ";
    if( A.Set( TestDate ) )
        cout << "Tested ok." << endl;
    else
        cout << "Something went wrong " << endl;
    A.SetMonth(1);
    cout << "Testing GetMonth: ";
    if( A.GetMonth() == 1 )
        cout << "Tested ok." << endl;
    else
        cout << "Something went wrong " << endl;

}