Homework 3 - MyString

2020_S19/wk3/hw3.cpp

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.
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.
Your string class will have the following member variables:

Your string class will have the following public interface:

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 strcpy().

Main function

Use the main function hw3.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:



The value of a is: 
The value of b is: Hello
The value of c is: r
The value of clone is: Hello
Enter a new word for c: John
The value of c is: John
Concatenation (b+clone): HelloHello
Assignment (a=b): Hello
Append (a+=c): HelloJohn
The value of a[0] is H
Equality (b==clone) is true.
Inequality (b!=c) is true.
Less than (a<b) is false.
Greater than (c>b) is true.
a's length is 9
After erase(), a's length is 0
Enter a new string for a (100 chars max): John Doe
You entered: John Doe