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

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