Featured
- Get link
- X
- Other Apps
C++ Case Study.
This case study is primarily designed to illustrate the use of generic classes and inheritance. The STL will be applied in the case studies in later chapters. From the perspective of the operating systems, files are collections of bytes, regardless of their contents. From the user's perspective, files are collections of words, numbers, data sequences, records, and so on. If the user wants to access the fifth word in a text file, a searching procedure goes sequentially through the file starting at position 0, and checks all of the bytes along the way. It counts the number of sequences of blank characters, and after it skips four such sequences (or five if a sequence of blanks begins the file), it stops because it encounters the beginning of the fifth nonblank sequence or the fifth word. This word can begin at any position of the file. It is impossible to go to a particular position of any text file and be certain that this is a starting position of the fifth word of the file. Ideally, we want to go directly to a certain position of the file and be sure that the fifth word begins in it. The problem is caused by the lengths of the preceding words and sequences of blanks. If we know that each word occupies the same amount of space, then it is possible to go directly to the fifth word by going to the position 4?length(word). But because words are of various lengths, this can be accomplished by assigning the same number of bytes to each word; if a word is shorter, some padding characters are added to fill up the remaining space; if it is longer, then the word is trimmed. In this way, a new organization is imposed on the file. The file is now treated not merely as a collection of bytes, but as a collection of records; in our example, each record consists of one word. If a request comes to access the fifth word, the word can be directly accessed without looking at the preceding words. With the new organization, we created a random access file. A random access file allows for direct access of each record. The records usually include more items than one word. The preceding example suggests one way of creating a random access file, namely, by using fixed-length records. Our task in this case study is to write a generic program that generates a random access file for any type of record. The workings of the program are illustrated for a file containing personal records, each record consisting of five data members (social security number, name, city, year of birth, and salary), and for a student file that stores student records. The latter records have the same data members as personal records, plus information about academic major. This allows us to illustrate inheritance. In this case study, a generic random access file program inserts a new record into a file, finds a record in the file, and modifies a record. The name of the file has to be supplied by the user, and if the file is not found, it is created; otherwise, it is open for reading and writing. The program is shown in Figure 1.5. The function find() determines whether a record is in the file. It performs the search sequentially comparing each retrieved record tmp to the sought record d using an overloaded equality operator ==. The function uses to some extent the fact that the file is random by scrutinizing it record by record, not byte by byte. To be sure, the records are built out of bytes and all the bytes belonging to a particular record have to be read, but only the bytes required by the equality operator are participating in the comparison. The function modify() updates information stored in a particular record. The record is first retrieved from the file, also using sequential search, and the new information is read from the user using the overloaded input operator >>. To store the updated record tmp in the file, modify() forces the file pointer database to go back to the beginning of the record tmp that has just been read; otherwise, the record following tmp in the file would be overwritten. The starting position of tmp can be determined immediately because each record occupies the same number of bytes; therefore, it is enough to jump back the number of bytes occupied by one record. This is accomplished by calling database.seekp(-d.size(),ios::cur), where size() must be defined in the class T, which is the class type for object d. The generic Database class includes two more functions. Function add() places a record at the end of file. Function print() prints the contents of the file. To see the class Database in action, we have to define a specific class that specifies the format of one record in a random access file.
Please Comment below if you want the solution of case study.
Comments
Post a Comment