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

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