home *** CD-ROM | disk | FTP | other *** search
- // \EXAMPLES\EX08051.CPP
- // member functions of the String class
- //--------------------------------------------------------------
-
- // files in this example:
- // %F,15,EX08051.H%EX08051.H definition of class String
- // %F,15,EX08052.H%EX08052.H definition of classes PhoneBk,
- // PhoneNum and List
- // EX08051.CPP this file
- // %F,15,EX08052.CPP%EX08052.CPP member functions of PhoneBk,
- // PhoneNum and List
- // %F,15,EX0805.CPP%EX0805.CPP main to exercise PhoneBk class
- //--------------------------------------------------------------
-
- #include <string.h> // standard string manipultion functions
- #include <iostream.h> // stream output
-
- #include "EX08051.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 - concatonate 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) const
- { return os << text; // output and return stream
- }
-
- //--------------------------------------------------------------
- // compare function - compare chars to specified char*
- //--------------------------------------------------------------
-
- int String::compare(const char* t) const
- { return strcmp(text, t); // compare and return -1 0 1
- }
-
- //---------------------------------------------------------------
- // 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
- }
-