Methods to the MyString class: • MyString::MyString(const MyString& s) This \"co
ID: 3630565 • Letter: M
Question
Methods to the MyString class:
• MyString::MyString(const MyString& s)
This "copy constructor" for the MyString class should initialize a new MyString object to the same string as the existing MyString object s. The required logic is:
1. Set the string size for the new object to the string size of s.
2. Set the string capacity for the new object to string capacity of s.
3. Use the string array pointer for the new object to allocate an array of char. The size of the new string array should be the string capacity plus one.
4. Copy the contents of the string array of s into the string array of the new object.
• int MyString::capacity() const
This method should return the string capacity.
• MyString::~MyString()
The destructor for the MyString class should use the delete[] operator to delete the string array.
• MyString& MyString::operator=(const MyString& rightOp)
This overloaded assignment operator should assign one MyString object (the object rightOp) to another (the object that called the method, which is pointed to by this). The required logic is:
1. Check for self-assignment. If the address stored in the pointer this is the same as the address of the object rightOp, then skip to the final step.
2. Delete the string array for the object that called the method.
3. Set the string size for the object that called the method to the string size of rightOp.
4. Set the string capacity for the object that called the method to the string capacity of rightOp.
5. Use the string array pointer for the object that called the method to allocate an array of char. The size of the new string array should be the string capacity plus one.
6. Copy the contents of the string array of rightOp into the string array of the object that called the method.
7. Return *this.
• MyString& MyString::operator=(const char* rightOp)
This overloaded assignment operator should assign a C string (the string rightOp) to a MyString object (the object that called the method, which is pointed to by this). The required logic is:
1. Delete the string array for the object that called the method.
2. Set the string size for the object that called the method to the length of the C string rightOp.
3. Set the string capacity for the object that called the method to the next multiple of 16 that is greater than the string size. Use the string array pointer for the object that called the method to allocate an array of char. The size of the new string array should be the string capacity plus one.
4. Copy the contents of the C string rightOp into the string array of the object that called the method.
5. Return *this.
• MyString MyString::operator+(const MyString& rightOp) const
This operator method should concatenate the strings stored in two MyString objects (the object that called the method, which is pointed to by this, and the object rightOp). This method will not work if you have not correctly written the copy constructor. The required logic is:
1. Declare a new MyString object to hold the result of the string concatenation. I'll call this the result object.
2. Set the string size for the result object to the sum of the string size of the object that called the method plus the string size of rightOp.
3. If the string size of the result object is greater than the string capacity of the result object, you will need to re-size the result object's string array.
1. Delete the result object's string array.
2. Set the string capacity for the result object to the next multiple of 16 that is greater than the string size of the result object.
3. Use the string array pointer for the result object to allocate an array of char. The size of the new string array should be the string capacity of the result object plus one.
4. Copy the contents of the string array of the object that called the method into the string array of the result object.
5. Concatenate the contents of the string array of the object rightOp onto the end of the contents of the string array of the result object. This is most easily done using the C string function strcat().
6. Return the result object.
• MyString MyString::operator+(const char* rightOp) const
This operator method should concatenate the C string rightOp onto the end of the string stored in the MyString object that called the method, which is pointed to by this. This method will not work if you have not correctly written the copy constructor. The required logic is:
1. Declare a new MyString object to hold the result of the string concatenation. I'll call this the result object.
2. Set the string size for the result object to the sum of the string size of the object that called the method plus the length of the C string rightOp.
3. If the string size of the result object is greater than the string capacity of the result object, you will need to re-size the result object's string array.
1. Delete the result object's string array.
2. Set the string capacity for the result object to the next multiple of 16 that is greater than the string size of the result object.
3. Use the string array pointer for the result object to allocate an array of char. The size of the new string array should be the string capacity of the result object plus one.
4. Copy the contents of the string array of the object that called the method into the string array of the result object.
5. Concatenate the C string rightOp onto the end of the contents of the string array of the result object.
6. Return the result object.
• char MyString::at(int sub) const throw(out_of_range)
If sub is less than 0 or greater than or equal to the string size, this method should throw an out_of_range exception, like so:
throw out_of_range("Subscript out of range");
Otherwise, the method should return element sub of the string array.
To make use of the existing standard exception class out_of_range, you will need to #include ; and code a using declaration for the class name.
• char& MyString::at(int sub) throw(out_of_range)
If sub is less than 0 or greater than or equal to the string size, this method should throw an out_of_range exception. Otherwise, it should return element sub of the string array.
In addition to the methods described above, you will need to write one new standalone function. This function is not (and cannot be) a method. You should
1. Include a friend declaration for the function in the MyString class definition
2. Put the definition for this function in MyString.cpp
• MyString operator+(const char* leftOp, const MyString& rightOp)
This operator function should concatenate the string stored in the MyString object rightOp onto the end of the C string leftOp. This function will not work if you have not correctly written the copy constructor. The required logic is:
1. Declare a new MyString object to hold the result of the string concatenation. I'll call this the result object.
2. Set the string size for the result object to the sum of the length of the C string leftOp plus the string size of the object rightOp.
3. If the string size of the result object is greater than the string capacity of the result object, you will need to re-size the result object's string array.
1. Delete the result object's string array.
2. Set the string capacity for the result object to the next multiple of 16 that is greater than the string size of the result object.
3. Use the string array pointer for the result object to allocate an array of char. The size of the new string array should be the string capacity of the result object plus one.
4. Copy the contents of the C string leftOp into the string array of the result object.
5. Concatenate the contents of the string array of the object rightOp onto the end of the contents of the string array of the result object.
Return the result object.
Explanation / Answer
MyString::MyString(const MyString & original)
{
capacityStr = original.lengthStr + 1;
str = new char[capacityStr + 1];
lengthStr = 0;
str[0] = '';
strcpy(str,original.str);
}
int MyString::capacity() const
{
return capacityStr;
}
// DESTRUCTOR
MyString::~MyString()
{
// dealocate memory storage for str
delete [] str;
}
MyString MyString::operator+ (const MyString & MyString)
{
// first check to see if we have enough capacity for the current chars plus the chars in aMyString
if (capacityStr < _length + MyString._length)
{
// we need more capacity, so let’s allocate some
capacityStr = lengthStr + MyString.lengthStr;
char * moreCapacity = new char[capacityStr];
// copy the chars from the current instance to our new memory
strcpy(newCapacity, _string);
// de-allocate our old memory
delete [] str;
// store a pointer to our new memory
str = newCapacity;
}
// by the time we get here we definitely have enough memory for all the chars
// now we can copy the chars from aMyString and append them to our current chars
for (int i = 0; i <= MyString.lengthStr; i++)
str[lengthStr+ i] = MyString._str[i];
// new we can store the new length
lengthStr = lengthStr + MyString.lengthStr;
//append null char
str[lengthStr] = '';
return MyString;
}
MyString MyString::operator+= (const char* CString )
{
int numCharsToAppend = strlen(CString);
// check for needed capacity
if (capacityStr < lengthStr + numCharsToAppend)
{ // adjust capacity
capacityStr = lengthStr + numCharsToAppend;
// acquire new memory
char * temp = new char[capacityStr];
// copy old chars
strcpy(temp, stringStr);
// delete old chars
delete [] str;
// save new chars
str = temp;
}
//concat
strncat(str, CString, numCharsToAppend);
lengthStr = lengthStr + numCharsToAppend;
str[lengthStr+1] = '';
}
}