C++ Programming Homework 3 Solution

●     (80 points) Write a String class which will be a wrapper class to the C style strings. The strings will be of varying lengths and must grow and shrink as necessary. Your String class must implement all the appropriate methods including constructors, assignment, equality operators, the index operator, reverse, indexOf (find), print, and read. I would prefer you not use any of the C  str functions (e.g. strcmp, or strcpy), however you may write them yourself, as static methods, then use them.

●     Class String declaration:
  class String
  {
  public:
    /// Both constructors should construct
    /// this String from the parameter s
    String( const char * s = "");
    String( const String & s );
    String operator = ( const String & s );
    char & operator [] ( int index );
    int size();
    String reverse(); // does not modify this String
    int indexOf( char c );
    int indexOf( String pattern );
    bool operator == ( String s );
    bool operator != ( String s );
    bool operator ( String s );
    bool operator < ( String s )
    bool operator <= ( String s );
    bool operator = ( String s );
    /// concatenates this and s to return result
    String operator + ( String s );
    /// concatenates s onto end of this
    String operator += ( String s );
    void print( ostream & out );
    void read( istream & in );
    ~String();
  private:
    bool inBounds( int i )
    {
      return i = 0 && i < len;
    }
    char * buf;
    int len;
  };
  ostream & operator << ( ostream & out, String str );
  istream & operator ( istream & in, String & str );



●     (20 points) Write a main function which tests each function defined in your class String.
Powered by