131: String temp(totalLen);
132: int i, j;
133: for (i = 0; i<itsLen; i++)
134: temp[i] = itsString[i];
135: for (j = 0, i = itsLen; j<rhs.GetLen(); j++, i++)
136: temp[i] = rhs[j];
137: temp[totalLen]=’\0’;
138: return temp;
139: }
140:
141: // creates a new string by adding
142: // one string to another
143: String operator+(const String& lhs, const String& rhs)
144: {
145: int totalLen = lhs.GetLen() + rhs.GetLen();
146: String temp(totalLen);
147: int i, j;
148: for (i = 0; i<lhs.GetLen(); i++)
149: temp[i] = lhs[i];
150: for (j = 0, i = lhs.GetLen(); j<rhs.GetLen(); j++, i++)
151: temp[i] = rhs[j];
152: temp[totalLen]=’\0’;
153: return temp;
154: }
155:
156: int main()
157: {
158: String s1(“String One “);
159: String s2(“String Two “);
160: char *c1 = { “C-String One “ } ;
161: String s3;
162: String s4;
163: String s5;
164:
165: cout << “s1: “ << s1.GetString() << endl;
166: cout << “s2: “ << s2.GetString() << endl;
167: cout << “c1: “ << c1 << endl;
168: s3 = s1 + s2;
169: cout << “s3: “ << s3.GetString() << endl;
170: s4 = s1 + c1;
171: cout << “s4: “ << s4.GetString() << endl;
172: s5 = c1 + s2;
173: cout << “s5: “ << s5.GetString() << endl;
174: return 0;
175: }
584 Day 16
LISTING16.8 continued