Homework #2

NOTE: To avoid conflicts with the real Morgan Stanley toolkit, place make sure that all class names re prefixed with MSCU, not just MS. Therefore, MSObject should be changed to MSCUObject. This change takes place from now on. Also, please note that the return type for GetType has changed from a char* to a string type.
Solution to last weeks assignment:

The MSCUObject class definition, in the mscui.h file:

class MSCUObject
{
public:
#ifndef MSNDEBUG
MSCUObject() { MSCUObjectCount++; }
~MSCUObject() { MSCUObjectCount--; }
#endif
const virtual string GetType() const { return( "MSObject" ); }

virtual void SaveToFile(ostream& O) const { throw "Unimplemented feature"; }
virtual void LoadFromFile(istream& i) { throw "Unimplemented feature";}

virtual void SetFromString( string& Src ) { throw "Unimplemented feature"; }
virtual string GetAsString() const { throw "Unimplemented feature"; return(0);}

#ifndef MSNDEBUG
static int GetCount() { return( MSCUObjectCount ); }
#endif
private:
#ifndef MSNDEBUG
static int MSCUObjectCount;
#endif
};

ostream& operator<<( ostream& O, const MSCUObject& MSO );
In the .cpp file:
#ifndef MSNDEBUG
int MSCUObject::MSCUObjectCount;
#endif

ostream& operator<<( ostream& O, const MSCUObject& MSO )
{
O << MSO.GetAsString().c_str();
return( O );
}

 

This week's assignment:
#1) Review the MSCUDate class below. Finish the class by adding the SetFromString, LoadFromFile, and the following operators: != < <= > >=. You should also write the following new functions, specific to this class:
string GetMonthName()
int GetMonth();
int GetDay();
int GetYear();

class MSCUDate : public MSCUObject
{
public:
MSCUDate()
{
if( !InitToday )
{
M = D = 0;
Y = 0;
}
else
{
time_t Time;
struct tm* TMStruct;
time(&Time);
TMStruct = localtime(&Time);
Y = TMStruct->tm_year;
D = TMStruct->tm_mday;
M = TMStruct->tm_mon+1;
}
}

int operator==( MSCUDate& Other )
{
return( Other.M==M && Other.D==D && Other.Y==Y );
}
virtual void SaveToFile( ostream& O )
{
O.write( (char*)&Y, sizeof(Y) );
O.write( &M, sizeof(M) );
O.write( &D, sizeof(D) );
}
virtual string GetAsString() const
{
char Tmp[11];
if( FourDigit )
sprintf( Tmp, "%02d%c%02d%c%04d", M, Sep, D, Sep, Y );
else
sprintf( Tmp, "%02d%c%02d%c%02d", M, Sep, D, Sep, Y );
return( Tmp ); // Note we return char*, not string… why does this work?
}

static bool InitTodaysDate( bool Mode )
{
bool Ret; Ret = InitToday; InitToday=Mode; return( Ret );
}
static bool Output4Digits( bool Mode )
{
bool Ret; Ret = FourDigit; FourDigit=Mode; return( Ret );
}
static char Seperator( char NewSep )
{
char Ret; Ret = NewSep; Sep=NewSep; return( Ret );
}
private:
static bool InitToday, FourDigit;
static char Sep;
char M, D;
short int Y;
};
bool MSCUDate::InitToday=true;
bool MSCUDate::FourDigit=true;
char MSCUDate::Sep='-';

#2) Using the above example, Create a class called MSCUSSN, which should be designed to implement a social security number class. The internal data storage format is up to you. The GetAsString function should return the ssn as "001-02-0003"