Homework 3 - Classes with Dynamic Arrays

Due: Monday February 12, 2007 at 5pm

Name your file hw3.cpp and mail the file as an attachment to my Helios account. The purpose of this assignment is to learn how to manipulate member variables that are dynamic arrays, to use operators and to create an abstract data type (ADT).

You will be making your own implementation of a string class called myString. Your string class will have the following features:

Requirements

The size member variable contains the size of the memory allocated, not the current string size. Use the length() member function to get the current string length. The current string size may be smaller than the amount of memory allocated in certain situations, such as when a short string is assigned to a long string.

For += (append), if the current string is does not have enough memory allocated to store itself plus the appended string, you must allocate a new string that is long enough. In this process, you have to save the contents of the current string so that you can copy it over to the new string.

For = (assignment), >> (input) and getline(), if the current string is not long enough to store the source string, delete the current string and allocate enough space. Unlike +=, you do not need to store the current string as it will be completely replaced by the new string. Also, do NOT point the character pointer of the current object to the character pointer of the source. Copy the data over to the current object's own array.

The default constructor may either allocate a fixed size array and set the first character to the null character ('\0') or it can set the array pointer equal to NULL (the integer value 0) and the size equal to zero.

The copy constructor and the constructor that takes a C-style string will allocate an array large enough for the source string and copy the string over to its array. Do NOT point the character pointer of the current object to the character pointer of the source. Copy the data over to the current object's own array.

The constructor that takes a character will allocate an array of size 2 or greater and set the first character to the character argument and the second character to the null character.

Main function

Use the main function in hw3main.cpp to test your class. You may add other tests to your main function, but you must retain all the tests in this main function. This file is set up to be used with seperate compilation if you wish to use that for this assignment.