Question: In C++ Write a class that implements your own string class, called MyString. Below is a description of the class attributes: str: stores the memory

In C++

Write a class that implements your own string class, called MyString.

Below is a description of the class attributes:

str: stores the memory address for a dynamically allocated array of characters. This stores a C-string. size: stores the number of characters in the c-string (not including the null terminator).

Constructor: accepts a C-String as it's only argument. Dynamically allocates an array long enough to store the argument and a null terminator. Sets size to the appropriate value. Copies the argument C-string into the new array.

Destructor: frees up the memory used by the dynamically allocated array.

size() - returns the number of characters in the string stored by MyString (excluding the null terminator).

set() - accepts a C-string as it's only argument. Replaces the current C-string held by the MyString object, resizing the array as needed.

at() - accepts an integer as it's only argument. Returns a reference to the element indicated by the argument. That is, if at is passed 1, then the second element is returned by reference. The method should throw the C-String exception "OUT OF BOUNDS!" if the argument passed would be an invalid subscript.

Make sure there are no memory leaks.

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!