
lafore_robert_objectoriented_programming_in_c
.pdf

Chapter 13
676
|
[ !(fr here || cd here) ] |
||
|
Going |
|
|
|
up |
|
|
[ fr above || |
[fr here || |
[cd above] |
|
cd here] |
|||
|
|||
cd above ] |
|
||
|
|
||
|
[ no fr && no cd ] |
|
|
Unloading |
Loading |
Getting |
|
Stopped |
|||
cd |
|||
Clear cd |
Clear fr |
||
|
|||
[cd here] |
[fr here] |
|
|
[ fr below || |
[fr here || |
|
|
cd here] |
|
||
cd below ] |
[cd below] |
||
|
Going down
[ !(fr here || cd here) ]
FIGURE 13.6
State diagram of an elevator object.
Summary
Vendor-provided object libraries are often distributed as a public component (the interface) containing class declarations in an .H header file, and a private component (the implementation) containing member function definitions in an .OBJ object file or .LIB library file.
C++ compilers allow you to combine several source or object files into a single executable file. This permits files provided by one vendor to be combined with files from another, to create a final application. The project feature simplifies keeping track of what files need to be compiled. It compiles any source file that has been modified since the last linking, and links the resulting object files.
Inter-file communication requires that variables, functions and class objects be defined in one file and declared in any other file where they’re used. A class definition must be placed in every file where objects are instantiated. Care must be taken with both source files and header files to ensure that multiple definitions don’t occur.


Chapter 13
678
12.True or false: a variable defined within a function body can be seen thoughout the file in which it’s defined.
13.A global variable is defined in file A. To access the variable in file B, you must
a.define it in file B using the keyword extern.
b.define it in file B using the keyword static.
c.no other action is necessary (do nothing).
d.declare it in file B using the keyword extern.
14.The region in a program where a variable can be accessed by variables in other parts of the program is called its __________.
15.The files that are actually combined by the linker are called _________ files.
16.A function is defined in file A. To call it from file B, the function must first be
__________ in ________ .
17.True or false: a function declaration does not require the keyword extern.
18.To define class objects in different files, in each file you must
a.declare the class.
b.define the class.
c.declare the class using extern.
d.define the class using extern.
19.True or false: a variable defined in a header file can be accessed from two source files if they both include the header file.
20.The #if !defined()...#endif construction can be used to prevent multiple definitions when
a.Two header files are included in a source file.
b.A header file is included in two source files.
c.Two header files are included in two source files.
d.A header file is included in another header file and both are included in a source file.
21.You use namespaces to
a.Automate the naming of variables.
b.Restrict the area where program elements are visible.
c.Divide a program into separate files.
d.Prevent the use of long variable names.






Chapter 14
684
A Simple Function Template
Our first example shows how to write our absolute-value function as a template, so that it will work with any basic numerical type. This program defines a template version of abs() and then, in main(), invokes this function with different data types to prove that it works. Here’s the listing for TEMPABS:
//tempabs.cpp
//template used for absolute value function #include <iostream>
using namespace std; //--------------------------------------------------------------
template <class |
T> |
//function template |
|
T abs(T n) |
|
|
|
{ |
|
|
|
return |
(n < |
0) |
? -n : n; |
} |
|
|
|
//-------------------------------------------------------------- |
|
|
|
int main() |
|
|
|
{ |
|
|
|
int int1 = 5; |
|
||
int int2 = -6; |
|
||
long lon1 = |
70000L; |
||
long lon2 = |
-80000L; |
||
double |
dub1 |
= 9.95; |
|
double |
dub2 |
= -10.15; |
//calls instantiate functions cout << “\nabs(“ << int1 << “)=” << abs(int1); //abs(int) cout << “\nabs(“ << int2 << “)=” << abs(int2); //abs(int) cout << “\nabs(“ << lon1 << “)=” << abs(lon1); //abs(long) cout << “\nabs(“ << lon2 << “)=” << abs(lon2); //abs(long)
cout << “\nabs(“ << dub1 << “)=” << abs(dub1); //abs(double) cout << “\nabs(“ << dub2 << “)=” << abs(dub2); //abs(double) cout << endl;
return 0;
}
Here’s the output of the program:
abs(5)=5 abs(-6)=6 abs(70000)=70000 abs(-80000)=80000 abs(9.95)=9.95 abs(-10.15)=10.15