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

Adding various operators to a string class? (C++) \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\

ID: 3669021 • Letter: A

Question

Adding various operators to a string class? (C++)

\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\

#include

#include
using namespace std;


class String
{
private:
   char strValue[250];
public:
   String()
   {
       strcpy(strValue, "");
   }


   String(char *strValue)
   {
       strcpy(this->strValue, strValue);
   }

   String(String &strObj)
   {
       strcpy(strValue, strObj.strValue);

   }

   int strCompare(String &strObj)
   {
       return strcmp(strValue, strObj.strValue);
   }


   char* getStringValue()
   {
       return strValue;
   }

   char *strConcat(String &strObj1, String &strObj2)
   {
       char str[250];
       strcat(strObj1.strValue, "");
       strcat(strObj1.strValue, strObj2.strValue);
       return strObj1.strValue;
   }

   char* strCopy(String &strObj1, String &strObj2)
   {
       return strcpy(strObj1.strValue, strObj2.strValue);
   }

  
   void strDisplay()
   {
       cout << this->strValue;

   }

   int strLength()
   {
       return strlen(strValue);
   }

};


int main()
{
   char *value = "First sentence going into the string.";

  
   String str(value);

  
   String str1("2nd string sentence");

  
   cout << "String object 1: ";
  
   str.strDisplay();
   cout << " " << endl;
   cout << endl;
   cout << "String Object 2: ";
   str1.strDisplay();
   cout << " " << endl;

  
   if (str1.strCompare(str1) == 0)
       cout << "Both the strings are equal " << endl;
   else
       if (str1.strCompare(str1) < 0)
           cout << "second string is greater than the first string object " << endl;

       else
           cout << "First string is greater than the second string object " << endl;
  


  
   cout << "Length of the first string object is: " << str.strLength() << endl;
   cout << " " << endl;
   cout << "Length of the second string object is: " << str1.strLength() << endl;
   cout << " " << endl;
  

  
   cout << "Contents in the first string object after calling concatination method: " << endl;
   str.strConcat(str, str1);
   str.strDisplay();
   cout << " " << endl;
  

  
   String str2("Another sentence");
   String str3;

  
   cout << "Contents of the third string: " << endl;
   str2.strDisplay();
   cout << " " << endl;
   cout << "Contents of the fourth string: " << endl;
   str3.strDisplay();
   cout << " " << endl;
      
  
   str3.strCopy(str3, str2);

  
   cout << "Copying the third string content into the fourth string" << endl;
   str3.strDisplay();
  

  
   return 0;


}

\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\

This is the existing program. The assignment is asking to add additional operators to the string class.

Modify Program One to add the following:

Assignment operator (“=”).

Comparison operators (“<”, “<=”, “==”, “!=”, “>=”, “>”)

Concatenation operator (“&”).

Concatenation and assignment operator (“&=”)

Input operator (“>>”).

Output operator (“<<”).

Above items 1 through 4 should have versions that work with a String object and a “C” type string on the right of the operator.

Items 5 and 6 require a String object to the right of the operator.

Items 1 and 4 will require a String object on the left of the operator.

Items 2 and 3 should have versions that allow both a String object and a “C” type string on the left of the operator.

Item 3 does not modify the object to the left of the operator.

Explanation / Answer

#include <iostream>
#include <string.h>
using namespace std;

class String
{
private:
   char strValue[250];
public:
   String()
   {
       strcpy(strValue, "");
   }

   String(char *strValue)
   {
       strcpy(this->strValue, strValue);
   }
   String(String &strObj)
   {
       strcpy(strValue, strObj.strValue);
   }
   int strCompare(String &strObj)
   {
       return strcmp(strValue, strObj.strValue);
   }

   char* getStringValue()
   {
       return strValue;
   }
   char *strConcat(String &strObj1, String &strObj2)
   {
       char str[250];
       strcat(strObj1.strValue, "");
       strcat(strObj1.strValue, strObj2.strValue);
       return strObj1.strValue;
   }
   char* strCopy(String &strObj1, String &strObj2)
   {
       return strcpy(strObj1.strValue, strObj2.strValue);
   }
  
   void strDisplay()
   {
       cout << this->strValue;
   }
   int strLength()
   {
       return strlen(strValue);
   }

   void operator=(const char s[])
   {
       strcpy(strValue, s);
   }
   bool operator <(const String& s)
   {
       if(strcmp(this->strValue, s.strValue) < 0) return true;
       else return false;
   }
   bool operator <=(const String& s)
   {
       if(strcmp(this->strValue, s.strValue) <= 0) return true;
       else return false;
   }
   bool operator ==(const String& s)
   {
       if(strcmp(this->strValue, s.strValue) == 0) return true;
       else return false;
   }
   bool operator !=(const String& s)
   {
       if(strcmp(this->strValue, s.strValue) == 0) return false;
       else return true;
   }
   bool operator >=(const String& s)
   {
       if(strcmp(this->strValue, s.strValue) >= 0) return true;
       else return false;
   }
   bool operator >(const String& s)
   {
       if(strcmp(this->strValue, s.strValue) > 0) return true;
       else return false;
   }
   String operator &(const String& s1)
   {
       char *temp = this->strValue;
       strcat(temp, s1.strValue);
       String s(temp);
       return s;
   }
   void operator &=(const String& s1)
   {
       strcat(this->strValue, s1.strValue);
   }
   friend istream &operator>>(istream &input, String &s)
   {
       input >> s.strValue;
       return input;
   }
   friend ostream &operator<<(ostream &output, String &s)
   {
       output << s.strValue;
       return output;
   }
};

int main(){
   char *value = "First sentence going into the string.";

   String str(value);

   String str1("2nd string sentence");

   cout << "String object 1: ";

   str.strDisplay();
   cout << " " << endl;
   cout << endl;
   cout << "String Object 2: ";
   str1.strDisplay();
   cout << " " << endl;
  
   if (str1.strCompare(str1) == 0)
       cout << "Both the strings are equal " << endl;
   else
       if (str1.strCompare(str1) < 0)
           cout << "second string is greater than the first string object " << endl;
       else
           cout << "First string is greater than the second string object " << endl;
  

  
   cout << "Length of the first string object is: " << str.strLength() << endl;
   cout << " " << endl;
   cout << "Length of the second string object is: " << str1.strLength() << endl;
   cout << " " << endl;


   cout << "Contents in the first string object after calling concatination method: " << endl;
   str.strConcat(str, str1);
   str.strDisplay();
   cout << " " << endl;
  
  
   String str2("Another sentence");
   String str3;

   cout << "Contents of the third string: " << endl;
   str2.strDisplay();
   cout << " " << endl;
   cout << "Contents of the fourth string: " << endl;
   str3.strDisplay();
   cout << " " << endl;
  

   str3.strCopy(str3, str2);

   cout << "Copying the third string content into the fourth string" << endl;
   str3.strDisplay();


   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