QuestionQuestion

In this project, you will implement a secure MyString class. A MyString class should behave much like the C++ string class, with some differences. Your class will use a dynamically allocated char array to hold a character string. There will also be a variable to hold the capacity of the array (i.e. max buffer size), and a variable to hold the number of characters actually held in the array. Note that the character array should be properly null-terminated. The length variable should not include the null-terminator, but the capacity variable should. Your MyString should start with a maximum capacity of 5 characters and should grow in size by doubling. In other words the capacity of your MyString should follow a pattern of 5, 10, 20, 40, 80, … bytes of memory (plus one for the null character) depending of the number of characters stored
You will be given full MyString.h file to start, as well as a MyString.cpp file with function stubs and some functions which have been completed for you. Your job is to complete the areas listed as TODO, following the purpose and description of each function I’ve documented for you. You are also required to create unit tests for each function that you write (with the exception of a few, which will be tested in the main file; this is specified on the rubric), following the demonstrations in class
Specifications are given below.
1. There is a starter file on Canvas set up for implementation and unit testing. Download this and make sure to add your name to the project.
2. It will be your job to make sure exhaustive appropriate unit tests are created for each function that you write.
3. The first function you should implement in MyString.cpp is dbgPrint() as it is relatively easy.
4. Now, implement one function at a time, creating an appropriate suite of tests in UnitTests (make sure to annotate/label each test you write).
A few notes on requirements:
• Except for istream’s get and peek functions and ostream’s put function, use of C or C++ library functions (particularly those built for C++ or C strings) is not allowed when implementing any function. For example, you may not call C++’s stoi function to implement your own stoi function and you may not call C’s strcpy to help you implement the copy constructor or overloaded assignment operator. In other words, you must write each function from scratch.
• I am well aware that programs with similarities may exist online. Please note that a) No program that currently exists online will correctly fit the requirements listed here; and b) You may refer to these programs to gain insight, but all code should exclusively be yours. Any references or sources you use MUST be cited in your work to avoid academic dishonesty.
• To clarify both points from above, may actually be a good resource. It may provide some useful insights when overloading the assignment operator or copy constructors, for instance. However, note that they use strcpy, which is not allowed here (although you may want to pay attention to a few of the checks they use in those functions).

Solution PreviewSolution Preview

These solutions may offer step-by-step problem-solving explanations or good writing examples that include modern styles of formatting and construction of bibliographies out of text citations and references. Students may use these solutions for personal skill-building and practice. Unethical use is strictly forbidden.

// Function: MyString
// Description: Default Constructor
// Purpose: Initialize the data char string to the empty string,
//                  and initialize the length and capacity
///////////////////////////////////////////////////////////////////////////////
MyString::MyString() {
len = 0;
capacity = 10;
data = new char[this->capacity + 1];
data[this->len] = '\0';
}

///////////////////////////////////////////////////////////////////////////////
// Function: MyString
// Description: Overloaded Constructor
// Purpose: Initialize the data char string to hold the input string,
// appropriately setting the size and capacity
///////////////////////////////////////////////////////////////////////////////
MyString::MyString(const char *cStr) {
len = 0;
capacity = 10;
data = new char[capacity + 1];

//iterate until the end of cStr, adding each character to data
while (cStr[len] != '\0') {
//if input string copying becomes too large, increase the capacity of
//our string
if (len >= capacity) {
//double the capacity
capacity = capacity * 2;

//copy all characters into a new buffer
char *tmp = new char[capacity + 1];
for (int i = 0; i < len; i++) {
tmp[i] = data[i];
}
delete[] data;
data = tmp;
tmp = NULL;
}

//add the character to our string
data[len] = cStr[len];
len++;
}

data[this->len] = '\0';
}

///////////////////////////////////////////////////////////////////////////////
// Function: ~MyString
// Purpose: Deallocate the memory to data and set to NULL, and set
// length and capacity to zero
///////////////////////////////////////////////////////////////////////////////
MyString::~MyString() {
delete[] data;
data = NULL;
capacity = 0;
len = 0;
}

///////////////////////////////////////////////////////////////////////////////
// Function: MyString
// Description: Copy Constructor
// Purpose: Set 'this' to be a deep copy of 'that'
///////////////////////////////////////////////////////////////////////////////
MyString::MyString(const MyString &that) {
this->len = that.len;
this->capacity = that.capacity;

this->data = new char[capacity + 1];

int index = 0;
//iterate until the end of cStr, adding each character to data
while (that.data[index] != '\0') {
this->data[index] = that.data[index];
index++;
}
this->data[this->len] = '\0';
}

///////////////////////////////////////////////////////////////////////////////
// Function: operator =
// Description: Overloaded Assignment Operator
// Purpose: Set 'this' to be a deep copy of 'that'
///////////////////////////////////////////////////////////////////////////////
MyString& MyString::operator = (const MyString& that) {
this->len = that.len;
this->capacity = that.capacity;
this->data = new char[capacity + 1];
int index = 0;
//iterate until the end of cStr, adding each character to data
while (that.data[index] != '\0') {
this->data[index] = that.data[index];
index++;
}
this->data[this->len] = '\0';
return *this;
}

///////////////////////////////////////////////////////////////////////////////
// Function: operator ==
// Description: Overloaded Equivalency Operator
// Purpose: Check that 'this' and 'that' are equivalent
///////////////////////////////////////////////////////////////////////////////
bool MyString::operator == (const MyString& that) const {
bool isEqual = true;
if(this->len != that.len) {
isEqual = false;
} else if(this->capacity != that.capacity) {
isEqual = false;
} else {
int index = 0;
while(this->data[index] != '\0' && that.data[index] != '\0') {
if(this->data[index] != that.data[index]) {
isEqual = false;
break;
}
index...

By purchasing this solution you'll be able to access the following files:
Solution.zip.

$45.00
for this solution

or FREE if you
register a new account!

PayPal, G Pay, ApplePay, Amazon Pay, and all major credit cards accepted.

Find A Tutor

View available Information Security Tutors

Get College Homework Help.

Are you sure you don't want to upload any files?

Fast tutor response requires as much info as possible.

Decision:
Upload a file
Continue without uploading

SUBMIT YOUR HOMEWORK
We couldn't find that subject.
Please select the best match from the list below.

We'll send you an email right away. If it's not in your inbox, check your spam folder.

  • 1
  • 2
  • 3
Live Chats