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

Programming Question C++ Question 4: [40 points] the following code declares a s

ID: 3840316 • Letter: P

Question

Programming Question C++

Question 4: [40 points] the following code declares a skeleton for a String class. An object of that class, would store, as a data member, the characters, including the NULL one, of a C++ character string (c-string) as well as the length of that string. An empty string object must be represented as follows:

class String {

friend String operator+ (________________________); // concatenation operator

friend bool operator= = (_______________________); // comparison operator

friend ostream & operator<< (_______________________________); // insertion  

public:

            String (const char * = “”);   // String default Constructor. It initializes the

                                                         // string object to null or to the characters of a c-string

            String (_____________________________);                     // copy constructor

            ~String ( );                                            // Class destructor

String & Operator= (_____________)         // overloading the assignment operator

String operator ( ) (int i , int n); // returns a string object which contains the

     // c-string that begins at i and of length n.            

private:

int length;                        // number of characters in the string (without the NULL).

          char * pchars;                 // pointer to the array that stores the string

     // characters. The array of characters is terminated

     // by the NULL character.

};

Complete the declaration of the String class

Implement the default constructor

3.Implement the copy constructor of the class

Overload the assignment operator “=” as a member function of the String class

Overload the operator “( )” which takes, as shown below, an index i and a length n and returns a string object that stores c-string that starts at i and of length n.

6.Overload the insertion operator “<<” as a friend function for the String class

Overload the string comparison operator “= =” as a friend function. That is, the comparison of two String objects should return the Boolean true, if the two objects are the same, false otherwise.

8.Overload the string concatenation operator “+” as a friend function. That is, the concatenation of two String objects should result in appending the right-hand-side string object to the “left-hand-side” String object and storing the result in a new string object that is returned by the operator function.

Implement an appropriate driver program that test the developed String class. Your program should test all of the implemented functions. In your testing, you may use the following string objects.

S: the empty string

S1 = {“ABC”}

S3 = {“DEFG”}

As an example of testing, the following sample code can be used. Of course, more test cases are needed and should be used.

            void main ( ) {

                        String S;

                        cout << S;        // prints out the following statement: This string is empty.

                      

                        String S1 (“ABC”);

                        cout << S1;     // prints out ABC

                      String S2 (S1);   

                        cout << S2; // prints out ABC

                       String S3 = “DEFG”;

                       cout << (S1 + S3) << endl;

    

           }

Explanation / Answer

#include <string.h>
#include <iostream.h>


class String
{
public:
String(char *x =0);
String(const String &);
~String();
String& operator =(const String&);
int operator ==(const String&);
int operator !=(const String&);
String operator +=(const String&);
char* c_str()const;
int length();
String readLine(istream&);//read to end of line
istream& read(istream&); //read to white space
ostream& write(ostream &)const;
private:
char *_data;
int _length;
};

String operator + (const String&,const String&);

ostream& operator <<(ostream &out,const String &s);

istream& operator >> (istream&, String&);


String::String(char *x )
{
if(x)
{
_length = strlen(x);
_data = new char[_length+1];
strcpy(_data,x);
}
else
{
_data = NULL;
_length = 0;
}
}
String::String(const String &x)
{
if(x._length >0)
{
_length = x._length;
_data = new char[_length+1];
strcpy(_data,x._data);
}
else
{
_length=0;
_data = NULL;
}
}
String::~String()
{
if(_data)
delete [] _data;
}

String& String::operator =(const String &x)
{
if(x._length >0)
{
_length = x._length;
char *temp = new char[_length+1];
strcat(temp,x._data);
if(_data)
delete [] _data;
_data = temp;
}
else
{
_length=0;
if(_data)
delete [] _data;
_data = NULL;
}
return *this;
}
int String::operator ==(const String &s)
{
return !strcmp(_data,s._data);
}
int String::operator !=(const String &s)
{
return strcmp(_data,s._data);
}
String String::operator +=(const String &s)
{
_length += s._length;

char *temp = _data;
_data = new char[_length +1];
strcpy(_data,temp);
strcat(_data,s._data);
delete [] temp;
return *this;
}
char* String::c_str()const
{
return _data;
}
int String::length()
{
return _length;
}
String String::readLine(istream &in)
{
long int eoln;
   char inval[255];
   char *ptr;
   do
   {
       in.getline(inval,255);
       ptr = strchr(inval,'');
       eoln = ptr -inval;
   } while (!eoln && !in.eof());
   if(_data)
delete [] _data;
_length = strlen(inval);
_data = new char[_length + 1];
strcpy(_data,inval);
return *this;
}
istream& String::read(istream &in)
{
   char inval[255];
inval[0]='';
char c;
//in >> noskipws;

do{
in>>c;
if(c != ' ')
{
char temp[2];
temp[0] = c;
temp[1]='';
strcat(inval,temp);
}
}while(c!=' ');
_length = strlen(inval);
_data = new char[_length + 1];
strcpy(_data,inval);
return in;

}
ostream& String::write(ostream &out)const
{
out<<_data;
return out;
}

String operator + (const String &s1,const String &s2)
{
String c = s1;
c += s2;
return c;
}

ostream& operator <<(ostream &out,const String &s)
{
return s.write(out);
}

istream& operator >> (istream &in, String &s)
{
return s.read(in);
}

void main ( ) {
String S;
cout << S; // prints out the following statement: This string is empty.
  
String S1 ("ABC");
cout << S1; // prints out ABC

String S2 (S1);   
cout << S2; // prints out ABC

String S3 = "DEFG";
cout << (S1 + S3) << endl;
  
}