29 Feb: Revised lstring.h, naming the lstring object's linked list seq because the input operator>> assumed its name was seq. If you have given the linked list a different name, e.g., foo, just change the appropriate line of operator>>. For example, change
s.seq = temp;to
s.foo = temp;
You and possibly a CS1321 classmate are to implement a string class using linked lists.
The Standard Template Library has a wonderful string class, which greatly eases use of strings in C++. In this homework, we implement a class called lstring that provides part of the functionality of the string class.
Our strategy for implementing lstring is to store each character of the string1 as an element of a Seq linked list. For example, the string ``cheerio'' is stored as a list of length seven with the second list element being `h'. While there are many ways to implement classes, we recommend (and, in fact, require) implementing lstring using the C++ class notation presented in the shopping cart example.
lstring objects should support the operations listed in Table 1. Also, the friend members in Table 2 should be implemented. For example uses, see the demo program.
|
The length of a string is its number of characters. In particular, an empty string has length 0.
A character's position within a string is the number of characters before it in the string. The very first character has no characters before it so it is in position 0. When inserting characters into a string, the specified position is just before the character of the same number. For example, inserting at position 0 specifies inserting before character at position 0, i.e., the very first character. It is legal to insert at any position up to and including the string's length, i.e., at the end of the string.
lstrings are ordered according to alphabetic ordering where spaces and capitalization are important. For example, ``hello world'' is less than ``helloworld'' because the space character is less than `w'. ``Hello'' is less than ``hello'' because `H' is less than `h'. If two strings have exactly the same characters but one is shorter than the other, the shorter string is less. For example, ``hell'' is less than ``hello''. When comparing individual chars, use the default ordering, which is usually, but not always, ASCII ordering. Thus, one can just use < to determine which of two characters is less.
C++ is a very verbose language, so we rarely have time to introduce all the notation during lecture. Read the textbook chapter 2 for more explanations.
The const keyword is an indication that a variable's value cannot change. It is not strictly necessary, but effective use of this keyword can yield a measurable reduction in running time. Function parameters are frequently declared const type & variable. const means the parameter's value may not be changed. Passing by reference (&) is a hack that permits writing faster code since passing by reference is frequently faster than copying an entire function argument. See also the textbook pp. 66-67.
Another use of const is in a declaration of a member function. For example, bool empty(void) const; indicates that the empty member function will not change any values in the object for which it is called. Thus, this member function can be used for a const object. When defining a const member function, write const between the function prototype and the opening bracket {. See also the textbook p. 33.
As we saw in Homeworks 2 and 3, typedefs permit creating synonyms for types. This is also legal inside a class definition. For example, we may write
typedef unsigned int length_pos
inside lstring's definition. Inside any code implementing the class, we can freely refer to length_pos. If the typedef is public, users of the code can refer to it as lstring::length_pos.
There are three essential files and one recommended file.
There are two styles for implementing C++ classes. The textbook declares member functions in the class declaration and implements them separately, sometimes in a separate file. The other style is presented in the shopping cart example. Use whichever style makes you more comfortable.
To use the code, write a .cc file containing a main function definition. Be sure to #include "lstring.h" and put the file in the same directory as lstring.h. Then compile the .cc file. For example,
g++ -Wall -pedantic demo-lstring.cc -o demo-lstring
As for previous homeworks, working with other people during your planning phase is encouraged. For this homework, you are permitted to write the code, i.e., program, with one other person in CS1321. To learn the material, both of you should be actively involved in the programming. Failure to do so will almost certainly hurt your comprehension of the material in the rest of the course.
Each one- or two-person team of programmers should submit only its completed lstring.h. You need not send any other files. Please send only text documents; do not send Microsoft Word documents, PDF documents, HTML documents, etc. Please include both your names and email addresses at the top of your program.
We will test your code using our own main() function. Please be sure it compiles without warning when using g++ -Wall -pedantic.
See the guidelines for programming assignments for instructions on how to e-mail the programs. For this assignment use a subject line of ``cs1321 homework 4''.
If a team of two are in different sections, submit exactly once to one of the two permissible email addresses.