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

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