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

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