Need a size() method. Language: c++ class NString { private: char * stringArrayP
ID: 3805588 • Letter: N
Question
Need a size() method. Language: 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
size_t capacity() const; // Method to return the string capacity
size_t size() const; // Method to return the string size. THIS IS THE METHOD THAT IS NEEDED TO BE ANSWERED
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
};
size() method that doesn't work:
size_t NString::size() const
{
NString o;
return o.size();
}
My program crashes when trying to make an object in main.
Explanation / Answer
At first, the reason why your program is crashing when you create the object of this class is something like when you are creating an object of the class NString with this much code only, it will give errors because the constructor as well as the destructor definitions are not given so the compiler can not find them and so your program might be crashing. So just define them empty using { } will work.
The complete code that is running is :
#include<iostream>
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
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. THIS IS THE METHOD THAT IS NEEDED TO BE ANSWERED
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
};
//size() method that doesn't work:
size_t NString::size() const
{
return Size;
}
int main()
{
NString ns;
cout << ns.size();
return 0;
}
Here, do not forget to write the constructors so that the size variable is not uninitialized. Now, the method you asked to implement is size() and inside it what you have done is you are creating a new object and returning its Size value. But, this should not be the case. The thing is, you are supposed to write the method in a way that it returns the value of Size variable from the object that had called the method.
Now, just remove the new object creation code and just return the value of Size variable. So the method is
size_t NString::size() const
{
return Size;
}
I have written this code in complete code above. You can write either "return Size" or "return this.Size" in this method to get the desired result.
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.