Need the operator=(const char* other) method definition. c++ class NString { pri
ID: 3806444 • Letter: N
Question
Need the operator=(const char* other) method definition. c++
class NString
{
private:
char * stringArrayPointer = nullptr; // 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 *This Method****
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
};
The method should assign a c string (the string other) to an NString object (the object that called the method, pointed to by this) and it should return a NString reference
NString& NString::operator=(const char* other)
{
DEFINITION
}
Explanation / Answer
NString& NString::operator=(const char* other)
{
NString ns;
int i;
for(i=0; other[i]!=''; i++)
{
ns.stringArrayPointer[i] = other[i];
}
Size = i;
return ns;
}
Here, the required code is as above. In this method, string is taken which is pointed by *other, which is to be copied in stringArrayPointer. The variable Capacity value will also get changed when we assign the value to stringArrayPointer. The string I copied using for loop in which starting from index 0 till *other gives i.e. null character, the characters are copied. And after this copy is done, the size variable value I updated to i. This is not mentioned in the question but this is necessary to do perfect execution,
Then the reference is needed to return which means we need to return an object of NString via & operator which means the object which called this method =, gets the same reference of this and its values get changed. So, if you write something like NString ns1;.... ns1 = "Abc"; then first this Abc string is copied in = method in a new object named ns, then this ns is assigned "Abc" in its string and then this ns is returned as a reference which means ns1 = ns. Combine this with your other code and check its working.
Please comment if there is any query. Thank you. :)
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.