Homework 4 - Classes with Dynamic Arrays

Due: Tuesday February 15 Wednesday February 16, 2011 at 5:00pm
Note: The last day to turn this in late is still Friday February 18th.

Coding Conventions

Use the same coding conventions as described in Homework 1. Additionally, make sure to indent each section of the class in the class definition.

Assignment

The purpose of this assignment is to learn how to manipulate member variables that are dynamic arrays, how to use operators with dynamic array member variables and how to create a small abstract data type (ADT).

You will be making your own implementation of a string class called myString. Use seperate compilation for this class. Name the header file mystring.h and name the implementation file mystring.cpp.

Your string class will not only implement the dynamic array functions such as a destructor, it will also dynamically reallocate itself when it runs out of space. For example, if the string only has room for three more characters and the user appends 10 characters, you will reallocate the string so it has enough space for all the characters.

Your myString class will be an abstract data type, so all member variables and helper functions need to be private. Additionally, you should use comments to describe each public member function in the header file.

Your string class will have the following member variables:

Your string class will have the following public interface:

Additional Notes and Requirements

The capacity member variable contains the amount of the memory allocated, not the length of the string. Use the length() member function (or the optional length member variable) to get the current string length. The current string length may be smaller than the amount of memory allocated in certain situations. This is perfectly fine. The length should NEVER be greater than the capacity.

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. Do not forget to save the current string before reallocating, if you need to reallocate.

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 using a for() loop or strncpy().

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 using a for() loop or strncpy().

Main function

Use the main function hw4main.cpp to test your myString class. You may alter this file to ADD additional tests if you wish, but do not remove any of the tests that are currently in it. Copy it to your current directory using:
wget http://www.cs.csubak.edu/~melissa/cs222-w10/hw3main.cpp

To submit the assignment, email me your header file (mystring.h) and your implementation file (mystring.cpp). Do not email me your main file. I will be using hw3main.cpp to grade the assignment.