The Code to be refined is provided and the instructions are provided as well. Th
ID: 3697625 • Letter: T
Question
The Code to be refined is provided and the instructions are provided as well. The correct output is there as well would appreciate if it works and is according to instructions . Thank you!
Code to be refined:
//Mystring.h
#include <iostream>
using namespace std;
namespace cs_mystring {
#ifndef _MYSTRING_H_
#define _MYSTRING_H_
class myString {
private:
char *str;
public:
myString();
myString(const char *s);
myString(const myString &s);
void operator = (myString &s);
void operator = (const char *cArr);
int length() const;
friend ostream &operator <<(ostream &out, const myString &s);
friend istream &operator >> (istream &out, myString &s);
bool operator <(const myString &s) const;
bool operator <=(const myString &s) const;
bool operator >(const myString &s) const;
bool operator >=(const myString &s) const;
bool operator ==(const myString &s) const;
bool operator !=(const myString &s) const;
friend bool operator <(const char *cArr, const myString &s);
friend bool operator <=(const char *cArr, const myString &s);
friend bool operator >(const char *cArr, const myString &s);
friend bool operator >=(const char *cArr, const myString &s);
friend bool operator ==(const char *cArr, const myString &s);
friend bool operator !=(const char *cArr, const myString &s);
char &operator [](int i) const;
};
#endif
}
//mystring.cpp
#include "myString.h"
#include <cstring>
using namespace cs_mystring;
myString::myString() {
str = new char;
str[0] = '';
}
myString::myString(const char *s) {
int len = strlen(s);
str = new char[len + 1];
for (int i = 0; i < len; ++i) {
str[i] = s[i];
}
str[len] = '';
}
myString::myString(const myString &s) {
int len = strlen(s.str);
str = new char[len + 1];
for (int i = 0; i < len; ++i) {
str[i] = s.str[i];
}
str[len] = '';
}
void myString::operator = (myString &s) {
s.str = new char[length() + 1];
for (int i = 0; i < length(); ++i) {
s.str[i] = str[i];
}
s.str[length()] = '';
}
void myString::operator = (const char *cArr) {
int len = strlen(cArr);
str = new char[len + 1];
for (int i = 0; i < len; ++i) {
str[i] = cArr[i];
}
str[len] = '';
}
int myString::length() const {
return strlen(str);
}
ostream &cs_mystring::operator <<(ostream &out, const myString &s) {
out << s.str;
return out;
}
istream &cs_mystring::operator >> (istream &in, myString &s) {
in >> s.str;
return in;
}
bool myString::operator < (const myString &s) const {
return strcmp(str, s.str) < 0;
}
bool myString::operator <= (const myString &s) const {
return strcmp(str, s.str) <= 0;
}
bool myString::operator > (const myString &s) const {
return strcmp(str, s.str) > 0;
}
bool myString::operator >= (const myString &s) const {
return strcmp(str, s.str) >= 0;
}
bool myString::operator == (const myString &s) const {
return strcmp(str, s.str) == 0;
}
bool myString::operator != (const myString &s) const {
return strcmp(str, s.str) != 0;
}
bool cs_mystring::operator <(const char *cArr, const myString &s) {
return strcmp(cArr, s.str) < 0;
}
bool cs_mystring::operator <=(const char *cArr, const myString &s) {
return strcmp(cArr, s.str) <= 0;
}
bool cs_mystring::operator >(const char *cArr, const myString &s) {
return strcmp(cArr, s.str) > 0;
}
bool cs_mystring::operator >=(const char *cArr, const myString &s) {
return strcmp(cArr, s.str) >= 0;
}
bool cs_mystring::operator ==(const char *cArr, const myString &s) {
return strcmp(cArr, s.str) == 0;
}
bool cs_mystring::operator !=(const char *cArr, const myString &s) {
return strcmp(cArr, s.str) != 0;
}
char &myString::operator [](int i) const {
return str[i];
}
//main.cpp
#include "mystring.h"
#include <cctype> // for toupper()
#include <iostream>
#include <string>
using namespace std;
using namespace cs_mystring;
void BasicTest();
void RelationTest();
void CopyTest();
myString AppendTest(const myString& ref, myString val);
string boolString(bool convertMe);
int main()
{
BasicTest();
RelationTest();
CopyTest();
}
void BasicTest()
{
myString s;
cout << "----- Testing basic String creation & printing" << endl;
const myString strs[] =
{ myString("Wow"), myString("C++ is neat!"),
myString(""), myString("a-z") };
for (int i = 0; i < 4; i++) {
cout << "string [" << i << "] = " << strs[i] << endl;
}
cout << endl << "----- Testing access to characters (using const)" << endl;
const myString s1("abcdefghijklmnopqsrtuvwxyz");
cout << "Whole string is " << s1 << endl;
cout << "now char by char: ";
for (int i = 0; i < s1.length(); i++) {
cout << s1[i];
}
cout << endl << "----- Testing access to characters (using non-const)" << endl;
myString s2("abcdefghijklmnopqsrtuvwxyz");
cout << "Start with " << s2;
for (int i = 0; i < s2.length(); i++) {
s2[i] = toupper(s2[i]);
}
cout << " and convert to " << s2 << endl;
}
string boolString(bool convertMe) {
if (convertMe) {
return "true";
}
else {
return "false";
}
}
void RelationTest()
{
cout << " ----- Testing relational operators between myStrings ";
const myString strs[] =
{ myString("app"), myString("apple"), myString(""),
myString("Banana"), myString("Banana") };
for (int i = 0; i < 4; i++) {
cout << "Comparing " << strs[i] << " to " << strs[i + 1] << endl;
cout << " Is left < right? " << boolString(strs[i] < strs[i + 1]) << endl;
cout << " Is left <= right? " << boolString(strs[i] <= strs[i + 1]) << endl;
cout << " Is left > right? " << boolString(strs[i] > strs[i + 1]) << endl;
cout << " Is left >= right? " << boolString(strs[i] >= strs[i + 1]) << endl;
cout << " Does left == right? " << boolString(strs[i] == strs[i + 1]) << endl;
cout << " Does left != right ? " << boolString(strs[i] != strs[i + 1]) << endl;
}
cout << " ----- Testing relations between myStrings and char * ";
myString s("he");
const char *t = "hello";
cout << "Comparing " << s << " to " << t << endl;
cout << " Is left < right? " << boolString(s < t) << endl;
cout << " Is left <= right? " << boolString(s <= t) << endl;
cout << " Is left > right? " << boolString(s > t) << endl;
cout << " Is left >= right? " << boolString(s >= t) << endl;
cout << " Does left == right? " << boolString(s == t) << endl;
cout << " Does left != right ? " << boolString(s != t) << endl;
myString u("wackity");
const char *v = "why";
cout << "Comparing " << v << " to " << u << endl;
cout << " Is left < right? " << boolString(v < u) << endl;
cout << " Is left <= right? " << boolString(v <= u) << endl;
cout << " Is left > right? " << boolString(v > u) << endl;
cout << " Is left >= right? " << boolString(v >= u) << endl;
cout << " Does left == right? " << boolString(v == u) << endl;
cout << " Does left != right ? " << boolString(v != u) << endl;
}
myString AppendTest(const myString& ref, myString val)
{
val[0] = 'B';
return val;
}
void CopyTest()
{
cout << " ----- Testing copy constructor and operator= on myStrings ";
myString orig("cake");
myString copy(orig); // invoke copy constructor
copy[0] = 'f'; // change first letter of the *copy*
cout << "original is " << orig << ", copy is " << copy << endl;
myString copy2; // makes an empty string
copy2 = orig; // invoke operator=
copy2[0] = 'f'; // change first letter of the *copy*
cout << "original is " << orig << ", copy is " << copy2 << endl;
copy2 = "Copy Cat";
copy2 = copy2; // copy onto self and see what happens
cout << "after self assignment, copy is " << copy2 << endl;
cout << "Testing pass & return myStrings by value and ref" << endl;
myString val = "winky";
myString sum = AppendTest("Boo", val);
cout << "after calling Append, sum is " << sum << endl;
cout << "val is " << val << endl;
val = sum;
cout << "after assign, val is " << val << endl;
}
Correct output:
Explanation / Answer
bool myString::operator + (const myString &s) const {
const myString concatStr= myString('');
return strcat(strcpy(concatStr,*str),s.str);
}
bool myString::operator += (const myString &s) const {
const myString concatStr= myString('');
return strcpy(*str,strcat(strcpy(concatStr,*str),s.str));
}