home *** CD-ROM | disk | FTP | other *** search
/ Microsoftware Monthly 19…2 Programming Power Tools / MASO9512.ISO / cpptutor / cpptutor.arj / EXAMPLES / EX08051.CPP < prev    next >
Encoding:
C/C++ Source or Header  |  1993-09-30  |  4.8 KB  |  115 lines

  1. // \EXAMPLES\EX08051.CPP
  2. //  member functions of the String class
  3. //--------------------------------------------------------------
  4.  
  5. //  files in this example:
  6. // %F,15,EX08051.H%EX08051.H       definition of class String
  7. // %F,15,EX08052.H%EX08052.H       definition of classes PhoneBk,
  8. //              PhoneNum and List
  9. // EX08051.CPP     this file
  10. // %F,15,EX08052.CPP%EX08052.CPP     member functions of PhoneBk,
  11. //              PhoneNum and List
  12. // %F,15,EX0805.CPP%EX0805.CPP   main to exercise PhoneBk class
  13. //--------------------------------------------------------------
  14.  
  15. #include <string.h>     // standard string manipultion functions
  16. #include <iostream.h>   // stream output
  17.  
  18. #include "EX08051.H"     // definition of class
  19.  
  20. //--------------------------------------------------------------
  21. // default constructor  ( optional maxlen defaults to 0 )
  22. //--------------------------------------------------------------
  23.  
  24. String::String(int maxlen)        // allow room for maxlen chars
  25. { len = 0;                        // no characters in string
  26.   size = maxlen+1;                // sized to hold terminator
  27.   text = new char [size];         // allocate from free store
  28.   strcpy( text, "");              // initialize to null string
  29. }
  30.  
  31. //--------------------------------------------------------------
  32. // copy constructor
  33. //--------------------------------------------------------------
  34.  
  35. String::String( const String& s) // copy an existing String obj.
  36. { len = s.len;                   // same length as existing obj.
  37.   size = len+1;                  // sized to hold terminator
  38.   text = new char [size];        // allocate from free store
  39.   strcpy( text, s.text);         // copy chars in existing obj.
  40. }
  41.  
  42. //--------------------------------------------------------------
  43. // constructor to convert from char*
  44. //--------------------------------------------------------------
  45.  
  46. String::String( char* s)         // convert from existing char*
  47. { len = strlen(s);               // set same length of string
  48.   size = len + 1;                // sized to hold terminator
  49.   text = new char [size];        // allocate from free store
  50.   strcpy( text, s);              // copy chars from char*
  51. }
  52.  
  53. String::~String()
  54.  
  55. //--------------------------------------------------------------
  56. // destructor
  57. //--------------------------------------------------------------
  58. {  delete[] text;               // deallocate free store
  59. }
  60.  
  61. //--------------------------------------------------------------
  62. // append function - concatonate char* to end of String
  63. //--------------------------------------------------------------
  64.  
  65. void String::append (const char* t)
  66. { int addlen = strlen(t);       // get number of chars to add
  67.   if ( len + addlen < size )    // test if additional chars fit
  68.   { strcat ( text, t);             // yes - concatenate
  69.     len = len + addlen;            //       update length
  70.   }
  71.   else                          // if additional chars don't fit
  72.   { len += addlen;                 // calculate required length
  73.     size = len + 1;                // sized to hold terminator
  74.     char* newtext = new char[size];// allocate from free store
  75.     strcpy( newtext, text);        // copy existing chars
  76.     strcat( newtext, t);           // concatenate
  77.     delete[] text;                 // destroy the old chars
  78.     text = newtext;                // update pointer to chars
  79.   }
  80. }
  81.  
  82. //--------------------------------------------------------------
  83. // print function - output chars to specified output stream
  84. //--------------------------------------------------------------
  85.  
  86. ostream& String::print(ostream& os) const
  87. { return os << text;                 // output and return stream
  88. }
  89.  
  90. //--------------------------------------------------------------
  91. // compare function - compare chars to specified char*
  92. //--------------------------------------------------------------
  93.  
  94. int String::compare(const char* t) const
  95. { return strcmp(text, t);            // compare and return -1 0 1
  96. }
  97.  
  98. //---------------------------------------------------------------
  99. // assignment operator  - sets one String equal to another
  100. //---------------------------------------------------------------
  101.  
  102. String& String::operator=( const String& s)
  103. { len = s.len;                     // set length of String
  104.   if ( len < size )                // test if chars fit
  105.     strcpy ( text, s.text);        // yes - copy
  106.   else                             // if chars don't fit
  107.   { size = len + 1;                // +1 for terminator
  108.     char* newtext = new char[size];// allocate free store
  109.     strcpy( newtext, s.text);      // copy chars
  110.     delete[] text;                 // destroy the old chars
  111.     text = newtext;             // update pointer to chars
  112.   }
  113.   return *this;                    // return updated String
  114. }
  115.