For this computer assignment, you are to design and implement a user-defined String class to approximate the behavior of the string class from the STL. Your class should export the following methods:

  • constructors
    • String ( ): creates an empty String.
    • String ( const String& s ): creates a copy of String s.
    • String ( const String& s, const size_t pos, const size_t n = npos ): creates a copy of the portion of String s that begins at position pos and spans over n characters.
    • String ( const char* cs, const size_t n ): creates a String from a copy of first n characters of C-string cs.
    • String ( const char* cs ): creates a String from a copy of characters of C-string cs.
    • String ( const size_t n, const char& c ): creates a String from a copy of n repetitions of character c.
  • destructor
    • ~String ( ): destroys the String object by deallocating all storage capacity allocated by String. 3
  • assignment operator ( = )
    • String& operator= ( const String& s ): assigns a copy of String s to a String.
    • String& operator= ( const char* cs ): assigns a copy of C-string cs to a String.
    • String& operator= ( const char& c ): assigns a copy of character c ( as a C-string) to a String.
  • operator+=
    • String& operator+= ( const String& s ): appends a copy of String s to a String.
    • String& operator+= ( const char* cs ): appends a copy of C-string cs to a String.
    • String& operator+= ( const char& c ): appends a copy of character c to a String.
  • length ( ), size ( ) and capacity ( ) functions
    • size_t length ( ) const: returns the size of a String.
    • size_t size ( ) const: also returns the size of a String.
    • size_t capacity ( ) const: returns number of characters that a String can hold.
  • empty ( ) and clear ( ) functions
    • bool empty ( ) const: tests whether a String empty.
    • void clear ( ): erases all characters of a String.
  • subscription operator [ ] and index function
    • char& operator[ ] ( const size_t& pos ): returns a reference to character at position pos of a String.
    • const char& operator[ ] ( const size_t pos ) const: const version of the subscription operator.
    • char& at ( const size_t pos ): returns a reference to character at pos of a String and performs a range check. The at ( ) function of the string class in the STL throws an out_of_range exception and aborts the program in case of a range error, but the at ( ) function of your String class should only print an error message on stderr and not abort the program.
    • const char& at ( const size_t pos ) const: const version of the index function.
  • substr ( ) function
    • String substr ( const size_t& pos = 0, const size_t& n = 0 ): returns a substring of a String, which starts at position pos and spans over n characters (or until the end of the String, whichever comes first. If pos equals to String length, the function returns an empty String. The substr ( ) function of the string class in the STL throws an out_of_range exception and aborts the program in case of a range error, but the substr ( ) function of your String class should only print an error message on stderr and not abort the program.
  • c_str ( ) and data ( ) functions
    • const char* c_str ( ) const: generates a C-string from a String.
    • const char* data ( ) const: also does the same.
  • push_back ( ) function
    • void push_back ( const char& c ): appends a copy of the character c to a String, increasing its size by one. It calls the expandMem ( ) function to increase the String's buffer size.
  • stream insertion (<<) and extraction (>>) operators and getline ( ) function
    • friend ostream& operator<< ( ostream& os, const String& s ): inserts the characters of String s in the ostream os.
    • friend istream& operator >> ( istream& is, String& s ): extracts the characters from istream is and inserts them in String s. It calls getline ( ) function to read the characters from is and stops reading at newline character.
    • friend istream& getline ( istream& is, String& s, const char& del = '\n' ): reads characters from istream is and stops reading at delimiter character del.
  • operator+
    • friend String operator+ ( const String& s1, const String& s2 ): returns a newly constructed String with its value being the concatenation of characters in String s1 followed by those in String s2.
    • friend String operator+ ( const String& s, const char* cs ): returns a newly constructed String with its value being the concatenation of characters in String s followed by those in C-string cs.
    • friend String operator+ ( const char* cs, const String& s ): This differs only from the previous one as the order of its two arguments are swapped.
    • friend String operator+ ( const String& s, const char& c ): returns a newly constructed String with its value being the concatenation of characters in String s followed by character c.
    • friend String operator+ ( const char& c, const String& s ): This differs only from the previous one as the order of its two arguments are swapped.
  • relational operators
    • friend bool operator== ( const String& s1, const String& s2 ): tests whether String s1 and String s2 are the same.
    • friend bool operator!= ( const String& s1, const String& s2 ): tests whether String s1 and String s2 are different.
    • friend bool operator< ( const String& s1, const String& s2 ): tests whether String s1 comes before String s2 in ASCII order.
    • friend bool operator<= ( const String& s1, const String& s2 ): tests whether String s1 comes before String s2 in ASCII order or they are the same.
    • friend bool operator> ( const String& s1, const String& s2 ): tests whether String s1 comes after String s2 in ASCII order.
    • friend bool operator>= ( const String& s1, const String& s2 ): tests whether String s1 comes after String s2 in ASCII order or they are the same.
  • expandMem ( ) function
    • void expandMem ( const size_t& n ): this is the private member function of the String class. If any of the functions of the String class needs to increase the buffer memory for a String, they call this function to reallocate the memory for size n.

Programming Notes

  • Your code should work directly with your underlying representation and should make no calls to any of the methods in the STL string class.
  • The npos is defined as a static value, and since it's static, there is only one copy that is shared by all objects of the String class. The type of this value is size_t, and for most systems, it is defined as unsigned int. You can assign -1 to npos. Then, it is guaranteed that npos will get the largest possible integer value in the system.
  • If you implement one version of a function in the group of similar function, usually you can easily implement another function in the same group by calling the original function by different set of arguments. Sometimes, this really very easy. For example, when the result of the relational operator== is true, then the result of the relational operator!= is false.
Academic Honesty!
It is not our intention to break the school's academic policy. Posted solutions are meant to be used as a reference and should not be submitted as is. We are not held liable for any misuse of the solutions. Please see the frequently asked questions page for further questions and inquiries.
Kindly complete the form. Please provide a valid email address and we will get back to you within 24 hours. Payment is through PayPal, Buy me a Coffee or Cryptocurrency. We are a nonprofit organization however we need funds to keep this organization operating and to be able to complete our research and development projects.