home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / cset21v1.zip / IBMCPP / TUTORIAL / ICLCC / TSTRING.H
C/C++ Source or Header  |  1993-05-07  |  5KB  |  121 lines

  1. /******************************************************************************/
  2. /*                                                                            */
  3. /* COPYRIGHT:                                                                 */
  4. /* ----------                                                                 */
  5. /* Copyright (C) International Business Machines Corp., 1991,1992.            */
  6. /*                                                                            */
  7. /* DISCLAIMER OF WARRANTIES:                                                  */
  8. /* -------------------------                                                  */
  9. /* The following [enclosed] code is sample code created by IBM                */
  10. /* Corporation.  This sample code is not part of any standard IBM product     */
  11. /* and is provided to you solely for the purpose of assisting you in the      */
  12. /* development of your applications.  The code is provided "AS IS",           */
  13. /* without warranty of any kind.  IBM shall not be liable for any damages     */
  14. /* arising out of your use of the sample code, even if they have been         */
  15. /* advised of the possibility of such damages.                                */
  16. /*                                                                            */
  17. /******************************************************************************/
  18. #ifndef  STRINGX_H
  19. #define  STRINGX_H
  20. //
  21. #include <string.h>
  22. #include <iostream.h>
  23. //
  24. //
  25. class String {
  26. // ==========================================================
  27. //           Data members
  28. // ==========================================================
  29.     char *text;
  30.     int length;
  31. public:
  32. // ==========================================================
  33. //    member functions
  34. // ==========================================================
  35.    char * getText() const {
  36.       return  text;
  37.  
  38.     }
  39.     int getLength() const {
  40.       return  length;
  41.     }
  42. // ==========================================================
  43. //    Constructors, destructors, member functions
  44. // ==========================================================
  45.     String(const char *iText) {
  46.       length = strlen(iText);
  47.       text = new char[length+1];
  48.       strcpy(text, iText);
  49.     }
  50.     String(const String& iString) {
  51.       length = iString.length;
  52.         text = new char[length+1];
  53.         strcpy(text, iString.text);
  54.     }
  55.     String(int iLength) {
  56.       length = iLength;
  57.       text = new char[length+1];
  58.       for (int i = 0; i < length; i++) text[i] = ' ';
  59.       text[length] = '\0';
  60.     }
  61. // ==========================================================
  62. // ===              Constructor
  63. // ==========================================================
  64.     String() {
  65.       length = 0;
  66.       text = new char[length+1];
  67.       text[0] = '\0';
  68.     }
  69. // ==========================================================
  70. // ===              Destructor
  71. // ==========================================================
  72.     String::~String() {
  73.       delete text;
  74.     }
  75. // ==========================================================
  76. // ===           (in)   equality operator overloading
  77. // ==========================================================
  78.     int operator == (const String& anOther)const {
  79.       return (strcmp(text, anOther.text) == 0);
  80.     }
  81.     int operator != (const String& anOther)const {
  82.       return !(*this == anOther);
  83.     }
  84. // =========================================================
  85. // ===              <, >  operator overloading
  86. // =========================================================
  87.     int operator < (const String& anOther)const {
  88.       return (strcmp(text, anOther.text) < 0);
  89.     }
  90.     int operator > (const String& anOther)const {
  91.       return (strcmp(text, anOther.text) > 0);
  92.     }
  93.     int operator <= (const String& anOther)const {
  94.       return !(*this > anOther);
  95.     }
  96.     int operator >= (const String& anOther)const {
  97.       return !(*this < anOther);
  98.     }
  99. // ===========================================================
  100. // ===              Assignment operator overloading
  101. // ===========================================================
  102.     String& operator = (const String& iString) {
  103.       if (& iString == this) return  *this;
  104.  
  105.       delete text;
  106.       length = iString.length;
  107.       text = new char[length+1];
  108.       strcpy(text, iString.text);
  109.       return  *this;
  110.     }
  111. } ;
  112.  
  113. inline ostream& operator<< (ostream& sout, String const& s)
  114. { return sout << s.getText ();
  115. }
  116.  
  117. inline int compare (String const &  s1, String const & s2){
  118.    return strcmp(s1.getText(),s2.getText());
  119. }
  120. #endif
  121.