// Simple String class // ------------------- #ifndef SIMPLE_STRING #define SIMPLE_STRING const SimpleString #include // This class provides a minimal character-string capability suitable // for classroom or textbook examples not concerned with techniques // of string class design or use. // Its main purpose is to support string-valued functions. It provides // concatenation but no substring or parsing functions. // For more complete or more efiicient character string classes, see // std::string or the IDI library classes Dstring, Vstring, and Cstring. class SimpleString { char* value; // Internal representation is int size; // null-terminated array of char public: SimpleString (const char *const arg = "") {size = strlen(arg); value = new char[1+size]; strcpy(value,arg); } SimpleString (const char x) {size = 1; value = new char[2]; value[0] = x; value[1] = '\0'; } SimpleString (SIMPLE_STRING& x) {size = x.size; value = new char[1+x.size]; strcpy(value,x.value); } ~SimpleString() {delete value;} SimpleString& operator=(SIMPLE_STRING& rs) {if (size != rs.size) // Check for reallocation needed {delete value; value = new char[1+rs.size];} strcpy(value,rs.value); return *this; } friend SimpleString operator+(SIMPLE_STRING ls, SIMPLE_STRING rs) {SimpleString result; result.size = ls.size + rs.size; delete result.value; result.value = new char[1 + result.size]; strcpy(result.value,ls.value); strcat(result.value,rs.value); return result; } static SimpleString toString(INT N, SHORT base) {int n = abs(N); static const char digitTable[17] = "0123456789ABCDEF"; char result[17]; result[16] = '\0'; int posn = 15; do {result[posn--] = digitTable[n%base]; n /= base; } while(n > 0); return result+posn+1; } friend ostream& operator<<(ostream&ls, SIMPLE_STRING& rs) {return ls << rs.value;} }; #endif