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:
- It will contain a character pointer for the string and an integer for the
size of the currently allocated dynamic array as its member variables. You may
add other member variables, but these two are required.
- It will dynamically grow to hold the characters it needs to hold.
- It will be an ADT, so all member variables and helper functions will
be private.
- It will support the following operators:
- >> The input operator. It will replace the current string with the
first word (up to the first whitespace) on the input stream.
- << The output operator. If the string exists, output the
string to the output stream.
- + Concatenation. Return a new string that is the left operand followed
by the right operand. ie "Hello "+"world" would return "Hello world".
- = Assignment. Copy the string of the source into the current string,
deleting anything the current string may contain.
- += Append. Append the contents of the right hand operand to the
current 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.
- > Greater than. Return true if the left string is lexiographically
greater than the right string.
- == Equality. Return true if the two strings have the same characters.
- != Inequality. Return true if the two strings differ for any character.
- It will have a default constructor, copy constructor, a constructor that
takes a C-style string, a constructor that takes a single character and a
destructor.
- It will have the following member functions:
- int length() - Returns the current length of the string. Use the strlen()
function to determine the current string length if the string exists.
- void erase() - Deletes the string. You may either delete the memory
allocated or set the first character to the null character.
- char *reverse() - Returns the string in reverse, does not alter the string
as stored in the class.
- void getline(istream &i, int count) - Get count number of characters from
the input stream i and store it, replacing the current string.
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.