|
|
|
|
|
|
|
The following table summarizes the correspondence between a parameter's data flow and the appropriate parameter-passing mechanism. |
|
|
|
|
| Data Flow for a Parameter | Parameter-Passing Mechanism | | Incoming | Pass-by-value | | Outgoing | Pass-by-reference | | Incoming/outgoing | Pass-by-reference |
|
|
|
|
|
|
There are exceptions to the guidelines in this table. C++ requires that I/O stream variables be passed by reference because of the way streams and files are implemented. We encounter one more exception in Chapter 11. |
|
|
|
| |
|
|
|
|
Conceptual Versus Physical Hiding of a Function Implementation |
|
|
|
| |
|
|
|
|
In many programming languages, the encapsulation of an implementation is purely conceptual. If you want to know how a function is implemented, you simply look at the function body. C++, however, permits function implementations to be written and stored separately from the main function. |
|
|
|
| |
|
|
|
|
Larger C++ programs are often split up and stored into separate files on a disk. One file might contain just the source code for the main function; another file, the source code for one or two functions invoked by main; and so on. This organization is called a multifile program. To translate the source code into object code, the compiler is invoked for each file independently of the others. A program called the linker then collects all the resulting object code into a single executable program. |
|
|
|
| |
|
|
|
|
When you write a program that invokes a function located in another file, it isn't necessary for that function's source code to be available. All that's required is for you to include a function prototype so that the compiler can check the syntax of the call to the function. After the compiler is done, the linker finds the object code for that function and links it with your main function's object code. We do this kind of thing all the time when we invoke library functions. C++ systems supply only the object code, not the source code, for library functions like sqrt. The source code for their implementations are physically hidden from view. |
|
|
|
|
|
|
|
|
|
(text box continues on next page) |
|
|
|
|
|