home *** CD-ROM | disk | FTP | other *** search
/ Visual Basic Source Code / Visual Basic Source Code.iso / vbsource / vbdatabs / dstring.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1999-03-17  |  3.5 KB  |  135 lines

  1. // ------------------------------- //
  2. // -------- Start of File -------- //
  3. // ------------------------------- //
  4. // ----------------------------------------------------------- // 
  5. // C++ Source Code File Name: dstring.cpp 
  6. // Compiler Used: MSVC40, DJGPP 2.7.2.1, GCC 2.7.2.1, HP CPP 10.24
  7. // Produced By: Doug Gaer 
  8. // File Creation Date: 04/05/1996 
  9. // Date Last Modified: 03/17/1999
  10. // ----------------------------------------------------------- // 
  11. // ------------- Program Description and Details ------------- // 
  12. // ----------------------------------------------------------- // 
  13. /*
  14. THIS SOFTWARE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND.
  15. THE ENTIRE RISK OF THE QUALITY AND PERFORMANCE OF THIS SOFTWARE
  16. IS WITH YOU. SHOULD ANY ELEMENT OF THIS SOFTWARE PROVE DEFECTIVE,
  17. YOU WILL ASSUME THE COST OF ALL NECESSARY SERVICING, REPAIR, OR
  18. CORRECTION.
  19.  
  20. A simple dynamic string class.
  21. */
  22. // ----------------------------------------------------------- // 
  23. #include <string.h>
  24. #include <iostream.h>
  25. #include <iomanip.h>
  26.  
  27. class DString 
  28. public:
  29.   DString() { str = 0; }
  30.   DString(char *s) { Store(s); }
  31.   DString(int sz);
  32.   ~DString() { delete str; }    
  33.   DString(const DString &s) { Store(s.str); }
  34.  
  35.   DString &operator=(const DString &s) {
  36.     if(this != &s) {
  37.       str = 0;
  38.       Store(s.str);
  39.     }
  40.     return *this;
  41.   }
  42.  
  43. public:
  44.   void Store(char *s);
  45.   char *c_str() const { return str; } 
  46.  
  47. public:
  48.   friend int operator==(const DString &a, const DString &b) {
  49.     return strcmp(a.str, b.str) == 0;
  50.   }
  51.  
  52.   friend int operator<(const DString &a, const DString &b) {
  53.     return strcmp(a.str, b.str) < 0;
  54.   }
  55.  
  56.   friend int operator>(const DString &a, const DString &b) {
  57.     return strcmp(a.str, b.str) > 0;
  58.   }
  59.  
  60.   friend int operator!=(const DString &a, const DString &b) {
  61.     return strcmp(a.str, b.str) != 0;
  62.   }
  63.   
  64.   friend ostream &operator<<(ostream &os, const DString &s) {
  65.     return os.write(s.str, strlen(s.str));
  66.   }
  67.  
  68.   friend istream &operator>>(istream &os, DString &s) {
  69.     os >> setw(strlen(s.str)) >> s.str;
  70.     return os;
  71.   }
  72.            
  73. private:
  74.   char *str;
  75. };
  76.  
  77. DString::DString(int sz)
  78. {
  79.   str = new char[sz+1];
  80.   str[sz+1] = '\0';
  81. }
  82.  
  83. void DString::Store(char *s)
  84. {
  85.   size_t len = strlen(s);
  86.   str = new char[len+1];
  87.   str[len+1] = '\0';
  88.   strcpy(str, s);
  89. }
  90.  
  91. // Test program code starts here
  92. // *********************************************************** //
  93. int main()
  94. {
  95.   cout << "Constructing DString class objects..." << endl;
  96.   DString a("DOG");
  97.   DString b("CAT");
  98.   DString c("MOUSE");
  99.   DString d("BIRD");
  100.  
  101.   cout << "Testing copy constructor..." << endl;
  102.   DString e(a);  
  103.  
  104.   cout << "Testing assignment operator..." << endl;
  105.   a = c;
  106.  
  107.   cout << endl;
  108.   cout << "Testing overloaded << operator..."  << endl;
  109.   cout << a << ' ' << b  << ' ' << c  << ' ' << d << ' ' << e << endl;
  110.  
  111.   cout << endl;
  112.   cout << "Testing c_str() function..." << endl;
  113.   cout << a.c_str() << endl;
  114.   cout << b.c_str() << endl;
  115.   cout << c.c_str() << endl;
  116.   cout << d.c_str() << endl;
  117.   cout << e.c_str() << endl;
  118.  
  119.   cout << endl;
  120.   cout << "Testing overloaded >> operator..." << endl;
  121.   DString buf(255);
  122.   cout << "Enter a string of characters: ";
  123.   cin >> buf;
  124.  
  125.   cout << endl;
  126.   cout << "Data entered = " << buf << endl;
  127.   
  128.   return 0;
  129. }  
  130. // ----------------------------------------------------------- // 
  131. // ------------------------------- //
  132. // --------- End of File --------- //
  133. // ------------------------------- //
  134.