Example: Because strings are stored in arrays, it is possible to attempt to access locations outside of the memory allotted to the string:Such attempts access memory that the code shouldn't, and may cause the program to crash.char label[10] = "value"; label[-1] = 'a'; // Eeek! label[200] = 'z'; // No such location here!
Example: A string cannot easily grow. In other words, without doing some dynamic allocation, the maximum size of a string is predetermined:char label[10] = "value"; ... // Not enough room, though it will attempt it anyhow! strcpy(label, "How now brown cow?");
Example: Using library functions with strings is not terribly natural:It would be more natural to be able to do:if (strcmp(label1, label2) > 0) // Ugh! strcpy(label1, label2);if (label1 > label2) // Ahhhh... label1 = label2;
Example: Determining the length of a string is inefficient in that it takes more time to find the nul character (\0) the longer the string:char name[] = "my name"; char dict[] = <Insert the contents of the dictionary here>; cout << "My name is " << strlen(name) // Fast << " characters long." << endl; cout << "The dictionary is " << strlen(dict) // La, la, la... << " characters long." << endl;
class String {
// FUNCTIONS (OR "METHODS")
// - allowing a user of the string to set up, access or
// change the string.
// DATA
// - needed to store the contents of the string and any other
// values that assist in our representation of the string.
};
By defining such a String class, we can solve our two
problems:
Your String class will have the following design:
The String class code must be put in its own module, i.e.,
a file mystring.h (the header file) should hold
the class definition and a file mystring.cpp (the
implementation file) should hold the method definitions.
Each String object should contain the following data as part of its internal representation:
The String class should provide the following methods for setting up, accessing and changing strings:
String();A default constructor that sets up the String to be an empty string.
void assign(const char s[]);A method that copies the contents of the passed string into the String.
void append(const String &str);A method that appends the contents of the passed String onto the end of the String the method is called on.
int compare_to(const String &str) const;A method that compares the String this method is called on with the String passed to the method. If the string is greater than the string passed, it should return a positive value; if less than, a negative value, and if equal, the value zero.
void print() const;A method that prints the value of the String it is called on to
cout.
int length() const;A method that returns the length of the String that it is called on.
char element(int i) const;A method that returns the ith element (zero-based, just like arrays) of the String it is called on. This method should print out an error message and return the nul character (
\0)
if the location asked for is out-of-range for the string.
string.h, which operate on arrays of
characters terminated by nul's (\0).
String-test.cpp
provides a main program to test your new