Header file
Function fileCode:#ifndef STRING_HPP #define STRING_HPP #include <iostream> using namespace std; /* mystring class */ class mystring { public: typedef struct STRING* String; /*String struct */ struct STRING { int size; char *theString; }; void validateString( mystring& theString ); mystring() { String->theString = NULL; String->size =0; } mystring( const char *init) { newString( init ); } ~mystring(mystring& theString) { destroyString( theString ); } mystring& destroyString( mystring& theString ); mystring& newString( char* theString ); char* charAt( mystring& theString, int index ); void printString( mystring& theString); friend mystring& operator+( mystring& string1, mystring& string2 ); }; #endif
Code:#include <iostream.h> #include <assert.h> #include "mystring.hpp" using namespace std; int stringCount = 0; /*Keep track of string count */ /*------------------------------------------------------ newString PURPOSE: Create a new string struct INPUT PARAMETERS:input string OUTPUT PARAMETERS: string object ------------------------------------------------------ */ mystring& mystring::newString( const char* init ) { String *newString = NULL; assert( init != NULL ); newString = new String[init]; assert( newString != NULL ); if ( newString != NULL ) { newString->theString = new char( strlen(init) + 1 ); /*Assign array size of the new string object*/ assert( newString -> theString != NULL ); if ( newString -> theString != NULL ) { strcpy( newString -> theString, init );/*Copy input string to string component of the struct */ newString -> size = strlen( newString -> theString );/*Assign size of the String object */ assert( newString -> size == strlen( init ) ); stringCount++; } else { delete newString ; newString = NULL; } } return( newString ); } /*------------------------------------------------------ validateString PURPOSE: To check if there is any content in the object or empty object */ INPUT PARAMETERS:string object OUTPUT PARAMETERS: void ------------------------------------------------------ */ void mystring::validateString( mystring& theString ) { assert( theString != NULL ); assert( theString -> theString != NULL ); assert( theString -> theString[theString -> size] == '\0' ); } /*------------------------------------------------------ destroyString PURPOSE: Destroys the string struct INPUT PARAMETERS:string object OUTPUT PARAMETERS: string object ------------------------------------------------------ */ mystring& mystring::destroyString( mystring& theString ) { validateString( theString ); assert( stringCount > 0 ); delete theString -> theString ; /*Delete String component of the struct */ delete theString ; /*Delete the object itself */ theString = NULL; stringCount--; return( theString ); } char* mystring::charAt( mystring& theString, int index ) { validateString( theString ); assert( index >= 0 ); assert( index < theString -> size ); return( theString -> theString[index] ); } /*------------------------------------------------------ operator+ PURPOSE: Operator overloading of the operator + INPUT PARAMETERS:string object OUTPUT PARAMETERS: new string object which has contents of string1 and 2 ------------------------------------------------------ */ mystring& mystring::operator+( mystring& string1, mystring& string2 ) { String *newString = NULL; validateString( string2 ); newString = new String[]; assert( newString != NULL ); if ( newString != NULL ) { newString -> size = this. size + string2 -> size; newString -> theString = new char [newString -> size + 1]; assert( newString -> theString != NULL ); if ( newString -> theString != NULL ) { strcpy( newString -> theString, string1 -> theString ); strcpy( &(newString -> theString[string1 -> size]), string2 -> theString ); assert( newString -> size == strlen( newString -> theString ) ); assert( newString -> theString[0] == this. theString[0] ); assert( newString -> theString[this. size] == string2 -> theString[0] ); stringCount++; } else { delete newString ; newString = NULL; } } return( newString ); } /*------------------------------------------------------ printString PURPOSE: Print the contents of the string object INPUT PARAMETERS: string onject OUTPUT PARAMETERS: void ------------------------------------------------------ */ void mystring::printString( mystring& theString ) { validateString( theString ); cout << theString->size<<endl; cout << theString -> theString << endl ; }



LinkBack URL
About LinkBacks


