Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Continue the implementation of the String class. Add each of the following A con

ID: 3549511 • Letter: C

Question

Continue the implementation of the String class. Add each of the following

A constructor String(int n, char c) that initializes the string with n copies of
the character c.

The + operator to perform concatenation of two String objects.

A member function compare(String) that returns -1, 0, or 1 depending upon
whether the string is lexicographically less than, equal to, or greater than the
argument. Then, using this member function, provide definitions for the comparison
operators <, <=, ==, !=, >, and >=.

A function resize(int n, char c) that changes the size of the string to n, either
truncating characters from the end, or inserting new copies of character c.

The function call operator, so that s(int start, int length) returns a substring
starting at the given position of the given size.


Here is the string class

[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;
};


the second part to question is


Our String class always deletes the old character buffer and reallocates a new character buffer on assignment or in the copy constructor. This need not be done if the new value is smaller than the current value, and hence would fit in the existing buffer. Can we rewrite the String class so that each instance will maintain an integer data field indicating the size of the buffer, then only reallocate a new buffer when necessary. Abstract the common tasks from the assignment operator and the copy constructor, and place them into an internal method?

Explanation / Answer

// I have change char *buffer to char *s in private member, strlen() function is used and made everywhere instead of length().


// Hope this will help you. For furthur query contact me ashishkguptaiit.cse@gmail.com.



class String
{

public:
String() // Default constructor

{

s = NULL;

}


String(const char p[]) // Simple constructor

{

s = new char[sizeof(p) + 1];

for(int i = 0; i < sizeof(p) + 1 ; i++)

{

s[i] = p[i];

}

}

String(const String& right) // Copy constructor

{

delete s[];

  s = new char[right.strlen() + 1];

for(int i = 0; i < right.strlen() + 1 ; i++)

{

s[i] = right[i];

}

}
~String() // Destructor

{

delete s[];

}
String& operator=(const String& right) // Assignment operator

{

  delete s[];

char * s1 = new char[right.strlen() + 1];

for(int i = 0; i <right.strlen() + 1 ; i++)

{

s1[i] = right[i];

}

s = s1;

return *this;

}




String& operator+=(const String& right)

{

int len = *this.strlen() + right.strlen() + 1;

int *s1 = new char [len];

int i = 0;

for( i = 0 ; i < *this.strlen(); i++)

{

s1[i] = s[i];

}

  

for(; i < len ; i++ )

{

s1[i] = right[i];

}


delete s[];

s = s1;

}





int strlen() const

{

char *s1 = s;

int i = 0;

while(s1 != NULL)

{

i++;

}

return ++i;

}
int length() const

{

return *this.strlen();

}
char& operator[](int index)

{

return s[index];

}
char operator[](int index) const

{

return s[index];

}
private:
char* s;
int len;
};

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote