class description (MyString):
class defines a sequence of characters (similar to class string). The implementation must be as an array of characters that are dynamically allocated and resized as necessary. You must use a null character to terminate the string - just like C-strings are null terminated.
deafult constructor:
Copy constructor
I'm getting seg faults and errors when I test these constructors. DO you see any errors in my code? I can't figure it out.
Thanks
class defines a sequence of characters (similar to class string). The implementation must be as an array of characters that are dynamically allocated and resized as necessary. You must use a null character to terminate the string - just like C-strings are null terminated.
deafult constructor:
Code:
/// get_length(char *s) gets length of s before null character. buf is private data member to store array for MyString
MyString::MyString(char *s)
{
buf = new char(get_length(s)+1);
for(int i=0;s;++i)
buf[i] = s[i];
buf[get_length(s)+1] = '\0';
}
Code:
MyString::MyString(const MyString &s)
{
char *s_arr = s.buf;
buf = new char(s.length()+1); ////length() gets length of MyString object before null
for(int i=0;s_arr;++i)
{
buf[i] = s.buf[i];
}
buf[length()+1] = '\0';
}
I'm getting seg faults and errors when I test these constructors. DO you see any errors in my code? I can't figure it out.
Thanks
Comment