< previous page page_397 next page >

Page 397
LISTING 22.3 continued
d5ef64f4d3250b96ba5c07ca5bbc2f56.gif
106:
107:   //non constant offset operator, returns
108:   // reference to character so it can be changed
109:   char & String::operator[](int offset)
110:   {
111:      if (offset > itsLen)
112:         return itsString[itsLen-1];
113:      else
114:         return itsString[offset];
115:   }
116:
117:   // constant offset operator for use
118:   // on const objects (see copy constructor!)
119:   char String::operator[](int offset) const
120:   {
121:      if (offset > itsLen)
122:         return itsString[itsLen-1];
123:      else
124:         return itsString[offset];
125:   }
126:
127:   // creates a new string by adding current
128:   // string to rhs
129:   String String::operator+(const String& rhs)
130:   {
131:      int totalLen = itsLen + rhs.GetLen();
132:      String temp(totalLen);
133:      int i,j;
134:      for (i = 0; i<itsLen; i++)
135:         temp[i] = itsString[i];
136:      for (j = 0; j<rhs.GetLen(); j++, i++)
137:         temp[i] = rhs[j];
138:      temp[totalLen]=\0;
139:      return temp;
140:   }
141:
142:   // changes current string, returns nothing
143:   void String::operator+=(const String& rhs)
144:   {
145:      int rhsLen = rhs.GetLen();
146:      int totalLen = itsLen + rhsLen;
147:      String temp(totalLen);
148:      int i,j;
149:      for ( i = 0; i<itsLen; i++)
150:         temp[i] = itsString[i];
151:      for ( j = 0; j<rhs.GetLen(); j++, i++)
152:         temp[i] = rhs[i-itsLen];
153:      temp[totalLen]=\0;
154:      *this = temp;
d5ef64f4d3250b96ba5c07ca5bbc2f56.gif
continues

 
< previous page page_397 next page >

If you like this book, buy it!