myString. h: #include using namespace std; #ifndef _MYSTRING_H_ #define _MYSTRING_H_ class myString { private: char *str; public: myString(); myString(const char *s); void operator = (myString &s); int length(); friend ostream &operator <<(ostream &out, myString &s); friend istream &operator >>(istream &out, myString &s); bool operator <(myString &s); bool operator <=(myString &s); bool operator >(myString &s); bool operator >=(myString &s); bool operator ==(myString &s); bool operator !=(myString &s); }; #endif myString. cpp: #include "myString. h" #include myString::myString() { str = new char; str[0] = '\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] = '\0'; } 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()] = '\0'; } int myString::length(){ return strlen(str); } ostream &operator <<(ostream &out, myString &s){ out << s.str; return out; } istream &operator >>(istream &in, myString &s) { in >> s.str; return in; } bool myString::operator < (myString &s) { return strcmp(str, s.str) < 0; } bool myString::operator <= (myString &s) { return strcmp(str, s.str) <= 0; } bool myString::operator > (myString &s) { return strcmp(str, s.str) > 0; } bool myString::operator >= (myString &s) { return strcmp(str, s.str) >= 0; } bool myString::operator == (myString &s) { return strcmp(str, s.str) == 0; } bool myString::operator != (myString &s) { return strcmp(str, s.str) != 0; } main. cpp: #include "myString.h" #include using namespace std; int main() { myString s1, s2; cout << "Enter first string: "; cin >> s1; cout << "Enter second string: "; cin >> s2; cout << "you entered: " << s1 << " and " << s2 << endl; myString s3 = s1; cout << "Value of s3 is " << s3 << endl; if(s1 < s2) { cout << s1 << " < " << s2 << endl; } if(s1 <= s2) { cout << s1 << " <= " << s2 << endl; } if(s1 > s2) { cout << s1 << " > " << s2 << endl; } if(s1 >= s2) { cout << s1 << " >= " << s2 << endl; } if(s1 == s2) { cout << s1 << " == " << s2 << endl; } if(s1 != s2) { cout << s1 << " != " << s2 << endl; } return 0; } 2. A read() function The read() function will allow the client programmer to specify the delimiting character (the character at which reading will stop). It should work just like the getline() function works for c-strings; that is, it should place everything up to but not including the delimiting character into the calling object, and it should also consume (and discard) the delimiting character. This will be a void function that will take two arguments, a stream and the delimiting character. It should not skip leading spaces. The limit of 127 characters imposed on the >> function above also applies to this function. Hint: Don't try to read character by character in a loop. Use the in.getline() function to read the input into a non-dynamic array, then use strcpy() to copy it into your data member.
myString. h:
#include <iostream>
using namespace std;
#ifndef _MYSTRING_H_
#define _MYSTRING_H_
class myString
{
private:
char *str;
public:
myString();
myString(const char *s);
void operator = (myString &s);
int length();
friend ostream &operator <<(ostream &out, myString &s);
friend istream &operator >>(istream &out, myString &s);
bool operator <(myString &s);
bool operator <=(myString &s);
bool operator >(myString &s);
bool operator >=(myString &s);
bool operator ==(myString &s);
bool operator !=(myString &s);
};
#endif
myString. cpp:
#include "myString. h"
#include <cstring>
myString::myString()
{
str = new char;
str[0] = '\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] = '\0';
}
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()] = '\0';
}
int myString::length(){
return strlen(str);
}
ostream &operator <<(ostream &out, myString &s){
out << s.str;
return out;
}
istream &operator >>(istream &in, myString &s)
{
in >> s.str;
return in;
}
bool myString::operator < (myString &s)
{
return strcmp(str, s.str) < 0;
}
bool myString::operator <= (myString &s)
{
return strcmp(str, s.str) <= 0;
}
bool myString::operator > (myString &s)
{
return strcmp(str, s.str) > 0;
}
bool myString::operator >= (myString &s)
{
return strcmp(str, s.str) >= 0;
}
bool myString::operator == (myString &s)
{
return strcmp(str, s.str) == 0;
}
bool myString::operator != (myString &s)
{
return strcmp(str, s.str) != 0;
}
main. cpp:
#include "myString.h"
#include <iostream>
using namespace std;
int main()
{
myString s1, s2;
cout << "Enter first string: ";
cin >> s1;
cout << "Enter second string: ";
cin >> s2;
cout << "you entered: " << s1 << " and " << s2 << endl;
myString s3 = s1;
cout << "Value of s3 is " << s3 << endl;
if(s1 < s2)
{
cout << s1 << " < " << s2 << endl;
}
if(s1 <= s2)
{
cout << s1 << " <= " << s2 << endl;
}
if(s1 > s2)
{
cout << s1 << " > " << s2 << endl;
}
if(s1 >= s2)
{
cout << s1 << " >= " << s2 << endl;
}
if(s1 == s2)
{
cout << s1 << " == " << s2 << endl;
}
if(s1 != s2)
{
cout << s1 << " != " << s2 << endl;
}
return 0;
}
2. A read() function
The read() function will allow the client programmer to specify the delimiting character (the character at which reading will stop). It should work just like the getline() function works for c-strings; that is, it should place everything up to but not including the delimiting character into the calling object, and it should also consume (and discard) the delimiting character. This will be a void function that will take two arguments, a stream and the delimiting character. It should not skip leading spaces. The limit of 127 characters imposed on the >> function above also applies to this function.
Hint: Don't try to read character by character in a loop. Use the in.getline() function to read the input into a non-dynamic array, then use strcpy() to copy it into your data member.
![](/static/compass_v2/shared-icons/check-mark.png)
Step by step
Solved in 2 steps
![Blurred answer](/static/compass_v2/solution-images/blurred-answer.jpg)
![Computer Networking: A Top-Down Approach (7th Edi…](https://www.bartleby.com/isbn_cover_images/9780133594140/9780133594140_smallCoverImage.gif)
![Computer Organization and Design MIPS Edition, Fi…](https://www.bartleby.com/isbn_cover_images/9780124077263/9780124077263_smallCoverImage.gif)
![Network+ Guide to Networks (MindTap Course List)](https://www.bartleby.com/isbn_cover_images/9781337569330/9781337569330_smallCoverImage.gif)
![Computer Networking: A Top-Down Approach (7th Edi…](https://www.bartleby.com/isbn_cover_images/9780133594140/9780133594140_smallCoverImage.gif)
![Computer Organization and Design MIPS Edition, Fi…](https://www.bartleby.com/isbn_cover_images/9780124077263/9780124077263_smallCoverImage.gif)
![Network+ Guide to Networks (MindTap Course List)](https://www.bartleby.com/isbn_cover_images/9781337569330/9781337569330_smallCoverImage.gif)
![Concepts of Database Management](https://www.bartleby.com/isbn_cover_images/9781337093422/9781337093422_smallCoverImage.gif)
![Prelude to Programming](https://www.bartleby.com/isbn_cover_images/9780133750423/9780133750423_smallCoverImage.jpg)
![Sc Business Data Communications and Networking, T…](https://www.bartleby.com/isbn_cover_images/9781119368830/9781119368830_smallCoverImage.gif)