This assignment is due June 2nd. It must be handed in on both disk and print out.
Your are going to write 3 classes, all detailed here, as well as a test program for the classes (A sample start is provided at the end of this document). The classes are an MObject class, which is what we discussed as a 'super base class' during classtime. It will serve as the base class to 2 other classes, MDate and MTime. This assignment will make use of a variety of topics discussed during class, as well as information from the class booklet.
int IsValid(void) const =0;
Determines if the object is valid (ie, errors such as an invalid
value is set). Public function.
char * GetObjectName( void ) const =0;
Returns the object's class name as a string. Make this a virtual
function. Public function.
From this class, you must create the following classes: MDate and MTime
Make sure that the sub-classes (MDate and MTime) implement the functions above as well. Use inline functions when possible, but don't make them too large.
For these two classes, which are detailed below, the << and >> operators must be overloaded with the ostream and istream classes, and work with the ofstream and ifstream classes.
A class designed to hold date information. Month, Day, and Year. Must be able to maintain a year rang from atleast 1970 to 2038. The following items are required:
Constructors:
Using a string in the format "mm/dd/yy". If the date
string is invalid, assign the value anyway.
Using another MDate (this is a copy constructor) If the other
MDate is invalid, assign the value anyway.
Operators:
== determines if to dates are equal (returns 0 if they are equal)
++ Adds one day. Date must remain valid after.
-- Subtracts one day. Date must remain valid after.
<< and >> (for ostream, ofstream, and work with istream,
and ifstream)
Functions:
int IsValid(void) const =0;
Determines if the MDate is valid (ie, a bad constructor string
was passed). Public function. Note: This assumes that the MDate
can hold an invalid date value, which is something we have tried
to avoid during our class discussions, but is OK in this assignment.
char *GetObjectName(void) const;
Returns the text "MDate".
void SetFormat( int Character);
Specifies the character to be used to separate month/day/year.
This character will default to '/', but may be changed to '-',
or any other single character with this function. Once changed,
all subsequent output should use the new format character. Note:
Setting the format for 1 MDate object, will effect all MDate objects.
char * GetMonthName(void) const;
Returns the month name, such as "January".
Note that real date arithmetic is not needed. For instance, you need not keep track of leap years. You must however properly maintain the month and year, as days are added and subtracted (for example, subtracting one day from 1/1/96 should leave 12/31/95 in the date. You can assume that every year is a leap year, in other words, just make believe February always has 29 days.
Note: The operator<< and operator>> for this class should output the date in m/d/y format, unless the MDate object is invalid, in which case it should output "*Invalid*".
A class dedicated to time. Maintains hour, minute, and second.
Constructors:
Using a string in the format "hh:mm:ss". If the string
is an invalid time, assign the value anyway.
Using another MTime. If the other MTime is an invalid time, assign
the value anyway.
Operators:
== determines if two times are equal (returns 0 if they are equal)
++ Adds one second
-- subtracts one second
<< and >> (for ostream, ofstream, and works with istream,
and ifstream)
Functions:
int IsValid(void) const =0;
Determines if the MTime is valid (ie, a bad constructor string
was passed). Public function. Note: This assumes that the MDate
can hold an invalid date value, which is something we have tried
to avoid during our class discussions, but is OK in this assignment.
char *GetObjectName(void) const;
Returns the text "MTime".
void SetFormat( int AMPM);
Specifies whether output from the class is in AMPM (1:00:00 AM)
or 24 hour (01:00:00) format The default should be AMPM. Once
changed, all subsequent output should use the new format. Note:
Changing the format for one MTime object, will affect all other
MTime objects.
Note: Unlike MDate, this class must properly maintain the time (mathematically). For example, if you subtract 1 second from 2:00:00, it should become 1:59:59. No negative values should be found.
Note: The operator<< and operator>> for this class should output the date in hh:mm:ss format, unless the MDate object is invalid, in which case it should output "*Invalid*".
The following is a suggestion for a test program to test your class. Please add to this test program the needed functionality to test all your functions. For example, there is no MTime testing code below, so I expect you to add it.
#include <iostream.h>
#include <fstream.h>
#include "mid.h"
void WhatIsIt( MObject * X )
{
cout << "It is " << X->GetObjectName()
<< endl;
}
void main()
{
MDate A( "4/10/64" );
MDate B;
MDate C("1/0/0");
B = A;
cout << B << endl;
A = ++B;
cout << "Test pre-increment " << A <<
endl << B << endl;
B.SetFormat('-');
cout << "Test addition and SetFormat "<<
B << endl;
cout << "Enter date: ";
cin >> C;
WhatIsIt( &B );
cout << B << endl << C << endl;
if( B==C )
cout << "B and C are equivalent" << endl;
else
cout << "B and C are different" << endl;
B = MDate("5/23/99");
cout << B << " ";
B++;
cout << B << " ";
B--;
cout << B << endl;
}