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

Description: Modify following string. h and string.cpp by using dynamic array an

ID: 3539277 • Letter: D

Question

Description:

Modify following string. h and string.cpp by using dynamic array and add the "big three"( destructor, copy constructor, and assignment operator overloading).

--------------string.h-------------------------------------------------------------

#ifndef _MYSTRING_H

#define _MYSTRING_H

#include<iostream>

#include <cstring> // for strlen(), etc.

using namespace std;


#define MAX_STR_LENGTH 200



class String {

public:

String();

String(const char s[]); // a conversion constructor

void append(const String &str);


// Relational operators

bool operator ==(const String &str) const;

bool operator !=(const String &str) const;

bool operator >(const String &str) const;

bool operator <(const String &str) const;


bool operator >=(const String &str) const;


String operator +=(const String &str);


void print(ostream &out) const;

int length() const;


char operator [](int i) const; // subscript operator


private:

char contents[MAX_STR_LENGTH+1];

int len;

};


ostream & operator<<(ostream &out, const String & r); // overload ostream operator "<<" - External!



#endif /* not defined _MYSTRING_H */

-----------------string.cpp--------------------------------------------------------------------------------------------

#include "mystring.h"


String::String()

{

contents[0] = '';

len = 0;

}


String::String(const char s[])

{

len = strlen(s);

strcpy(contents, s);

}




void String::append(const String &str)

{

strcat(contents, str.contents);


len += str.len;


}



bool String::operator ==(const String &str) const

{

return strcmp(contents, str.contents) == 0;

}


bool String::operator !=(const String &str) const

{

return strcmp(contents, str.contents) != 0;

}


bool String::operator >(const String &str) const

{

return strcmp(contents, str.contents) > 0;

}


bool String::operator <(const String &str) const

{

return strcmp(contents, str.contents) < 0;

}



bool String::operator >=(const String &str) const

{

return strcmp(contents, str.contents) >= 0;

}



String String::operator +=(const String &str)

{

append(str);

return *this;

}


void String::print(ostream &out) const

{

out << contents;

}


int String::length() const

{

return len;

}


char String::operator [](int i) const

{

if (i < 0 || i >= len) {

cerr << "can't access location " << i

   << " of string "" << contents << """ << endl;

return '';

}

return contents[i];

}


ostream & operator<<(ostream &out, const String & s) // overload ostream operator "<<" - External!

{

s.print(out);

return out;

}

Explanation / Answer

#ifndef _MYSTRING_H
#define _MYSTRING_H
#include<iostream>
#include <cstring> // for strlen(), etc.
using namespace std;

#define MAX_STR_LENGTH 200
class String {
public:
String();
~String();
String(const char s[]); // a conversion constructor
String(String& str);// copy constructor
void append(const String &str);
// Relational operators
bool operator ==(const String &str) const;   
bool operator !=(const String &str) const;  
string operator =(const string& str);
bool operator >(const String &str) const;   
bool operator <(const String &str) const;   
bool operator >=(const String &str) const;
String operator +=(const String &str);     
void print(ostream &out) const;   
int length() const;
char operator [](int i) const; // subscript operator
private:
    char *contents;
    int len;
};
ostream & operator<<(ostream &out, const String & r); // overload ostream operator "<<" - External!
#endif /* not defined _MYSTRING_H */

-----------------string.cpp--------------------------------------------------------------------------------------------

#include "mystring.h"
String::String()
{
contents=new char[MAX_STR_LENGTH+1]
contents[0] = '';
len = 0;
}

~String()
{
       delete[] contents;
}
String(String& str)
{
    char *data=new char[strlen(str)+1];
    strcpy(data,str);
    return data;
}

String::String(const char s[])
{
len = strlen(s);
strcpy(contents, s);
}

void String::append(const String &str)
{
strcat(contents, str.contents);
len += str.len;
}

bool String::operator ==(const String &str) const
{
return strcmp(contents, str.contents) == 0;
}

bool String::operator !=(const String &str) const
{
return strcmp(contents, str.contents) != 0;
}

string operator= (const string& str)
{
    char *data=new char[strlen(contents)+1];
    strcpy(data,contents);
    return data;
}
bool String::operator =(const String &str) const
{
return strcmp(contents, str.contents) != 0;
}


bool String::operator >(const String &str) const
{
return strcmp(contents, str.contents) > 0;
}

bool String::operator <(const String &str) const
{
return strcmp(contents, str.contents) < 0;
}

bool String::operator >=(const String &str) const
{
return strcmp(contents, str.contents) >= 0;
}

String String::operator +=(const String &str)
{
append(str);
return *this;
}

void String::print(ostream &out) const
{
out << contents;
}

int String::length() const
{
return len;
}

char String::operator [](int i) const
{
if (i < 0 || i >= len) {
    cerr << "can't access location " << i
         << " of string "" << contents << """ << endl;
    return '';
}
return contents[i];
}

ostream & operator<<(ostream &out, const String & s) // overload ostream operator "<<" - External!
{
s.print(out);
return out;
}