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

Hello all. I am stuck on this reserve method. c++. Thank you in advance! class N

ID: 3806489 • Letter: H

Question

Hello all. I am stuck on this reserve method. c++. Thank you in advance!

class NString
{
private:
char * stringArrayPointer = NULL; // PTR to char, used to dynamically allocate an array of char
size_t Capacity; // Keeps track of the # of elements in the string array
size_t Size; // Stores current length of the C string stored in the string array
public:
NString(){} // Default constructor
NString(const char* other); // Initialization constructor
NString(const NString& other); // Copy constructor
~NString(){} //Destructor - calls clear() method
NString& operator=(const NString& other);// Overloaded assignment operator
NString operator=(const char* other);// Overloaded assignment operator c string
size_t capacity() const; // Method to return the string capacity
size_t size() const; // Method to return the string size.
bool empty() const; // Method to return true if string size = 0. Otherwise return false
void clear(); // Method to set string size and string cap to -. uses delete[] to del str array.
void reverse(size_t n);// Modifies an object's string cap w/o chaning its size or contents**************************8
bool operator==(const NString& rhs) const; // Return true if chars are idenditcal
const char& operator[](size_t pos) const; // Returns element pos of the string array
char& operator[](size_t pos); // Return element pos of the string array
friend bool operator==(const char* lhs, const NString& rhs); // Returns true if chars are identical
friend ostream& operator<<(ostream& lhs, const NString& rhs); // Overloading insertion op
};

void NString::reserve(size_t n)

{

DEFINITION;

}

This method modifies an object's string capacity without changing its size or the contents of the string array. The required logic is:

If n is less than the string size or n is equal to the current string capacity, simply return.

Set the string capacity to n.

Declare a temporary array pointer (a pointer to a char).

If the string capacity is 0, set the temporary array pointer to nullptr. Otherwise, use the temporary array pointer to allocate an array of char. The number of elements in the new temporary array should be equal to the string capacity.

Copy the contents of the string array into the temporary array.

Delete the string array.

Set the string array pointer to the temporary array pointer.

Explanation / Answer

The step-by-step method code is as follows:

Source Code:

// This method modifies an object's string capacity without changing its size or the contents of the string array.

void NString::reserve(size_t n) {

//If n is less than the string size or n is equal to the current string capacity, simply return.
if (n < Size || n == Capacity)
return;

//Set the string capacity to n.
Capacity = n;

//Declare a temporary array pointer (a pointer to a char).
char *tmp_ptr;

//If the string capacity is 0, set the temporary array pointer to nullptr.
if (Capacity == 0)
tmp_ptr = NULL;
//Otherwise, use the temporary array pointer to allocate an array of char. The number of elements in the new //temporary array should be equal to the string capacity.
else
tmp_ptr = new char [Capacity];

//Copy the contents of the string array into the temporary array.
for (int i = 0; stringArrayPointer[i] != ''; i++) {
tmp_ptr[i] = stringArrayPointer[i];
}

//Delete the string array.
free(stringArrayPointer);

//Set the string array pointer to the temporary array pointer.
stringArrayPointer = tmp_ptr;
}

Note: In your class definition, change the method name from reverse() to reserve(). You asked for reserve() but in your class you have declared it as reverse() so change the name there otherwise compiler won't be identifying reserve() method.

I have done exactly mentioned in the steps.

First I checked for value of 'n' to check whether to return or not. If this condition is false, the next statements will get executed.

In this steps, I modified the Capacity value to n, created the temporary array, set it to NULL if n=0, otherwise copying the values in that temporary array till the stringArrayPointer is not NULL i.e. till stringArray has a value. Then the stringArrayPointer is freed and now it points to temporary one. At last in this method, do not free the temporary pointer otherwise all the content will also get deleted because now stringArrayPointer and tmp_array both point to the same memory location. So, freeing one will also affect to both.

Please comment if there is any query. Thank you. :)

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