home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / string.cpp < prev    next >
Text File  |  1994-05-15  |  5KB  |  145 lines

  1. /*----------------------------------------------------------------------------*/
  2. /*  (c) 1994 Larry Morley                                                     */
  3. /*----------------------------------------------------------------------------*/
  4. /*  Module     : strings.cpp                            Date: 04 April 1994   */
  5. /*                                                                            */
  6. /*  Description: A string class that automatically sizes itself based on the  */
  7. /*               data assigned to it or provided when an object of the class  */
  8. /*               type is created.  Automatically adjusts string length for    */
  9. /*               concatenation.  Looks to other functions just like an        */
  10. /*               ordinary char * via a conversion function.                   */
  11. /*                                                                            */
  12. /*----------------------------------------------------------------------------*/
  13. /*                                                                            */
  14. /*  To contact the author, send an e-mail message to 73670,563 or a regular   */
  15. /*  letter / note to:                                                         */
  16. /*                                                                            */
  17. /*  Larry Morley / Enlightened Computer Solutions                             */
  18. /*  6909 Custer #2503                                                         */
  19. /*  Plano, TX 75023                                                           */
  20. /*                                                                            */
  21. /*  Please let me know if you're using this code, and your suggestions,       */
  22. /*  questions, etc.  Thanks.                                                  */
  23. /*                                                                            */
  24. /*----------------------------------------------------------------------------*/
  25.  
  26. #include <stdio.h>
  27. #include <string.h>
  28.  
  29. /*----------------------------------------------------------------------------*/
  30.  
  31. int main(void);
  32.  
  33. /*----------------------------------------------------------------------------*/
  34. // Note that there's no need to override the assignment operator;
  35. // since there's a constructor that takes a char * as an argument,
  36. // the compiler automatically supplies the conversion from char *
  37. // to String, by creating an interim string from the char *, and a
  38. // new string through the constructor as a result of the assignment.
  39.  
  40. class String
  41. {
  42.    private:
  43.  
  44.       // The actual string text
  45.       char *szText;
  46.  
  47.    public:
  48.  
  49.       // Constructor for string literals,
  50.       // char * strings, etc.
  51.       String(char *initText)
  52.       {
  53.          // szText <- address if enough memory
  54.          // is available or NULL if out of memory.
  55.          szText = new char[strlen(initText)+1];
  56.          // Make sure we got the memory
  57.          if (szText)
  58.             strcpy(szText,initText);
  59.       };
  60.  
  61.       //---------------------------------------------------
  62.  
  63.       // Constructor for a string of
  64.       // a certain length
  65.       String(int size)
  66.       {
  67.          // szText <- address if enough memory
  68.          // is available or NULL if out of memory.
  69.          szText = new char [size];
  70.          // Make sure we got the memory
  71.          // Terminate the string at the
  72.          // first element (empty string)
  73.          if (szText)
  74.             szText[0] = (char)0;
  75.       };
  76.  
  77.       //---------------------------------------------------
  78.  
  79.       ~String()
  80.       {
  81.          // If there's a string, delete it
  82.          if (szText)
  83.             delete[] szText;
  84.       };
  85.  
  86.       //---------------------------------------------------
  87.  
  88.       // Conversion function - makes class objects
  89.       // function and look like a normal char *s
  90.       operator char *()
  91.       {
  92.          return szText;
  93.       };
  94.  
  95.       //---------------------------------------------------
  96.  
  97.       // Overload addition operator
  98.       // to mean concatenation
  99.       String operator+(String szStringB)
  100.       {
  101.          // Create a temporary string
  102.          char *p = new char[strlen(szText)+strlen(szStringB)+1];
  103.          // Make sure we got the memory
  104.          if (p)
  105.          {
  106.             strcpy(p,szText);
  107.             // Delete the old string
  108.             delete[] szText;
  109.             // Copy the original string into the
  110.             // temporary string
  111.             szText = p;
  112.             // Do the concatenation
  113.             strcat(szText,szStringB);
  114.             // Return the new value of szText
  115.             return szText;
  116.          }
  117.          // Out of memory - return NULL
  118.          return (String)0;
  119.       };
  120.  
  121. };
  122.  
  123. /*----------------------------------------------------------------------------*/
  124. // Try the String class out...
  125.  
  126. main()
  127. {
  128.    String StringA = "This is a string";
  129.    char * StringB = ", alright.";
  130.  
  131.    printf("Before concatenation:\n");
  132.    printf("            StringA = %s\n",StringA);
  133.    printf("            StringB = %s\n",StringB);
  134.  
  135.    StringA = StringA + StringB;
  136.  
  137.    printf("After concatenation :\n");
  138.    printf("            StringA = %s\n",StringA);
  139.    printf("            StringB = %s\n",StringB);
  140.  
  141.    return 0;
  142. };
  143.  
  144. /*----------------------------------------------------------------------------*/
  145.