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

  1. // \EXAMPLES\EX07031.CPP
  2. // a sample class using dynamic memory - String
  3.  
  4. // files in this example:
  5. // %F,15,EX0703.H%EX0703.H      definition of the class Sting
  6. // EX07031.CPP   this file -- member functions
  7. // %F,15,EX07032.CPP%EX07032.CPP   main() to exercise this class
  8.  
  9. #include <string.h>     // standard string manipulation functions
  10. #include <iostream.h>   // stream output
  11.  
  12. #include "EX0703.H"     // definition of class
  13.  
  14. //--------------------------------------------------------------
  15. // default constructor  ( optional maxlen defaults to 0 )
  16. //--------------------------------------------------------------
  17.  
  18. String::String(int maxlen)        // allow room for maxlen chars
  19. { len = 0;                        // no characters in string
  20.   size = maxlen+1;                // sized to hold terminator
  21.   text = new char [size];         // allocate from free store
  22.   strcpy( text, "");              // initialize to null string
  23. }
  24.  
  25. //--------------------------------------------------------------
  26. // copy constructor
  27. //--------------------------------------------------------------
  28.  
  29. String::String( const String& s) // copy an existing String obj.
  30. { len = s.len;                   // same length as existing obj.
  31.   size = len+1;                  // sized to hold terminator
  32.   text = new char [size];        // allocate from free store
  33.   strcpy( text, s.text);         // copy chars in existing obj.
  34. }
  35.  
  36. //--------------------------------------------------------------
  37. // constructor to convert from char*
  38. //--------------------------------------------------------------
  39.  
  40. String::String( char* s)         // convert from existing char*
  41. { len = strlen(s);               // set same length of string
  42.   size = len + 1;                // sized to hold terminator
  43.   text = new char [size];        // allocate from free store
  44.   strcpy( text, s);              // copy chars from char*
  45. }
  46.  
  47. String::~String()
  48.  
  49. //--------------------------------------------------------------
  50. // destructor
  51. //--------------------------------------------------------------
  52. {  delete[] text;               // deallocate free store
  53. }
  54.  
  55. //--------------------------------------------------------------
  56. // append function - concatenate char* to end of String
  57. //--------------------------------------------------------------
  58.  
  59. void String::append (const char* t)
  60. { int addlen = strlen(t);       // get number of chars to add
  61.   if ( len + addlen < size )    // test if additional chars fit
  62.   { strcat ( text, t);             // yes - concatenate
  63.     len = len + addlen;            //       update length
  64.   }
  65.   else                          // if additional chars don't fit
  66.   { len += addlen;                 // calculate required length
  67.     size = len + 1;                // sized to hold terminator
  68.     char* newtext = new char[size];// allocate from free store
  69.     strcpy( newtext, text);        // copy existing chars
  70.     strcat( newtext, t);           // concatenate
  71.     delete[] text;                 // destroy the old chars
  72.     text = newtext;                // update pointer to chars
  73.   }
  74. }
  75.  
  76. //--------------------------------------------------------------
  77. // print function - output chars to specified output stream
  78. //--------------------------------------------------------------
  79.  
  80. ostream& String::print(ostream& os)
  81. { return os << text;                 // output and return stream
  82. }
  83.