Hello all, I am currently stuck on my equality operators for my C++ program. Tha
ID: 3806525 • Letter: H
Question
Hello all, I am currently stuck on my equality operators for my C++ program. 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
bool operator==(const NString& rhs) const; // Return true if chars are idenditcal*******this***********
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*******this*****
friend ostream& operator<<(ostream& lhs, const NString& rhs); // Overloading insertion op
};
method equality:
bool NString::operator==(const NString& rhs) const
This method should return true if the characters stored in the string array of the object that called the method are identical to the characters stored in the string array of the NIUString object passed in as rhs.
The logic for this method is less difficult that it might initially appear to be. The first step is to compare the string sizes of the two strings. If they are different, return false (two strings of different lengths can not be equal).
Otherwise, loop through the elements of both string arrays, starting at 0 and stopping when you reach the string size. (The size of which string doesn't matter, since you know they're the same by this point.) Compare the current element from each string array. If characters are different, return false. If the characters are the same, don't return true; do nothing and let the loop continue.
Once the loop ends and you've reached the end of both strings, return true.
friend function equality:
bool operator==(const char* lhs, const NString& rhs)
This method should return true if the characters of the C string passed in as lhs (up to, but not including the null character) are identical to the characters stored in the string array of the NString object passed in as rhs.
The logic for this function is similar to that of the two relational operator methods.
Explanation / Answer
//Add folowing function code in your NString.cpp file
//Please declate this function prototype in you NString.h file
char * NString::getStr()const{
return stringArrayPointer;
}
bool NString::operator==(const NString& rhs) const{
if(Size != rhs.Size)
return false;
for(int i=0;i<Size; i++){
if(stringArrayPointer[i] != rhs.stringArrayPointer[i])
return false;
}
return true;
}
//This function can not be the part of you NString.h file
bool operator==(const char* lhs, const NString& rhs){
if(strcmp(lhs,rhs.getStr())==0)
return true;
return false;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.