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:
- A character pointer for the string
- An integer for the number of characters currently allocated to the string
(e.g. the total capacity of the string)
- (Optional) An integer for the number of characters in the string (e.g
the string length). This is optional because
strlen
will also
tell you the number of characters in a string.
Your string class will have the following public interface:
- Default constructor - Allocate a character array that can hold a string
with 21 characters (20 characters plus the null character). 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.
- Assignment operator (=) - 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. Important: make sure you check for 'a = a' and return
out of the function without doing anything if that happens.
- 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.
- Output operator (<<) - If the string exists, output the
string to the output stream. Otherwise, output the empty string ("").
- Concatenation operator (+) - Return a new string that is the left operand
followed by the right operand (i.e. "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 operator (+=) - 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. If the index is invalid, exit out of the program.
- Less than operator (<) - Return true if the left string is
lexiographically less than the right string. Hint: wrap around strcmp()
- Greater than operator (>) - Return true if the left string is
lexiographically greater than the right string. Hint: wrap around strcmp()
- Equality operator (==) - Return true if the two strings have the same
characters. Hint: wrap around strcmp()
- Inequality operator (!=) - Return true if the two strings differ for any
character. Hint: wrap around strcmp()
- 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.
- void getline(istream &i, int size) - Get size - 1 characters from
the input stream i and store it in the current string object, replacing
the current string. If size 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 (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.