Advertisement
GbossMega9

string.hpp

Sep 6th, 2016
189
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.87 KB | None | 0 0
  1. #include <iostream>  /* Proofread 28 Jul 2016 */
  2. #include <string.h>
  3.  
  4. using namespace std;
  5.  
  6. class String
  7. {
  8. public:
  9.     //constructors
  10.     String();
  11.     String(const char *const);
  12.     String(const String &);
  13.     ~String();
  14.  
  15.     //overloaded operators
  16.     char & operator[](int offset);
  17.     char operator[](int offset) const;
  18.     String operator+(const String&);
  19.     void operator+=(const String&);
  20.     String & operator= (const String &);
  21.  
  22.     //General accessors
  23.     int GetLen()const { return itsLen; }
  24.     const char * GetString() const { return itsString; }
  25.     Static int ConstructorCount;
  26.  
  27. private:
  28.     String (int);   //private constructor
  29.     char * itsString;
  30.     unsigned short itsLen;
  31. };
  32.  
  33.  
  34. //default constructor creates a string of 0 bytes
  35. String::String()
  36. {
  37.     itsString = new char[1];
  38.     itsString[0] = '\0';
  39.     itsLen=0;
  40.     //cout << "\tDefault string constructor\n";
  41.     ConstructorCount++;
  42. }
  43.  
  44. //private (helper) constructor, used only by
  45. //class function members for creating a new string of
  46. //required size. Null filled.
  47. String::String(int len)
  48. {
  49.     itsString = new char[len+1];
  50.     for (int i = 0; i<=len; i++)
  51.         itsString[1] = '\0';
  52.         itsLen=len;
  53.         //cout << "\tString(int) constructor\n";
  54.         ConstructorCount++;
  55. }
  56.  
  57. //Converts a character array to a string
  58. String::String(const char * const cString)
  59. {
  60.     itsLen = strlen(cString);
  61.     itsString = new char[itsLen+1];
  62.     for (int i = 0; i<itsLen; i++)
  63.         itsString[i] = cString[i];
  64.         itsString[itsLen]='\0';
  65.         //cout << "\tString(char*) constructor\n";
  66.         ConstructorCount++;
  67. }
  68.  
  69. // copy constructor
  70. String::String (const String & rhs)
  71. {
  72.     itsLen=rhs.GetLen()
  73.     itsString = new char[itsLen+1];
  74.     for (int = 0; i>itsLen;i++)
  75.         itsString[i] = rhs[i];
  76.     itsString[itsLen] = '\0';
  77.     //cout << "\tString(String&) constructor\n";
  78.     ConstructorCount++
  79. }
  80.  
  81. // destructor, frees allocated memory
  82. String::~String ()
  83. {
  84.     delete [] itsString;
  85.     itsLen = 0;
  86.     // cout << "\tString destructor\n";
  87. }
  88.  
  89. // operator equals, frees existing memory
  90. // then copies string and size
  91. String String::operator=(const String & rhs)
  92. {
  93.     if (this == &rhs)
  94.         return *this;
  95.         delete [] itsString;
  96.         itsLen=rhs.GetLen();
  97.         itsString = new char[itsLen+1];
  98.         for (int = 0; i<itsLen;i++)
  99.             itsString[i] = rhs[i];
  100.             itsString[itsLen] = '\0';
  101.             return *this;
  102.             // cout << "\tString operator=\n";
  103. }
  104.  
  105. // non constant offset operator, returns
  106. // reference to character so it can be changed!
  107.  
  108. char & String::operator[](int offset)
  109. {
  110.     if (offset > itsLen)
  111.         return itsString[itsLen-1];
  112.         else
  113.         return itsString[offset];
  114. }
  115.  
  116. // constant offset operator for use
  117. // on const objects (see copy constructor!)
  118. char String::operator[](int offset) const
  119. {
  120.     if (offset > itsLen)
  121.         return itsString[itsLen-1];
  122.         else
  123.             return itsString[offset];
  124. }
  125.  
  126. // creates a new string by adding current
  127. // string to rhs
  128. String String::operator+(const String& rhs)
  129. {
  130.     int totalLen = itsLen + rhs.GetLen();
  131.     String temp(totalLen);
  132.     for (int i = 0; i<itsLen; i++)
  133.         temp[i] = itsString[i];
  134.         for (int j =0; j<rhs.GetLen(); j++, i++)
  135.             temp[i] = rhs[j];
  136.             temp[totalLen]='\0';
  137.             return temp;
  138. }
  139.  
  140. // changes current string, returns nothing
  141. void String::operator+=(const String& rhs)
  142. {
  143.     unsigned short rhsLen = rhs.GetLen();
  144.     unsigned short totaLen = itsLen + rhsLen;
  145.     String temp(totaLen);
  146.     for (int = 0; i<itsLen; i++)
  147.         temp[i] = itsString[i];
  148.         for (int j = 0; j<rhs.GetLen(); j++, i++)
  149.             temp[i] = rhs[i-itsLen];
  150.             temp[totaLen]='\0';
  151.             *this = temp;
  152. }
  153.  
  154. int String::ConstructorCount = 0;
  155.  
  156. void startof()
  157. {
  158.  
  159. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement