The purpose of this lab is to practice operator overloading
for the input (>>) and output (<<) operators
with respect to a dynamic member variable within a 'SimpleString'
class definition. In this lab, you will be given a SimpleString class
which will contain three private member variables and a private resize
member function. The class definition will also contain a default
constructor, a destructor, and two overloaded operators, <<
and >>.
For this lab, you will write the definitions to the private 'resize' helper
function, the default constructor, destructor, and overloaded input and output
operators. Use the given descritions below.
void resize(const int) - this function will pass a constant integer
by value for the new capacity for the dynamic character array.
This function will be called whenever the dynamic member variable
'str' has reached its allocatd capacity and needs more memory allocated.
For this function you will check to see if the passed parameter is
lesser than zero or lesser then or equal to the current object's capacity,
if so, do nothing and return. If the object's dynamic member
variable 'str' is NULL, allocate a new character array with the requested
capacity (the passed parameter). Set the first character of the 'str'
variable to character NULL, capacity to the value of the passed parameter, and
zero the the current length of the string.
Otherwise, declare a temporary character pointer and allocate a new character
array sized from the passed parameter. Copy the the current string value
of 'str' into the temporary string, deallocate 'str', and assign
the temporary pointer to 'str'. Set capacity to the passed parameter,
and length of the current string length of 'str'.
SimpleString() - the default constructor will allocate a DEFAULT size
to the dynamic member variable 'str', set capacity to DEFAULT, length to zero,
and the first character of str to the NULL character.
~SimpleString() - the destructor will deallocate 'str' if str has allocated
memory. Set the char pointer str to NULL, and capacity and length to zero.
istream& operator>>(istream& in, SimpleString& src) -
This overloaded input operator will extract characters from the input
stream one character at a time until a space character is read. Declare a
counter variable to zero and a character var. Read in one character from
the input stream (cin.get). While the read character is not a space character,
check to see if the current count of read characters is greater than or
equal to the SimpleString object's capacity. If so, resize the SimpleString object's
capacity by calling the resize function, passing the current size + DEFAULT.
Then, set the SimpleString's str @ index 'count' to the read character.
Increment count and read in the next character from the input
stream (cin.get). Once a space character has been read, set the next character
of the SimpleString's str array to character NULL. Set the SimpleStrings length to the
current amount of characters within the string (strlen or count), and return the
istream object.
ostream& operator<<(ostream&, const SimpleString& src) -
This overloaded output operator will output the current value of the passed
SimpleString object's 'str' value. For debug purposes, also print out
the object's current length and capacity, then return the output
stream object