// P8_08M.cpp // member function definitions for p8_08.cpp // operator + taken out for assignment // class String #include using std::cout; using std::ostream; #include // strcpy and strcat prototypes #include "p8_08.h" // String class definition // Conversion constructor: Convert a char * to String String::String( const char * const zPtr ) { length = strlen( zPtr ); // compute length sPtr = new char[ length + 1 ]; // allocate storage assert( sPtr != 0 ); // terminate if memory not allocated strcpy( sPtr, zPtr ); // copy literal to object } // end String conversion constructor // Copy constructor String::String( const String © ) { length = copy.length; // copy length sPtr = new char[ length + 1 ]; // allocate storage assert( sPtr != 0 ); // ensure memory allocated strcpy( sPtr, copy.sPtr ); // copy string } // end String copy constructor // Destructor String::~String() { delete [] sPtr; } // reclaim string // Overloaded = operator; avoids self assignment const String &String::operator=( const String &right ) { if ( &right != this ) { // avoid self assignment delete [] sPtr; // prevents memory leak length = right.length; // new String length sPtr = new char[ length + 1 ];// allocate memory assert( sPtr != 0 ); // ensure memory allocated strcpy( sPtr, right.sPtr ); // copy string } else cout << "Attempted assignment of a String to itself\n"; return *this; // enables concatenated assignments } // end function operator= // Concatenate right operand and this object and // store in temp object. // OVERLOADED + OPERATOR GOES HERE // end function operator+ // overloaded output operator ostream & operator<<( ostream &output, const String &s ) { output << s.sPtr; return output; // enables concatenation } // end function operator<< /************************************************************************* * (C) Copyright 1992-2003 by Deitel & Associates, Inc. and Prentice * * Hall. All Rights Reserved. * * * * DISCLAIMER: The authors and publisher of this book have used their * * best efforts in preparing the book. These efforts include the * * development, research, and testing of the theories and programs * * to determine their effectiveness. The authors and publisher make * * no warranty of any kind, expressed or implied, with regard to these * * programs or to the documentation contained in these books. The authors * * and publisher shall not be liable in any event for incidental or * * consequential damages in connection with, or arising out of, the * * furnishing, performance, or use of these programs. * *************************************************************************/