Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Complete the following functions so that program outputs matches the expected ou

ID: 3548150 • Letter: C

Question

Complete the following functions so that program outputs matches the expected output.

#include <iostream>
#include <cstring>
using namespace std;

class MyString
{
private:
char* text;
public:
MyString()
{
cout << "MyString: default ctor" << endl;
text = new char[1];
text[0] = '';
}
~MyString()
{
delete [] text;
}
MyString(const MyString& str)
{
// added your code here

}
MyString(const char *cstr)
{
// added your code here

}
MyString& operator= (const MyString& str)
{
// added your code here

}
operator char*() const
{
// added your code here

}
void Display() const
{
cout << text << endl;
}

const MyString& operator+= (const MyString &other)
{
// added your code here

}
const MyString operator+(const MyString &other) const
{
// added your code here

}

friend ostream& operator<<(ostream& os, const MyString& str)
{
// added your code here

}

friend istream& operator>>(istream& is, MyString& str)
{
// added your code here

}
};

int main()
{
MyString str = "This is a test";
cout << str;
cin >> str;      
cout << str;
MyString str2 = "SJSU";
str2 = str;
cout << str2;

MyString str3 = "USF";
str = str + str3;
cout << str;
return 0;
}

The input:

SSD

The expected output:

This is a test
SSD
SSD
SSDUSF

Explanation / Answer

#include <iostream>
#include <cstring>
using namespace std;
class MyString
{
private:
char* text;
public:
MyString()
{
cout << "MyString: default ctor" << endl;
text = new char[1];
text[0] = '';
}
~MyString()
{
delete [] text;
}
MyString(const MyString& str)
{
// added your code here
int length = strlen(str.text);
text = new char[length+1];
strcpy(text,str.text);
}
MyString(const char *cstr)
{
// added your code here
int length = strlen(cstr);
text = new char[length+1];
strcpy(text,cstr);
}
MyString& operator= (const MyString& str)
{
// added your code here
int length = strlen(str.text);
text = new char[length+1];
strcpy(text,str.text);
return *this;
}
operator char*() const
{
// added your code here
return text;
}
void Display() const
{
cout << text << endl;
}

const MyString& operator+= (const MyString &other)
{
// added your code here
char* temp = new char[strlen(text)+1];
strcpy(temp,text);
delete[] text;
text = new char[strlen(temp)+strlen(other.text)+1];
strcpy(text,temp);
strcat(text,other.text);
}
const MyString operator+(const MyString &other) const
{
// added your code here
MyString temp = *this;
temp.text = new char[ strlen(text)+strlen(other.text)+1 ];
strcpy(temp.text,text);
strcat(temp.text,other.text);
return temp;
}

friend ostream& operator<<(ostream& os, const MyString& str)
{
// added your code here
os << str.text << endl;
return os;
}

friend istream& operator>>(istream& is, MyString& str)
{
// added your code here
is >> str.text;
return is;
}
};

int main()
{
MyString str = "This is a test";
cout << str;
cin >> str;
cout << str;
MyString str2 = "SJSU";
str2 = str;
cout << str2;
MyString str3 = "USF";
str = str + str3;
cout << str;
return 0;
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote