Please take a look at the question I posted two days ago. I really need help wit
ID: 3595033 • Letter: P
Question
Please take a look at the question I posted two days ago. I really need help with it and it's due tonight.
Link to question: https://www.chegg.com/homework-help/questions-and-answers/hello-need-help-finishing-c-project-thank-help-instructions-develop-c-string-class-describ-q24375977
Greetings everyone, this is kind of urgent. Somehow nobody is answering this question and I really need some answers since the assignment is due tonight... Whoever answered that questino can answer this one too for some free points as a bonus. Thank you for reading and please help!
Please leave a comment if you need additional information.
Have a nice day.
Explanation / Answer
Implemented all method present in file. Don't know anything about your tester code.
(BUt seeems like failing with seg fault)
// File: mystring.cpp
// ==================
// Implementation file for user-defined String class that stores
// characters internally in a dynamically-allocated array.
#include "mystring.h"
String::String()
{
contents = new char[1]; // Allocate array for 1 char.
// finish it by setting the data for contents and len
//.....
len = 0;
contents[0] = '';
}
String::String(const char *s) // or String::String(const char s[])
{
len = strlen(s);
// missing one statement here
contents = new char[len+1];
//...
strcpy(contents, s);
}
/*
String::String(const String &str) // or String::String(const char s[])
{
len = str.len;
// missing one statement here
contents = new char[len+1];
//...
strcpy(contents, str.contents);
}
String::~String()
{
delete [] contents;
len = 0;
}
String &String::operator =(const String &s) // assignment operator
{
len = s.len;
// missing one statement here
char *new_contents = new char[len+1];
//...
strcpy(new_contents, s.contents);
delete [] contents;
contents = new_contents;
}
*/
// The "big three" implementation including their names and parameter lists
// (1) copy constructor
// (2) destructor
// (3) overloading =
//A method that appends the contents of the passed String onto the end of the String the method is called on.
//You can use C string function to implement it. However,the space should be taken care of.
void String::append(const String &str)
{
char *new_contents = new char[(len + str.len) + 1];
strcpy(new_contents, contents);
// complete it ....
strcpy(new_contents, str.contents);
new_contents[(len + str.len)] = '';
delete [] contents;
contents = new_contents;
}
bool String::operator ==(const String &str) const
{
return strcmp(contents, str.contents) == 0;
}
bool String::operator !=(const String &str) const
{
// implement it ...
return strcmp(contents, str.contents) != 0;
}
bool String::operator >(const String &str) const
{
// implement it ...
return strcmp(contents, str.contents) > 0;
}
bool String::operator <(const String &str) const
{
// implement it ...
return strcmp(contents, str.contents) < 0;
}
bool String::operator >=(const String &str) const
{
// implement it ...
return strcmp(contents, str.contents) >= 0;
}
bool String::operator <=(const String &str) const
{
// implement it ...
return strcmp(contents, str.contents) <= 0;
}
String String::operator +=(const String &str)
{
append(str);
return *this;
}
void String::print(ostream &out) const
{
// implement it ...
out << contents;
}
int String::length() const
{
return len;
}
char String::operator [](int i) const
{
if (i < 0 || i >= len) {
cerr << "can't access location " << i
<< " of string "" << contents << """ << endl;
return '';
}
// complete it ....
}
ostream & operator<<(ostream &out, const String & s) // overload ostream operator "<<" - External!
{
// implement it ...
out << s;
return out;
}