home *** CD-ROM | disk | FTP | other *** search
- // \EXAMPLES\EX15022.H
- //--------------------------------------------------------------
- // definition of the class String
- //--------------------------------------------------------------
-
- // files in this example:
- // %F,15,EX15021.H%EX15021.H definition of Astack & AstackItr
- // EX15022.H this file
- // %F,15,EX1502.CPP%EX1502.CPP main() - exercise templates
- // %F,15,EX15021.CPP%EX15021.CPP member functions of Astack
-
- //--------------------------------------------------------------
- #ifndef STRCLASS_H
- #define STRCLASS_H
-
- //--------------------------------------------------------------
- #include <iostream.h> // stream input and output
- #include <string.h> // string manipulation functions
-
- //--------------------------------------------------------------
- class String { // data members
- char* text; // pointer to the characters
- int len; // number of chars in String
- int size; // allocated space for chars
- public: // member functions
- String(int maxlen = 0); // default constructor
- String( const String& s); // copy constuctor
- String( char* s); // convert char* to String
- ~String(); // destructor
- String& operator=( const String& s);// assignment operator
- friend ostream& operator<<( ostream& os, const String& s);
- };
-
- //--------------------------------------------------------------
- // default constructor ( optional maxlen defaults to 0 )
- //--------------------------------------------------------------
-
- String::String(int maxlen) // allow room for maxlen chars
- { len = 0; // no characters in string
- size = maxlen+1; // sized to hold terminator
- text = new char [size]; // allocate from free store
- strcpy( text, ""); // initialize to null string
- }
-
- //--------------------------------------------------------------
- // copy constructor
- //--------------------------------------------------------------
-
- String::String( const String& s) // copy an existing String obj.
- { len = s.len; // same length as existing obj.
- size = len+1; // sized to hold terminator
- text = new char [size]; // allocate from free store
- strcpy( text, s.text); // copy chars in existing obj.
- }
-
- //--------------------------------------------------------------
- // constructor to convert from char*
- //--------------------------------------------------------------
-
- String::String( char* s) // convert from existing char*
- { len = strlen(s); // set same length of string
- size = len + 1; // sized to hold terminator
- text = new char [size]; // allocate from free store
- strcpy( text, s); // copy chars from char*
- }
-
- String::~String()
-
- //--------------------------------------------------------------
- // destructor
- //--------------------------------------------------------------
- { delete[] text; // deallocate free store
- }
-
- //---------------------------------------------------------------
- // assignment operator - sets one String equal to another
- //---------------------------------------------------------------
-
- String& String::operator=( const String& s)
- { len = s.len; // set length of String
- if ( len < size ) // test if chars fit
- strcpy ( text, s.text); // yes - copy
- else // if chars don't fit
- { size = len + 1; // +1 for terminator
- char* newtext = new char[size];// allocate free store
- strcpy( newtext, s.text); // copy chars
- delete[] text; // destroy the old chars
- text = newtext; // update pointer to chars
- }
- return *this; // return updated String
- }
-
- //---------------------------------------------------------------
- // output operator
- //---------------------------------------------------------------
-
- ostream& operator<<( ostream& os, const String& s)
- { os << s.text;
- return os;
- }
-
- //---------------------------------------------------------------
- #endif
-