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;
#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;
}
Comments
Leave a comment