//////////////////////////////////////////////////// // str.cpp - implementation file for string class // //////////////////////////////////////////////////// #include #include #include "str.h" using namespace std; //----< sized constructor >------------------------------------ str::str(int n) { array = new char[n]; if(array==0) throw "\n memory allocation failure\n\n"; max = n; len = 0; } //------------------------------------------ str::str(const str &s) { array = new char[s.max]; if(array==0) throw "\n memory allocation failure\n\n"; int i; for(i=0; i-------------------------------- str::str(const char *s) { int length = strlen(s) + 1; array = new char[length]; int i; for(i=0; i------------------------------------------- str::~str() { delete [] array; max = len = 0; } // //----< assignment operator >---------------------------------- str& str::operator=(const str &s) { if(this == &s) return *this; if(max >= s.max) { int i; for(i=0; i<=s.size(); i++) array[i] = s.array[i]; len = s.len; return *this; } char *temp = new char[s.max + 10]; int i; for(i=0; i<=s.size(); i++) temp[i] = s.array[i]; len = s.len; max = s.max + 10; delete [] array; array = temp; return *this; } //---< index operator >---------------------------------------- char& str::operator[](int n) { return array[n]; } //----< index operator for const str >------------------------- char str::operator[](int n) const { return array[n]; } //----< return number of chars in str >------------------------ int str::size() const { return len; } // //----< append char to string >-------------------------------- void str::operator+=(char ch) { if(len < max-1) { array[len] = ch; array[len+1] = '\0'; len++; } else { max *= 2; // multiply by 2 char *temp = new char[max]; int i; for(i=0; i------------------------------ void str::operator+=(const str &s) { if(len < max-s.size()) { int i; for(i=0; i<=s.len; i++) array[len+i] = s[i]; len += s.size(); } else { max += max + s.size(); char *temp = new char[max]; int i; for(i=0; i----------------------------------- ostream& operator<<(ostream &out, const str &s) { int i; for(i=0; i-------------------------------------------- #ifdef TEST_STR void main() { cout << "\n Testing str class " << "\n ===================\n\n"; str s1 = "this is a string"; // calls str(const char *s) cout << " original str object: " << s1 << endl; str s2 = s1; // calls str(const str &s) cout << " copy of original: " << s2 << endl; char ch = s1[1]; cout << " second character is: " << ch << endl; s2[3] = 'x'; cout << " modified str object: " << s2 << endl; cout << "\n\n"; cout << " appending \'x\' to orignial str object: "; s1 += 'x'; cout << s1 << endl; cout << " size of str object is " << s1.size() << endl; cout << " appending \"yyy\" to modified last str object: "; s1 += "yyy"; cout << s1 << endl; cout << " size of str object is now " << s1.size() << endl; cout << " appending a long string to str object:\n "; s1 += " and the rain in Spain falls mainly on the plain"; cout << s1 << endl; cout << " size of str object is now " << s1.size() << "\n\n"; cout << " assigning the original's copy to this string: "; s1 = s2; cout << s2 << endl; cout << " size of str object is now " << s1.size() << "\n\n"; cout << " assigning \"C++ is great!!! \" several times to this string:\n"; int i; for(i=0; i<10; i++) { s1 += "C++ is great!!! "; s2 = s1; cout << " " << s2 << endl; cout << " size of str object is now " << s2.size() << "\n\n"; } } #endif