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

In C++ write the String & operator=(String); function form the Header file and i

ID: 3803152 • Letter: I

Question

In C++ write the String & operator=(String); function form the Header file and implement it into the .cpp File.

--------------------------------------------------------------------------------
string.hpp
--------------------------------------------------------------------------------
#ifndef STRING_HPP
#define STRING_HPP

#include <iostream>

/**
* @invariant str[length()] == 0
* && length() == capacity()
* && capacity() == stringSize - 1
*/
class String {
private:
// helper constructors and methods
String(int);
String(int, const char *);
void reset_capacity (int);

char * str;

// size includes NULL terminator
int STRING_SIZE;

public:

// constructor: empty string, String('x'), and String("abcd")
String();
String(char);
String(const char *);

// copy ctor, destructor, constant time swap, and assignment
String(const String &);
//~String();
void swap (String &);
String & operator= (String);

// subscript: accessor/modifier and accessor
char & operator[](int);
char operator[](int) const;

// max chars that can be stored (not including null terminator)
int capacity() const;
// number of char in string
int length () const;

// concatenation
String operator+ (const String &) const;
String & operator+=(String);

// relational methods
bool operator==(const String &) const;
bool operator< (const String &) const;

// i/o
friend std::ostream& operator<<(std::ostream &, const String &);
friend std::istream& operator>>(std::istream &, String &);

};

// free functios for concatenation and relational
String operator+ (const char *, const String &);
String operator+ (char, const String &);
bool operator== (const char *, const String &);
bool operator== (char, const String &);
bool operator< (const char *, const String &);
bool operator< (char, const String &);
bool operator<= (const String &, const String &);
bool operator!= (const String &, const String &);
bool operator>= (const String &, const String &);
bool operator> (const String &, const String &);

#endif

---------------------------------------------------------------------------------------------------------------------------
string.cpp
---------------------------------------------------------------------------------------------------------------------------
#include <iostream>
#include <cstring>
#include <cassert>
#include "string.hpp"
//private constructors impementation
// // helper constructors and methods
String::String(int)
{
   STRING_SIZE=0;
   }
String::String(int c, const char *s)
{
   int pos = 0;
   while(s[pos] != '')
   {
   str[pos] = s[pos];
   ++pos;
   }
   str[pos] = '';  
   STRING_SIZE=pos;
}
void String::reset_capacity(int s)
{
   STRING_SIZE=s;
}

//// constructor: empty string, String('x'), and String("abcd")
String::String(){
str = NULL;
}

String::String(char ch){
str[0] = ch;
str[1] = '';
}

String::String(const char* chars){
STRING_SIZE = strlen(chars)+1 ;
str = new char[STRING_SIZE];
for(int i = 0; i < STRING_SIZE-1; i++)
{
str[i] = chars[i];
}
str[STRING_SIZE-1] = '';
  
}
//// copy ctor, destructor, constant time swap, and assignment
String::String(const String &s)
{
   STRING_SIZE=s.length();
   str=s.str;
}
// number of char in string

int String::length() const{
int size = 0;
while(str[size] != '')
++size;
return size;
  
}
// max chars that can be stored (not including null terminator)
int String::capacity() const{
return STRING_SIZE-1;
}
// subscript: accessor/modifier and accessor
char& String::operator[](int i){
assert(i >= 0);
assert(i <= length());
  
return str[i];
}

void String::swap(String& str1){
int tempStr = STRING_SIZE;
STRING_SIZE = str1.STRING_SIZE;
str1.STRING_SIZE = tempStr;

char* tempStr1 = str;
str = str1.str;
str1.str = tempStr1;
}

char String::operator[](int i) const{
assert(i >= 0);
assert(i <= length());
  
return str[i];
}
// relational methods
bool String::operator==(const String& rhs) const{
int pos = 0;
while(str[pos] != '' && str[pos] == rhs.str[pos]){
++pos;
}
return str[pos] == rhs.str[pos];
}
// i/o
std::istream& operator>>(std::istream& in, String& rhs){
in >> rhs.str;
return in;
}

std::ostream& operator<<(std::ostream& out, const String& rhs){
out << rhs.str;
return out;
}
// relational methods
bool String::operator<(const String& rhs) const{
int pos = 0;
while(str[pos] != '' && rhs.str[pos] != '' && str[pos] == rhs.str[pos]){
++pos;
}
return str[pos] < rhs.str[pos];
}

// free functios for concatenation and relational
String String::operator+(const String& rhs) const{
String result(str);
int offset = length();
int pos = 0;
// concatenation
while(rhs.str[pos] != ''){
result.str[offset + pos] = rhs.str[pos];
++pos;
}
result.str[offset + pos] = 0;
return result;
}

String& String::operator+=(String rhs){
int offset = length();
int pos = 0;
  
while(rhs.str[pos] != ''){
str[offset ++] = rhs.str[pos];
++pos;
}
str[offset++] = '';
reset_capacity(offset);
return *this;
}

String operator+(const char charArray[], const String& rhs){
   String s(charArray);
   return rhs + s ;
}

String operator+(char s, const String& rhs){
return s + rhs;
}

bool operator==(const char charArray[], const String& rhs){
if(charArray == rhs){
return true;
}
else{
return false;
}
}

bool operator==(char s, const String& rhs){
if(s == rhs){
return true;
}
else{
return false;
}
}

bool operator<(const char charArray[], const String& rhs){
if(charArray < rhs){
return true;
}
else{
return false;
}
}

bool operator<(char s, const String& rhs){
if(s < rhs){
return true;
}
else{
return false;
}
}

bool operator<=(const String& lhs, const String& rhs){
if(lhs < rhs || lhs == rhs){
return true;
}
else{
return false;
}
}

bool operator!=(const String& lhs, const String& rhs){
if(lhs.length() != rhs.length()){
return true;
}
int pos = 0;
while(lhs[pos] != rhs[pos]){
pos++;
}
if(pos == lhs.length()){
return true;
}
return false;
}

bool operator>=(const String& lhs, const String& rhs){
if(lhs > rhs || lhs == rhs) {
return true;
}
else{
return false;
}
}

bool operator>(const String& lhs, const String& rhs){
if(!(lhs <= rhs)){
return true;
}
else{
return false;
}
}

Explanation / Answer

Here is the code for String.cpp with operator= overloaded:

#include <iostream>
#include <cstring>
#include <cassert>
#include "string.hpp"
//private constructors impementation
// // helper constructors and methods
String::String(int)
{
STRING_SIZE=0;
}
String::String(int c, const char *s)
{
int pos = 0;
while(s[pos] != '')
{
str[pos] = s[pos];
++pos;
}
str[pos] = '';
STRING_SIZE=pos;
}
void String::reset_capacity(int s)
{
STRING_SIZE=s;
}
//// constructor: empty string, String('x'), and String("abcd")
String::String(){
str = NULL;
}
String::String(char ch){
str[0] = ch;
str[1] = '';
}
String::String(const char* chars){
STRING_SIZE = strlen(chars)+1 ;
str = new char[STRING_SIZE];
for(int i = 0; i < STRING_SIZE-1; i++)
{
str[i] = chars[i];
}
str[STRING_SIZE-1] = '';
  
}
//// copy ctor, destructor, constant time swap, and assignment
String::String(const String &s)
{
STRING_SIZE=s.length();
str=s.str;
}
// number of char in string
int String::length() const{
int size = 0;
while(str[size] != '')
++size;
return size;
  
}
// max chars that can be stored (not including null terminator)
int String::capacity() const{
return STRING_SIZE-1;
}
// subscript: accessor/modifier and accessor
char& String::operator[](int i){
assert(i >= 0);
assert(i <= length());
  
return str[i];
}
void String::swap(String& str1){
int tempStr = STRING_SIZE;
STRING_SIZE = str1.STRING_SIZE;
str1.STRING_SIZE = tempStr;
char* tempStr1 = str;
str = str1.str;
str1.str = tempStr1;
}
char String::operator[](int i) const{
assert(i >= 0);
assert(i <= length());
  
return str[i];
}
String& String::operator=(String other)
{
   STRING_SIZE = other.STRING_SIZE;
   for(int i = 0; i < STRING_SIZE; i++)
       str[i] = other.str[i];
   return *this;  
}

// relational methods
bool String::operator==(const String& rhs) const{
int pos = 0;
while(str[pos] != '' && str[pos] == rhs.str[pos]){
++pos;
}
return str[pos] == rhs.str[pos];
}
// i/o
std::istream& operator>>(std::istream& in, String& rhs){
in >> rhs.str;
return in;
}
std::ostream& operator<<(std::ostream& out, const String& rhs){
out << rhs.str;
return out;
}
// relational methods
bool String::operator<(const String& rhs) const{
int pos = 0;
while(str[pos] != '' && rhs.str[pos] != '' && str[pos] == rhs.str[pos]){
++pos;
}
return str[pos] < rhs.str[pos];
}
// free functios for concatenation and relational
String String::operator+(const String& rhs) const{
String result(str);
int offset = length();
int pos = 0;
// concatenation
while(rhs.str[pos] != ''){
result.str[offset + pos] = rhs.str[pos];
++pos;
}
result.str[offset + pos] = 0;
return result;
}
String& String::operator+=(String rhs){
int offset = length();
int pos = 0;
  
while(rhs.str[pos] != ''){
str[offset ++] = rhs.str[pos];
++pos;
}
str[offset++] = '';
reset_capacity(offset);
return *this;
}
String operator+(const char charArray[], const String& rhs){
String s(charArray);
return rhs + s ;
}
String operator+(char s, const String& rhs){
return s + rhs;
}
bool operator==(const char charArray[], const String& rhs){
if(charArray == rhs){
return true;
}
else{
return false;
}
}
bool operator==(char s, const String& rhs){
if(s == rhs){
return true;
}
else{
return false;
}
}
bool operator<(const char charArray[], const String& rhs){
if(charArray < rhs){
return true;
}
else{
return false;
}
}
bool operator<(char s, const String& rhs){
if(s < rhs){
return true;
}
else{
return false;
}
}
bool operator<=(const String& lhs, const String& rhs){
if(lhs < rhs || lhs == rhs){
return true;
}
else{
return false;
}
}
bool operator!=(const String& lhs, const String& rhs){
if(lhs.length() != rhs.length()){
return true;
}
int pos = 0;
while(lhs[pos] != rhs[pos]){
pos++;
}
if(pos == lhs.length()){
return true;
}
return false;
}
bool operator>=(const String& lhs, const String& rhs){
if(lhs > rhs || lhs == rhs) {
return true;
}
else{
return false;
}
}
bool operator>(const String& lhs, const String& rhs){
if(!(lhs <= rhs)){
return true;
}
else{
return false;
}
}