home *** CD-ROM | disk | FTP | other *** search
- // \EXAMPLES\EX07031.CPP
- // a sample class using dynamic memory - String
-
- // files in this example:
- // %F,15,EX0703.H%EX0703.H definition of the class Sting
- // EX07031.CPP this file -- member functions
- // %F,15,EX07032.CPP%EX07032.CPP main() to exercise this class
-
- #include <string.h> // standard string manipulation functions
- #include <iostream.h> // stream output
-
- #include "EX0703.H" // definition of class
-
- //--------------------------------------------------------------
- // 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
- }
-
- //--------------------------------------------------------------
- // append function - concatenate char* to end of String
- //--------------------------------------------------------------
-
- void String::append (const char* t)
- { int addlen = strlen(t); // get number of chars to add
- if ( len + addlen < size ) // test if additional chars fit
- { strcat ( text, t); // yes - concatenate
- len = len + addlen; // update length
- }
- else // if additional chars don't fit
- { len += addlen; // calculate required length
- size = len + 1; // sized to hold terminator
- char* newtext = new char[size];// allocate from free store
- strcpy( newtext, text); // copy existing chars
- strcat( newtext, t); // concatenate
- delete[] text; // destroy the old chars
- text = newtext; // update pointer to chars
- }
- }
-
- //--------------------------------------------------------------
- // print function - output chars to specified output stream
- //--------------------------------------------------------------
-
- ostream& String::print(ostream& os)
- { return os << text; // output and return stream
- }
-