Question: compiler required: visual studio 2019 and do not use #include Modify the String class in the following ways: a) Our String class always deletes the

compiler required: visual studio 2019 and do not use #include

Modify the String class in the following ways:

a) Our String class always deletes the old character buffer and allocates a new character buffer on assignment or in the copy constructor. This need not be done if the new value is shorter than the current value and hence would fit the existing buffer. Rewrite the String class to only allocate a new buffer when necessary

b) Overload the compound += operator to perform concatenation of two String objects.

c) Overload the + operator using the += operator.

d) Add a member function compare (String) that returns -1 or 0 or 1 depending on whether the string is lexicographically less than, equal to, or greater than the argument. Provide a test program (as a seperate cpp file) to test your class.

string.cpp:------------------------------------------------------------------------------------

#include #include using namespace std; #include "string.h"

String::String() { len = 0; buffer = NULL; // No need to allocate array to hold zero characters }

String::String(const char p[]) { // Determine number of characters in string (strlen(p)) len = 0; while (p[len] != '\0') len++; // Allocate buffer array, remember to make space for NULL character buffer = new char[len + 1]; // Copy new characters (strcpy( buffer, p )) for (int i = 0; i < len; i++) buffer[i] = p[i]; buffer[len] ='\0'; }

String::String(const String& right) { len = right.length(); buffer = new char[len + 1]; for (int i = 0; i < len; i++) buffer[i] = right[i]; buffer[len] = '\0'; }

String::~String() { delete[] buffer; }

int String::length() const { return len; }

char& String::operator [] (int index) { assert((index >= 0) && (index < len )); return buffer[index]; }

char String::operator [] (int index) const { assert((index >= 0) && (index < len )); return buffer[index]; }

String& String::operator=(const String& right) { if (this != &right) { delete[] buffer; // Get rid of old buffer len = right.length(); buffer = new char[len + 1]; for (int i = 0; i < len; i++) buffer[i] = right[i]; buffer[len] = '\0'; } return *this; }

ostream& operator << (ostream& out, const String& right) { int n = right.length(); for (int i = 0; i < n; i++) out << right[i]; return out;

}

string.h:----------------------------------------------------------------------------------

#ifndef STRING_H #define STRING_H class String { public: String(); // Default constructor String(const char p[]); // Simple constructor String(const String& right); // Copy constructor ~String(); // Destructor String& operator=(const String& right); // Assignment operator String& operator+=(const String& right); int length() const; char& operator[](int index); char operator[](int index) const; private: char* buffer; int len; }; #endif

stringtest.cpp:---------------------------------------------------------------

#include #include "string.h" using namespace std; ostream& operator << (ostream& out, const String& right); void main(){ String new_name; cout << new_name << endl; char letters[] = "Hello World"; String s(letters); cout << s << endl; String first("Fred"); cout << first << endl; String second(first); cout << second << endl; String third = first; // Also uses copy constructor cout << third << endl; String classmates[5] = {"Jim", "Peter"}; for (int i = 0; i < 5; i++) cout << i << " " << classmates[i] << endl; String test = classmates[2]; cout << "test is "<< test << endl; cout << test.length() << endl; //cout << (int)test[0] << endl; }

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!