CS 1704 Fall 2002 Homework 3 Answers Q A Reason ------------------------------------------------------------------------------------ 1 3 "using namespace A" directs the compiler to search the namespace A whenever it does not (otherwise) find a declaration for an identifier. "using A::MAX" directs the compiler to map every use of the identifier A to the (presumed) declaration of A in the namespace A. So, either one will make the declaration of the array legal. 2 4 The declaration explicitly indicates that the declaration of MAX occurs within the namespace A, so nothing else is needed. 3 9 There are two namespaces involved here, std and Embroidery, and each contains a declaration of a type string. In this case, neither 1 nor 2 will accomplish anything useful; whether the compiler searches std or Embroidery first, it will find the same declaration of string both times it's trying to resolve the two parameter declarations and that's not what is needed. Similarly, neither 3 nor 4 will accomplish anything except to provide contradictory instructions to the compiler. 5 and 6 together will handle the problem (as long as the same idea is applied within the function body as well). It is possible that other combinations will work, but those weren't offered as choices. 4 1 Yes, but you have to be a little clever. You can handle the problem by wrapping the developer's declarations within a namespace yourself: namespace Wrapper { #include "Embroidery.h" }; Then, you adopt the solution from question 3. 5 2 The compiler searches first for a local declaration, and it finds one. Of course, it's of the wrong type, but that just causes a compiler error due to the mismatch. 6 2 Using "::A" tells the compiler to search the (nameless) global namespace for the declaration of A; that bypasses the local declaration entirely. 7 1 The type string is used in the class declaration, so its declaration must precede the class declaration; the only way to do that is to place the include directive in Name.h before the class declaration. (Of course, it's also necessary to do something else...) 8 5 The declarations in are inside the namespace std, so you must do something to direct the compiler to look there; either 1 or 2 will do that. 3 is simply irrelevant to the problem. 9 2 The body of Name::fullName() uses the operators + and = from the string class, but their declarations are associated with the class declaration for string, and the compiler only needs their declarations to be happy. 10 3 The header is needed in Name.cpp, and it won't be included there unless Name.h is included (with the given code organization). Also, the identifier Name is used in Name.cpp but its declaration is in Name.h. 11 2 When compiling Name.cpp, there won't be more than one include directive for Name.h, so there's no multiple inclusion problem. (It's still a good idea to use the conditional compilation directives anyway, since they may well be necessary when other files are compiled.) 12 2 StudentID uses both Name and string as types, so both header files are needed; BUT, Name.h already includes , so there's no reason to include separately. (Wouldn't do any harm though, since the header will use conditional compilation directives.) 13 1 StudentID.h and RoomAssignment.h both include the header Name.h; so it's being included twice in the code above. But neither of the other header files is included more than once. 14 1 Dogbert.cpp contains the implementations of two functions, but only one of them, Dogbert() is called by a function that's not in that file, so that's the only one whose declaration needs to be "published" in the header file. 15 7 Since both functons, Wally() and Alice(), are called by functions in other files, both function declarations need to be "published". 16 1 main() calls Dogbert(), so main.cpp needs to include Dogbert.h. No other function calls Dogbert(), and the declaration for Wally() will be in Dogbert.cpp, so no other file needs to include Dogbert.h. 17 5 main() calls Wally() and Dogbert() calls Alice(), so both main.cpp and Dogbert.cpp need to include Wally.h. 18 7 There certainly won't be LESS source code; in fact if you count the preprocessor directives, there will be more. But, if the code is well-organized in header/implementation files, then some of the individual components, like classes, will be easier to reuse because they are physically separated and already prepared to be linked with external code. And, recompilation only requires recompiling the cpp files that have actually been modified, so only part of the code typically must be processed. 19 2 Not 1, because the bundling is achieved by the class mechanism whether separate compilation is used or not. 2, certainly is an option; and by distributing an object file instead of the cpp file you hide the implementation details from the user. 20 2 There's no reason to "publish" declarations of entities that aren't used elsewhere, and good reason not to, so 1 is wrong. It IS necessary to "publish" declarations of entities that ARE used elsewhere, so 2 is correct. 3 is just silly.