Sample Assignment: Your String Class
(Chapter 4)



Data Structures and Other Objects Using C++
Third Edition
by Michael Main and Walter Savitch
ISBN 0-321-19716-X

The Assignment:
You will implement and test a class called mystring. Each mystring object keeps track of a sequence of characters, similar to the standard C++ string class but with fewer operations.

Purposes:
Ensure that you can write a class that uses dynamic memory to store a sequence whose length is unspecified. Keep in mind that if you were actually writing a program that needs a string, you would use the C++ standard string, so this is mostly a learning experience to teach you the kind of things that go on in the implementation of the C++ standard string.
Make sure you continue to practice incremental development and testing of your classes.

Before Starting:
Read Sections 3.1, 3.3 and all of Chapter 4.
Download these files:
  1. mystring.h
  2. stringtest.cxx
  3. stringexam.cxx

Discussion of the Assignment
You will implement a new class called mystring, using a header file (most of which is written for you) and an implementation file (which you will write by yourself). The string that you implement is based on the C++ standard string class. In particular, you must:

Implement and test the string class discussed in Section 4.5 of the textbook, including:

  1. The constructor discussed on page 189
  2. An overloaded copy constructor
  3. The destructor
  4. The length member function (as an inline function)
  5. The reserve function. Note: The array should be resized to n+1 characters (to allow room for the null terminator). If n+1 is less than the currently allocated size, then the array should be made smaller, but never make it smaller than the current string size plus one. Also, make sure that you don't have a heap leak (the original array must be returned to the heap.)
  6. An overloaded assignment operator
  7. The operator [ ], which must have an assert statement to check its precondition
  8. Three different versions of the operator +=
  9. The operator +
  10. The operator <<
  11. The operator >>
  12. The getline function
  13. The six comparison operators

Whenever possible, use functions from the #include <cstring> facility (such as strcpy, strcat and so on).

Follow our style guide religiously. Do your work in a separate directory called hw-string that contains only the work for this assignment.

After you implement each piece, copy your header and cxx files to restore point files (with names such as mystring.h.004 and mystring.cxx.004). You'll need to submit all of these restore point files.

Note: In class, I will show you one major difference between your implementation and the typical implementation of the C++ standard string class. But don't worry about this difference for your work.

Warnings
The strcat function does not work if the area you are copying from overlaps the area you are copying to. Do not use the built-in C++ string class at any point in your implementation.

Input and Output
Your member functions must NOT write any output to cout, nor expect any input from cin. All the interaction with the member functions occurs through their parameters.

Submitting:
As you complete each part of the assignment, you must save it in files such as mystring.h.004 and mystring.cxx.004. All this work should be in a separate directory called hw-string. When you have completed all the work, go to your working directory, remove all .o and .exe files, and then create a tar file that contains all of your work. To create the tar file, change to the directory just above your working directory and give the command:
   tar -cvf hw-string.tar hw-string
This creates a file called hw-string.tar that you can submit to webct no later than 11:55pm on Friday, Sep 17.