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:
- It will contain a character pointer for the string and an integer for the 
    capacity of the currently allocated dynamic array as its member variables. 
    You do not need a member variable to track how much of the array is in use
    currently since strings use the null character ('\0') to mark the end of
    the array. But, you can add such a member variable if you so choose.
 - The character array pointer should reallocate when needed to hold the number
    of characters it is requested to hold. 
 - The class will be an abstract data type, so all member variables and helper
    functions need to be private. Additionally, you should describe each member
    function in the header file.
 - The class will have the following constructors and destructor:
  
  - Default constructor - Allocate a character array that can hold a string
      with 20 characters (don't forget space for the null character too). Make
      this new array the empty string by setting the first character to the 
      null character.
  
 - Copy constructor - Allocate a character array based on the source's
      capacity and copy the source string over into that array.
  
 - Destructor - Deallocate the array if it is allocated.
  
 - Constructor that takes a single character - Allocate a character array
      that can hold a string with 20 characters (plus the null character) like
      the default constructor. Then assign the passed character to the first
      slot in the array and also set the null character. 
  
 - Constructor that takes a C-style string (character array) - Allocate a
      character array large enough to hold the passed C-style string (plus the
      null character). Then copy the characters from the passed array into
      the class's array.
  
 
   
 - The class will support the following operators:
  
  -  =  Assignment - Copy the string of the source into the current string,
       deleting anything the current string may contain. Only reallocate if 
       your current character array does not have the capacity to hold the
       source string.
  
 -  >>  The input operator - It will replace the current string with the
       first word (up to the first whitespace) on the input stream. You may 
       need to reallocate the character array if there are more characters on
       the input stream before the first whitespace than the array can hold.
  
 -  <<  The output operator - If the string exists, output the
       string to the output stream. Otherwise, output the empty string ("").
  
 -  +  Concatenation - Return a new string that is the left operand followed
       by the right operand. ie "Hello "+"world" would return "Hello world".
       Your temporary string object within the operator needs to have a capacity
       large enough to hold left's characters, right's characters and the null
       character.
  
 -  +=  Append - Append the contents of the right hand operand to the 
       current string. You may need to expand the character array in the current
       object so that it has enough capacity to hold the new string.
  
 -  []  Index operator - Return the character at the specified index if
       the string exists. 
  
 -  <  Less than - Return true if the left string is lexiographically less
       than the right string. Hint: wrap around strncmp()
  
 -  >  Greater than - Return true if the left string is lexiographically
       greater than the right string. Hint: wrap around strncmp()
  
 -  ==  Equality - Return true if the two strings have the same characters.
       Hint: wrap around strncmp()
  
 -  !=  Inequality - Return true if the two strings differ for any character.
       Hint: wrap around strncmp()
  
 
 - The class will also have the following member functions:
  
  - int length() - Returns the current length of the string. If the string
      exists, just wrap around the strlen() function. Otherwise, return 0.
  
 - void erase() - Deletes the string. You may either delete the memory 
      allocated or set the first character to the null character. 
  
 - myString reverse() - Returns the string in reverse. Do NOT alter the 
      string as stored in the class. Create a new string and make the changes 
      in it.  Return the new string.
  
 - void getline(istream &i, int count) - Get count - 1 characters from
      the input stream i and store it in the current string object, replacing 
      the current string. If count is greater than the current capacity, 
      reallocate the array to be able to hold count characters.
  
 
 
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.