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

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