/////////////////////////////////////////////////////////////// // str.cpp - implementation file for string class // // ver 1.4 // // // // Language: Visual C++, ver 6.0 // // Platform: Micron Dual Pentium Pro 200, Win NT 4.0 // // Application: ECS500 Example // // Author: Jim Fawcett, ECS500 Instructor // // Syracuse University, CST 2-187 // // fawcett@ecs.syr.edu, (315) 443-3948 // /////////////////////////////////////////////////////////////// #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[max = s.max]; if(array==0) throw "\n memory allocation failure\n\n"; len = s.len; int i; for(i=0; i<=len; i++) array[i] = s.array[i]; }; //----< promotion constructor >-------------------------------- str::str(const char *s) { len = strlen(s); max = len+1; array = new char[len+1]; if(array == NULL) throw "\n memory allocation failure\n\n"; int i; for(i=0; i<=len; i++) array[i] = s[i]; } // //----< destructor >------------------------------------------- str::~str() { delete [] array; max = len = 0; } //----< assignment operator >---------------------------------- str& str::operator=(const str &s) { if(this == &s) return *this; if(max >= s.len+1) { len = s.len; int i; for(i=0; i<=len; i++) array[i] = s.array[i]; return *this; } delete [] array; array = new char[max = s.max + 10]; if(array == NULL) throw "\n memory allocation failure\n\n"; int i; len = s.len; for(i=0; i<=len; i++) array[i] = s.array[i]; return *this; } //---< index operator >---------------------------------------- char& str::operator[](int n) { static char safe = '?'; if(n<0 || len<=n) { cout << "\n index out of bounds in operator[]\n\n"; return safe; } return array[n]; } //----< index operator for const str >------------------------- char str::operator[](int n) const { char safe = '?'; if(n<0 || len<=n) { cout << "\n index out of bounds in operator[]\n\n"; return safe; } return array[n]; } // //----< 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]; if(temp == NULL) throw "\n memory allocation failure\n\n"; 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---------------------------------- istream& operator>>(istream &in, str &s) { char ch; s.flush(); in >> ch; while((ch != '\n') && in.good()) { s += ch; in.get(ch); } return in; } // //----< test stub >-------------------------------------------- #ifdef TEST_STR #include void main() { cout << "\n Testing str class " << "\n ===================\n"; cout << "\n testing promotion constructor " << "\n -------------------------------\n"; str s1 = "this is a string"; // calls str(const char *s) cout << " new str object: " << s1 << endl; cout << "\n testing copy constructor " << "\n --------------------------\n"; str s2 = s1; // calls str(const str &s) cout << " copy of original: " << s2 << endl; cout << "\n testing index operator " << "\n ------------------------\n"; char ch = s1[1]; cout << " second character is: " << ch << endl; s2[3] = 'x'; cout << " modifying fourth character: " << s2 << endl; cout << "\n testing appends " << "\n -----------------\n"; cout << " appending vowel characters:\n"; s1 += 'a'; cout << " " << s1 << endl; s1 += 'e'; cout << " " << s1 << endl; s1 += 'i'; cout << " " << s1 << endl; s1 += 'o'; cout << " " << s1 << endl; s1 += 'u'; cout << " " << s1 << endl; cout << " size of str object is " << s1.size() << endl; cout << "\n appending string \"yyy\": "; 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 << "\n testing assignment operator " << "\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 << "\n testing memory resizing by multiple appends " << "\n ---------------------------------------------\n"; cout << " appending \"C++ is great!!! \" several times to this string:\n\n"; int i; for(i=0; i<5; i++) { s1 += "C++ is great!!! "; s2 = s1; cout << " " << s2 << endl; cout << " size of str object is now " << s2.size() << "\n\n"; } cout << "\n testing insertion and extraction operators " << "\n --------------------------------------------\n"; ofstream out("test.dat"); if(!out.good()) { cout << "\n can't open file for writing strings\n\n"; return; } str insert1 = "Now is the time for all good men "; insert1 += "to come to the aid of their country"; out << insert1 << endl; str insert2 = "The rain in Spain falls mainly on the plain"; out << insert2 << endl; str insert3 = "This is the last line"; out << insert3 << endl; out.close(); ifstream in("test.dat"); if(!in.good()) { cout << "\n can't open file test.dat for input\n\n"; return; } str extract; while(in.good()) { in >> extract; cout << " " << extract << endl; } cout << endl; } #endif