Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Write this code in C++ Write a class that implements your own string class, called MyString. UML diagram: Description of the class attributes: str: stores

Write this code in C++

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

UML diagram:

image text in transcribed

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.

Place your complete class in it's own header file named MyString.h. Be sure to include preprocessor guards and mark accessors const.

MyString -str: char -size: int +MyString(s: char*) t-MyString0) +size(): int +set(s: char*): void +at(i: int): char&

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access with AI-Powered Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions