$29
Part I The MyString class
Code the MyString class in indicated in the following UML diagram. Note that the member variable is a DYNAMIC ARRAY, so this class will be implementing the Big 3 function (copy constructor, operator= and destructor).
A MyString object is similar to a string object which you have used in Project 5. An object of type MyString will store a collection of characters – a string. Internally, the MyString object will use a char array (c-string) to store the data. This array will be a dynamic array, so that when a MyString object is created, the exact amount of storage needed can be allocated (don’t forget that a c-string stores a ‘\0’ following all chars).
MyString
char * str // storage for characters in the string. This array will contain a ‘\0’ at
//the end so that C-string functions can be used
int len //number of characters in str (not counting the ‘\0’)
+ MyString()
+ MyString(char newstr[])
+ MyString(const MyString& obj) // copy constructor
+ ~MyString() // destructor
+ int getLen() const
+ void setElement(int index, char item)
+ char getElement(int index) const
+ bool operator==(const MyString& obj)
+ bool operator!=(const MyString& obj)
+ char& operator[] (int);
+ MyString& operator=(const MyString& obj) //assignment operator overload
friend ostream& operator<<(ostream& out, const MyString& obj)
MyString() Creates a string which contains “default”;
MyString(char newstr[]) Accepts one C-string argument, allocates
an array large enough for this CString (don’t forget room for the ‘\0’), stores it, and
initializes len
+ MyString(const MyString& obj) This copy constructor initializes the new MyString object
to store the same characters as ‘obj’. The new object is totally independent of the
object ‘obj’ (deep copy)
+ ~MyString() Deletes any dynamic memory that was allocated for this object.
+ int getLen() Returns the number of characters in this string (does not count \0)
//These functions have a precondition that 0 <= in < len
+ void setElement(int in, char item) Sets the character at index ‘in’ to ‘item’
+ char getElement(int in) Returns the character at index ‘in’
+ bool operator==(const MyString& obj) Returns true if this MyString object contains the same
characters (in the same order) as obj
+ bool operator!=(const MyString& obj) Returns true if this MyString object does not contain
the same characters (in the same order) as obj
+ char& operator[] (int in) returns the character at index ‘in’
+ MyString& operator=(const MyString& obj) assignment operator overload
ostream& operator<<(ostream& out, const MyString& obj) non-member function which outputs
all characters MyString object obj
Part II Application
Write an application which:
//tests simple constructors
Creates a MyString object, str1, using the no-arg constructor
Creates a MyString object, str2, using on-arg constructor (user provides string to be passed as argument)
Updates each of the characters of str1, allowing user to enter characters and position
Outputs str1 and str2
// tests simple operator overloads
Outputs a message indicating if str1 and str2 are equal or not
Outputs the characters of str2, one per line (use the [] to access chars)
//tests copy constructor, verifying object independence
Creates a MyString object, str3, initializing it to str2
Output str2 and str3
Updates 2 characters of str2, allowing user to enter characters and position
Output str2 and str3
//tests assignment operator, verifying object independence
Output str1 and str3
Assigns str3 to str1
Output str1 and str3
Updates 2 characters of str3, allowing user to enter characters and position
Output str1 and str3