The Entire StrType Class. McGraw-Hill The Complete Reference Fourth Edition
Reklama
Reklama
C h a p t e r 3 9 : I n t e g r a t i n g N e w C l a s s e s : A C u s t o m S t r i n g C l a s s
949
The conversion to char * does circumvent encapsulation, because once a function has a pointer to the object's string, it is possible for that function to modify the string directly, bypassing the StrType member functions and without that object's knowledge. For this reason, you must use the conversion to char * with care. You can prevent the underlying string from being modified by having the conversion to char * return a const pointer.
With this approach, encapsulation is preserved. You might want to try this change on your own.
The Entire StrType Class
Here is a listing of the entire StrType class along with a short main( ) function that demonstrates its features:
#include <iostream>
#include <new>
#include <cstring>
#include <cstdlib> using namespace std; class StrType { char *p; int size; public:
StrType();
StrType(char *str);
StrType(const StrType &o); // copy constructor
~StrType() { delete [] p; } friend ostream &operator<<(ostream &stream, StrType &o); friend istream &operator>>(istream &stream, StrType &o);
StrType operator=(StrType &o); // assign a StrType object
StrType operator=(char *s); // assign a quoted string
StrType operator+(StrType &o); // concatenate a StrType object
StrType operator+(char *s); // concatenate a quoted string friend StrType operator+(char *s, StrType &o); /* concatenate a quoted string with a StrType object */
950
C + + : T h e C o m p l e t e R e f e r e n c e
StrType operator-(StrType &o); // subtract a substring
StrType operator-(char *s); // subtract a quoted substring
// relational operations between StrType objects int operator==(StrType &o) { return !strcmp(p, o.p); } int operator!=(StrType &o) { return strcmp(p, o.p); } int operator<(StrType &o) { return strcmp(p, o.p) < 0; } int operator>(StrType &o) { return strcmp(p, o.p) > 0; } int operator<=(StrType &o) { return strcmp(p, o.p) <= 0; } int operator>=(StrType &o) { return strcmp(p, o.p) >= 0; }
// operations between StrType objects and quoted strings int operator==(char *s) { return !strcmp(p, s); } int operator!=(char *s) { return strcmp(p, s); } int operator<(char *s) { return strcmp(p, s) < 0; } int operator>(char *s) { return strcmp(p, s) > 0; } int operator<=(char *s) { return strcmp(p, s) <= 0; } int operator>=(char *s) { return strcmp(p, s) >= 0; } int strsize() { return strlen(p); } // return size of string void makestr(char *s) { strcpy(s, p); } // null-terminated string
}; operator char *() { return p; } // conversion to char *
// No explicit initialization.
StrType::StrType() { size = 1; // make room for null terminator try { p = new char[size];
} catch (bad_alloc xa) { cout << "Allocation error\n"; exit(1);
} strcpy(p, "");
}
// Initialize using a quoted string.
StrType::StrType(char *str) { size = strlen(str) + 1; // make room for null terminator try { p = new char[size];
} catch (bad_alloc xa) {
C h a p t e r 3 9 : I n t e g r a t i n g N e w C l a s s e s : A C u s t o m S t r i n g C l a s s
951
} cout << "Allocation error\n"; exit(1);
} strcpy(p, str);
// Initialize using a StrType object.
StrType::StrType(const StrType &o) { size = o.size; try { p = new char[size];
} catch (bad_alloc xa) { cout << "Allocation error\n"; exit(1);
} strcpy(p, o.p);
}
// Output a string.
ostream &operator<<(ostream &stream, StrType &o)
{ stream << o.p; return stream;
}
// Input a string.
istream &operator>>(istream &stream, StrType &o)
{ char t[255]; // arbitrary size - change if necessary int len; stream.getline(t, 255); len = strlen(t) + 1; if(len > o.size) { delete [] o.p; try { o.p = new char[len];
} catch (bad_alloc xa) { cout << "Allocation error\n"; exit(1);
}
952
C + + : T h e C o m p l e t e R e f e r e n c e
} o.size = len;
} strcpy(o.p, t); return stream;
// Assign a StrType object to a StrType object.
StrType StrType::operator=(StrType &o)
{
StrType temp(o.p); if(o.size > size) { delete [] p; // free old memory try { p = new char[o.size];
} catch (bad_alloc xa) { cout << "Allocation error\n"; exit(1);
} size = o.size;
} strcpy(p, o.p); strcpy(temp.p, o.p);
} return temp;
// Assign a quoted string to a StrType object.
StrType StrType::operator=(char *s)
{ int len = strlen(s) + 1; if(size < len) { delete [] p; try { p = new char[len];
} catch (bad_alloc xa) { cout << "Allocation error\n"; exit(1);
} size = len;
}
C h a p t e r 3 9 : I n t e g r a t i n g N e w C l a s s e s : A C u s t o m S t r i n g C l a s s
953
} strcpy(p, s); return *this;
// Concatenate two StrType objects.
StrType StrType::operator+(StrType &o)
{ int len;
StrType temp; delete [] temp.p; len = strlen(o.p) + strlen(p) + 1; temp.size = len; try { temp.p = new char[len];
} catch (bad_alloc xa) { cout << "Allocation error\n"; exit(1);
} strcpy(temp.p, p); strcat(temp.p, o.p);
} return temp;
// Concatenate a StrType object and a quoted string.
StrType StrType::operator+(char *s)
{ int len;
StrType temp; delete [] temp.p; len = strlen(s) + strlen(p) + 1; temp.size = len; try { temp.p = new char[len];
} catch (bad_alloc xa) { cout << "Allocation error\n"; exit(1);
}
954
C + + : T h e C o m p l e t e R e f e r e n c e strcpy(temp.p, p); strcat(temp.p, s);
} return temp;
// Concatenate a quoted string and a StrType object.
StrType operator+(char *s, StrType &o)
{ int len;
StrType temp; delete [] temp.p; len = strlen(s) + strlen(o.p) + 1; temp.size = len; try { temp.p = new char[len];
} catch (bad_alloc xa) { cout << "Allocation error\n"; exit(1);
} strcpy(temp.p, s); strcat(temp.p, o.p);
} return temp;
// Subtract a substring from a string using StrType objects.
StrType StrType::operator-(StrType &substr)
{
StrType temp(p); char *s1; int i, j; s1 = p; for(i=0; *s1; i++) { if(*s1!=*substr.p) { // if not first letter of substring temp.p[i] = *s1; // then copy into temp s1++;
C h a p t e r 3 9 : I n t e g r a t i n g N e w C l a s s e s : A C u s t o m S t r i n g C l a s s
955
}
} else { for(j=0; substr.p[j]==s1[j] && substr.p[j]; j++) ; if(!substr.p[j]) { // is substring, so remove it s1 += j; i--;
} else { // is not substring, continue copying temp.p[i] = *s1; s1++;
}
}
} temp.p[i] = '\0'; return temp;
// Subtract quoted string from a StrType object.
StrType StrType::operator-(char *substr)
{
StrType temp(p); char *s1; int i, j; s1 = p; for(i=0; *s1; i++) { if(*s1!=*substr) { // if not first letter of substring temp.p[i] = *s1; // then copy into temp s1++;
} else { for(j=0; substr[j]==s1[j] && substr[j]; j++) ; if(!substr[j]) { // is substring, so remove it s1 += j; i--;
} else { // is not substring, continue copying temp.p[i] = *s1; s1++;
}
}
}
956
C + + : T h e C o m p l e t e R e f e r e n c e
} temp.p[i] = '\0'; return temp; int main()
{
StrType s1("A sample session using string objects.\n");
StrType s2(s1);
StrType s3; char s[80]; cout << s1 << s2; s3 = s1; cout << s1; s3.makestr(s); cout << "Convert to a string: " << s; s2 = "This is a new string."; cout << s2 << endl;
StrType s4(" So is this."); s1 = s2+s4; cout << s1 << endl; if(s2==s3) cout << "Strings are equal.\n"; if(s2!=s3) cout << "Strings are not equal.\n"; if(s1<s4) cout << "s1 less than s4\n"; if(s1>s4) cout << "s1 greater than s4\n"; if(s1<=s4) cout << "s1 less than or equals s4\n"; if(s1>=s4) cout << "s1 greater than or equals s4\n"; if(s2 > "ABC") cout << "s2 greater than ABC\n\n"; s1 = "one two three one two three\n"; s2 = "two"; cout << "Initial string: " << s1; cout << "String after subtracting two: "; s3 = s1 - s2; cout << s3;
C h a p t e r 3 9 : I n t e g r a t i n g N e w C l a s s e s : A C u s t o m S t r i n g C l a s s
957 cout << endl; s4 = "Hi there!"; s3 = s4 + " C++ strings are fun\n"; cout << s3; s3 = s3 - "Hi there!"; s3 = "Aren't" + s3; cout << s3; s1 = s3 - "are "; cout << s1; s3 = s1; cout << "Enter a string: "; cin >> s1; cout << s1 << endl; cout << "s1 is " << s1.strsize() << " characters long.\n"; puts(s1); // convert to char * s1 = s2 = s3; cout << s1 << s2 << s3; s1 = s2 = s3 = "Bye "; cout << s1 << s2 << s3;
} return 0;
The preceding program produces this output:
A sample session using string objects.
A sample session using string objects.
A sample session using string objects.
Convert to a string: A sample session using string objects.
This is a new string.
This is a new string. So is this.
Strings are not equal.
s1 greater than s4 s1 greater than or equals s4 s2 greater than ABC
Atsisiųsti
Reklama