Write a string class. To avoid conflicts with other similarly named classes, we will call our version MyString. This object is designed to make working with sequences of characters a little more convenient and less error-prone than handling raw c-strings, (although it will be implemented as a c-string behind the scenes). The MyString class will handle constructing strings, reading/printing, and accessing characters. In addition, the MyString object will have the ability to make a full deep-copy of itself when copied.

Respuesta :

Answer:

Check the explanation

Explanation:

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;

}

Kindly check the output below.

Ver imagen temmydbrain