Homework #2

You are to write a class that represents a string. The class is to be called flString where fl are your first and last initials. Following is a list of functions and data members you are to use.


A constructor that initializes the object. Initialize it to an empty string ("").

flString( flString& Src );
A copy constructor that initializes the object with the same string that Src contains.

A destructor that releases memory from the string object.

const char* GetCPointer() const;
A function that returns your internal char*, so that it can be used by C-style functions.

bool Set( const char* Str );
A function that sets the object to contain a copy of Str. Returns false upon failure, true upon success.

bool Set( flString& Src );
A function that sets the object to contain a copy of Src.

int GetLength() const;
A function that returns the length of the string in the object.

This assignment must use dynamic memory allocation. It is OK to use C memory management routines such as strdup, malloc, and free.

Data Members

char* m_Str;
This data member is your allocated string space.

You may also implement other data members if you desire, but no more are needed.


Test Program and output

int main( )
	MGString A;
	A.Set( "Homework 2");
	MGString B(A);
	cout << A.GetCPointer() << " has ";
	cout << A.GetLength() << " characters" << endl;

Output should be:

Homework 2 is 10 character long.