Absolute C++ 5th Edition By Walter Savitch – Test Bank
1. Any global declarations or definitions can be placed in a namespace.
Explanation: It is preferable to place definitions in one or more namespaces where the names may be managed so that name clashes may be prevented.
2. The include statement, #include “file.h” looks first in the system defined directory for file.h then, if the file is not found, it looks in the user’s current directory. (Windows PC and Macintosh users sometimes use “folder” for what I call “directory”.)
Explanation: The question had it backwards. The #include “file.h” makes the preprocessor look in the user’s current directory first, then in the system defined directory.
3. The include statement, #include <file.h> looks in the system defined directory for the file, file.h. (Windows PC and Macintosh users sometimes use “folder” for what I call “directory”.)
Explanation: Every system has one or more include directories that C++ compilers search for include files. The < > in #include <file.h> signal the preprocessor to look in the system directory.
4. A namespace is a collection of name definitions such as class definitions, variable definitions and function definitions used to permit the same name, or names, to be used both in a library and in your own code.
Explanation: By qualifying a name from a namespace with the namespace name and the scope resolution operator, the client can specify the name from the namespace.
5. In C++, a compilation unit is a class or function.
Explanation: A C++ compilation unit is the file. A file may contain the contents of include files (files that are present by way of #include directives), global namespace function and class definitions, an unnamed namespace, and possibly one or more named namespaces.
6. An unnamed namespace provides a facility for collecting names that are local to the file where the unnamed namespace is located. Such names are available in that file, and are unavailable in any other file.
Explanation: The names in an unnamed namespace can be reused in another unnamed namespace that is located in another file with no chance of name clash.
7. A function defined inside an unnamed namespace requires qualification.
Explanation: An unnamed namespace grouping behaves as if the unnamed namespace were named with a name that is not used anywhere else in the program, perhaps unique_1. The rest of the file behaves as if it had the directive,
using namespace unique_1;
so that the names from the namespace can be used in the file, but are unavailable in any other file.
8. You can use the static qualifier with a global function, class or variable to get the same effect as an unnamed namespace.
Explanation: This might work for a time, but for some future version of your C++ compiler, this will stop working. This use of the keyword static in C++ is being phased out. It is being replaced by unnamed namespaces. Some future version of the C++ compiler will give an error when compiling such code. (Programmers say, “Some later version of the compiler will break your code.”)
9. In a particular file, the names from the global namespace and names from an unnamed namespace defined in the file are accesses the same way: by writing the name.
Explanation: To use either name, one just uses the name. The compiler knows the difference. However, if the unnamed namespace name and the global name are the same, use of that name will cause an ambiguity error. The compiler will not be able to distinguish them. There is no mechanism to use the unnamed namespace name, but the global name can be accessed by qualifying with the just the scope resolution operator, for example, ::name.
10. If I just write code, it is not in any namespace.
Explanation: Every piece of code that is not explicitly put into a name space is in the global namespace, and could be addressed using the scope resolution operator, as in ::name.
11. The scope of a using directive or using declaration is from its position in the block to the end of the program.
Explanation: Using directives and declarations have scope exactly like a local variable. The scope extends from the declaration to the end of the block in which the declaration occurs.
12. You can have a name spelled the same in two different namespaces with no conflict in your program.
Explanation: The namespace groups names together and provides an access method that allows the same name to be used in several namespaces without conflict.
13. You can use #define to define a name for a C++ variable.
Explanation: #define defines a symbol for use by the C++ preprocessor in things such as preventing multiple inclusion and turning off the assert macro. C++ has
a different syntax for defining variables. This syntax is TypeName VariableName;