|
|
HP C++ Programmer's Guide: HP 9000 Series Workstations and Servers > Chapter 3 Compiling and Executing HP C++ ProgramsAn Extensive Example |
|
This section describes one model for designing a typical kind of C++ program. There are many ways to design a program. The method described here illustrates an object-oriented approach that uses data hiding. The discussion is organized according to the following topics:
Most programs are made up of several separately compiled units, usually files. The term module refers to a file containing a variable or function declaration, a function definition, or several of these or similar items logically grouped together. Thus, a program usually consists of several modules. A C++ service consists of the following: The interface is usually in one or more header files, or .h files. The implementation is usually in one or more .C or .c files associated with the corresponding .h files. Code in an application using the service is sometimes called a client of the service. Client source code is usually in a .C or a .c file. Suppose, for example, a simple lending library service is organized into two modules, library_ex.h and library_ex.C. The source file for this program resides in the directory /usr/contrib/CC/Examples (or /opt/CC/contrib/Examples/library_ex for HP-UX 10.x C++ versions.) The interface module, library_ex.h, contains the declarations of the objects in the service. Perhaps these would be class types named library, book, borrower, and transaction. The implementation module, library.C, contains the function definitions for the objects in the interface. Examples of these might be function definitions for library::display_books () and library::add_book(book*). A client of the library service could then consist of code in a .C file such as use_library.C. The sample program at the end of this chapter (example 3-1) is organized in just this way. This type of organization uses data hiding, since it allows you to make available to clients of the service only the names they need to know. You can hide information that a client need not know in the .C files, or, if necessary, keep the implementation in object file format (.o files) only. This type of service also provides considerable flexibility. An implementation can consist of one or more .C files, and you can provide several different interfaces in the form of .h files. The next section describes how the separate modules of the service can be linked together. Just like a program consisting of a single source file, a program consisting of many separately compiled parts must be consistent in its use of names and types. For instance, a name that is not local to a function or a class must refer to the same type, value, function, or object in every separately compiled part of a program. That is, there can only be one nonlocal type, value, function, or object in a program with that name. An object may be declared many times, but it must be defined exactly once. Also, the types must agree exactly in all the declarations of the object. Constants, inline functions, and type definitions can be defined as many times as necessary, but they must be defined identically everywhere in the program. The best way to ensure that the declarations in separate modules are consistent is to follow these steps: In example 3-1 of a library service, the use_library.C and library_ex.C files each contain the following line:
You could generate an object file from library_ex.C using the following command:
Similarly, you generate an object file from use_library.C using the following command line:
Finally, you link the object files to create an executable file named a.out, using the following command:
This section presents a simple example of a C++ service. The example is not intended to be a realistic application, but it illustrates the organization and concepts that have been discussed in this section. The service example is a lending library. Its principal objects correspond to the books in the library's collection (book) and people who are enrolled to borrow books (borrower). The service includes an interaction object (transaction), which associates a particular book with a particular borrower, and an object that contains book, borrower, and transaction objects (library). There is an abstract data type (list) from which all the other classes are derived, making it easier to handle lists of the various types of objects. The interface for the service is in the file library_ex.h, which lists the declarations for the book, borrower, transaction, library, and list classes. The implementation for the service is in the file library_ex.C, which lists the definitions for the book, borrower, transaction, library, and list classes. A client application program is listed in use_library.C. The source file for this program resides in the directory /usr/contrib/CC/Examples (or /opt/CC/contrib/Examples/library_ex for for HP-UX 10.x C++ versions.) To use the lending library service, put your source files in the same directory and follow the steps described above. To run the executable file, enter:
The rest of this chapter consists of Example 3-1, which shows the three files discussed above: library_ex.h, library_ex.C, and use_library.C. Figure 3-3 Example 3-1. A C++ Service: library_ex.h, library_ex.C, and use_library.C
|
|