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

Hello all, I need help with the second constructor. Please comment or explain yo

ID: 3806438 • Letter: H

Question

Hello all, I need help with the second constructor. Please comment or explain your code. C++. Thank you in advance.

#include

using namespace std;

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 -- This constructor needs work. I will post what I have below. -------
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
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
};

*Doesn't work*

NString::NString(const char* other)
{
int index = 0;
//Determine the length of the array
while (other[index] != NULL)
index++;
//Allocate dynamic memory on the heap
char *stringArrayPointer;
stringArrayPointer = new char[index+1];
//Copy the contents of the array pointed by other into stringArrayPointer
//which is the char array of the object
for (int i = 0; i < index; i++)
stringArrayPointer[i] = other[i];
stringArrayPointer[index+1] = '';
Size = index+1;
}

Also if this is correct, it may be that my overloaded assignment operators could be the problem. Providing code or even psuedo code for the assignment operator would be appreciated if the constructor is correct.

Explanation / Answer

Here is the code for you:

#include<iostream>
using namespace std;
class NString
{
private:
char * stringArrayPointer; // 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 -- This constructor needs work. I will post what I have below. -------
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
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
};
/*Doesn't work*/
/* Everything seems fine, but the problem here is, you're trying to create another
stringArrayPointer here locally, which will assign the the values to the local variable.
This should not be case. Actually, the private class variable should be initialized.
So, the modification is done here.
*/
NString::NString(const char* other)
{
int index = 0;
//Determine the length of the array
while (other[index] != NULL)
index++;
//Allocate dynamic memory on the heap
//char *stringArrayPointer;   Just removing this line of code will make this constructor work perfect.
stringArrayPointer = new char[index+1];
//Copy the contents of the array pointed by other into stringArrayPointer
//which is the char array of the object
for (int i = 0; i < index; i++)
stringArrayPointer[i] = other[i];
stringArrayPointer[index+1] = '';
Size = index+1;
}

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