Answer to Question #263467 in C++ for sam

Question #263467

Consider the myString class, which is having only one privately declared data item: string str; OR char str[MAX]; and publically declared constructors and required member functions. Write down a member function that subtracts one object’s string from another object’s string. Sample calling statements are given as under. myString S1, S2, S3; S1.readd(); S2.readd(); S3 = S1 – S2;


1
Expert's answer
2021-11-10T06:34:28-0500
#include <cstring>
#include <iostream>
using namespace std;
class myString {
	char* str;


public:
	myString();
	myString(char* S1);
	myString(const myString& source);
	myString(myString&& source);
};
myString::myString()
	: str{ nullptr }
{
	str = new char[1];
	str[0] = '\0';
}
myString::myString(char* S1)
{
	if (S1 == nullptr) {
		str = new char[1];
		str[0] = '\0';
	}


	else {


		str = new char[strlen(S1) + 1];
		strcpy(str, S1);
cout << "The string passed is: "
			<< str << endl;
	}
}
myString::myString(const myString& S2)
{
	str = new char[strlen(S2.str) + 1];
	strcpy(str, S2.str);
}
myString::myString(myString&& S2)
{
	str = S2.str;
	S2.str = nullptr;
}
int main()
{
	myString a;
	char temp[] = "Hello";
	myString b{ temp };
	myString c{ a };


	char temp1[] = "World";
	myString d{ myString{ temp } };
	return 0;
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS