Homework 4 - Classes with Dynamic Arrays

Due: Friday, February 20, 2009 at 5:00pm
NOTE:The assignment will only be accepted late until 5:00pm on Saturday, February 21, 2009. At that point, the solution will be posted so people can study it for Midterm 2 on Monday, February 23, 2009. NO late assignments will be accepted after the solution has been posted.

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 have the following features:

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 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.

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.

To submit the assignment, email me your header file and your implementation file.