Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

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

/** * @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 #include #include #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] != '\0') { str[pos] = s[pos]; ++pos; } str[pos] = '\0'; 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] = '\0'; }

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] = '\0'; } //// 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] != '\0') ++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] != '\0' && 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] != '\0' && rhs.str[pos] != '\0' && 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] != '\0'){ 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] != '\0'){ str[offset ++] = rhs.str[pos]; ++pos; } str[offset++] = '\0'; 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; } }

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

SQL For Data Science Data Cleaning Wrangling And Analytics With Relational Databases

Authors: Antonio Badia

1st Edition

3030575918, 978-3030575915

More Books

Students also viewed these Databases questions