Homework #3

Your assignment this week is to extend your flString class to be a more practical and re-usable class. You must make the following changes to your class:

const MGString& operator=( const char* Src );
const MGString& operator=( const MGString& Src );
Assignment operators. Sets the this object to contain the Src string, after releasing any previously-help memory. Returns the this object.

int Compare( const char* Src, bool Sensitive=false ) const;
Comparison function. Returns <0 if this is less than Src, 0 if this equals Src, or >0 if this is greater than Src. The Sensitive parameter determines of comparison is case sensitive or not.

bool operator==( const char* Src ) const;
bool operator!=( const char* Src ) const;
bool operator>( const char* Src ) const;
bool operator>=( const char* Src ) const;
bool operator<( const char* Src ) const;
bool operator<=( const char* Src ) const;

Comparison operators. Returns true if operation is true, false if not.

operator const char* () const { return( GetCPointer() ); }
Conversion operator for MGString to convert into a const char *. Note: The entire code is listed above, you need only add this to your class definition.

bool Insert( const char* Str, int Index );
bool Insert( const MGString& Src, int Index );

Insertion functions. Inserts Str or Src at position Index in the 'this' object. Returns true if successful, or false upon failure. If not successful, the original string should not be destroyed. Index may be between 0 and the index of the null character of this. Returns false if unable to insert the text or true if it was able. If unable to insert the text, the original text should remain unchanged.

bool Append( const char* Str );
bool Append( const MGString& Src );

Append functions. Appends Str or Src at end of the 'this' object. Returns true if successful, or false upon failure. If not successful, the original string should not be destroyed. Returns false if unable to append the text or true if it was able. If unable to append the text, the original text should remain unchanged.

MGString operator+=( const char* Str );
Concatenation operator. Appends Str to the end of this, and returns an MGString object. Note: For now, failures are ignored. Modifies the this object.

MGString operator+( const char* Op2 ) const;
MGString operator+( const MGString& Op2 ) const;

Creates a temporary MGString object composed of the this object, and the Op2 string appended to it. Does not modify this. Note: For now, failures are ignored.

bool SetSize( int NewSize );
Internal, protected member function. This function is optional, but may prove useful. Call it to size the string buffer to a desired size. If a string is currently in the m_Str, then the strings contents is saved (if the buffer is being made smaller, then portions of the string will be lost).

 

static bool m_Sensitive;
This item is a static data member. The comparison operators will use this member to determine if operators are case sensitive or not.

ostream& operator<<( ostream& Dest, const MGString& Str );

Sample Test Program

#include <iostream>
using namespace std;
#include "MGString.h"

void Foo( MGString S ) 
{
	cout << "Foo got: " << S << endl;
}

int main() {
	MGString A;
	A = "Test A";
	MGString B=A;
	if( A != B ) 	cout << "Either '==' or Copy Constructor failed" << endl;
	else 		cout << "Both '==' and Copy Constructor look ok" << endl;
	if( A > B ) 	cout << "The '>' operator didn't work" << endl;
	else 		cout << "The '>' operator looks Ok" << endl;
	A.Insert( " of", 4 );
	cout << A << endl;
	if( A == "Test of A" ) cout << "Insert seems to have worked" << endl;
	else 	cout << "Insert seems to have failed!" << endl;
	
	A = A + " went well ";
	cout << A << endl;
	A += B;
	cout << A << endl;
	if( A == "Test of A went well Test A" ) cout << "Operators went well" << endl;
	else cout << "Uh oh" << endl;
	Foo( A );
	return( 0 );
}

Sample Output

Both '==' and Copy Constructor look ok
The '>' operator looks Ok
Test of A
Insert seems to have worked
Test of A went well
Test of A went well Test A
Operators went well
Foo got: Test of A went well Test A