Topical Information

The purpose of this quiz is to give you a chance to focus your knowledge of libraries (aka separate compilation) in C++.

Question Set Information

Questions

  1. Show the preprocessor directives needed in ANY library interface (.h) file. What is the meaning of the symbol involved? Why must this symbol be unique? (Hint: Proof by contradiction is quite useful sometimes...)

    
            In any interface file, we need the following lines:
    
                #ifndef SYMBOL
                #define SYMBOL
    
                #endif
    
            The SYMBOL represents the fact that the interface file has been
            included.  It is normally created by uppercasing the interface's
            filename and appending _INC — representing that it has been
            included.
    
            This symbol must be unique to this library so that we don't have a name
            clash when trying to include multiple libraries.  Let's assume for a
            moment that we can use the same symbol in two separate libraries A and
            B.  Let's call our common symbol S.  Both library A and library B will
            have this preprocessor code in their interfaces:
    
                #ifndef S
                #define S
    
                #endif
    
            Of course, what is between the #define and the #endif will
            differ in each case.  But, upon seeing the first library
            #included, A for instance:
    
                #include "A.h"
    
            The preprocessor will see that it has not defined the symbol S and
            therefore will define this symbol and pull in A's code up thru the
            #endif.  Later, we'll #include library B:
    
                #include "B.h"
    
            Now what happens?  The preprocessor sees that it has already defined
            S.  So it skips everything until the #endif that matches our
            #ifndef at the top.  It has skipped over all of B's code!  But
            surely the program #included this library because it wanted to use
            its code...  Here we have a contradiction to our original assumption
            that two libraries could share a common symbol in their
            inclusion-protection and still both work within a program.
    
    
  2. An interface file for a library normally contains _____, _____, and _____.

    1. prototypes, definitions, mainNO
    2. constants, typedefinitions, prototypesYES
    3. cake, coffee, ice creamNO
    4. classes, structures, enumerationsOKAY
    5. functions, variables, ADTsNO

    The classes, etc. choice is okay because those things listed are all special kinds of typedefinitions. The choice listing also prototypes and constants is preferred because it is more complete.

    Special circumstances (such as _____ functions) also warrant the inclusion of _____ and any support _____ to which they need access.

    1. void, references, local variablesNO
    2. default, initialization, overloadingNO
    3. class, private data, utility functionsNO
    4. inline, function definitions, library headersYES
    5. constructor, classes, dataNO
  3.  
    TRUE/FALSE When I use a function from the standard library, I must remember to place a prototype for it in the main application.
    TRUE/FALSE When I extract functions to a library, I must leave their prototypes in the main application.
    TRUE/FALSE A library is typically designed to be used with just a single application.
    TRUE/FALSE I can only place functions to manipulate 2D points in a library.
    TRUE/FALSE C libraries work [essentially] the same as C++ libraries.
  4. When compiling a program which uses libraries, you must remember to place the names of all the source files after the CPP command — separated by spacing, of course. (In a Windows-style environment you would normally create a project and insert all of the C++ (.cpp) files in it.) We don't have to ever mention the interface (.h) files in the compilation process because they are #included in any C++ (.cpp) file which needs them.

  5. What possible problem are we trying to eliminate with the multiple preprocessor directives in a library interface file? (Hint: There are actually two related problems — one causes the other.)

    
            multiple inclusion — which can cause warnings or errors from
                                  duplications in code
    
            circular inclusion — which can cause the compiler to be caught
                                  in an infinite loop or even crash from a
                                  [function call] stack overflow
    
    
  6. Why do we #include the interface of a library in its implementation file? (Hint: It is not to see the prototypes of the functions we are about to define!)

    
            We need it to be sure we have any constants or typedefinitions
            from the interface which are sure to be used in our implementation.
    
            Having the prototypes is nice since it relieves us from having to
            define our functions in a certain order, but is not terribly
            necessary.
    
    
  7.  
    TRUE/FALSE We surround a local header file in double quotes ("") for #include'ing.
    TRUE/FALSE We can also surround a standard header file in double quotes ("") for #include'ing.
    TRUE/FALSE However, we usually surround a standard header file in angle brackets (<>) for #include'ing.
  8. If you need to #include a standard library in your library's interface file, you should not place 'using namespace std;' there, too. Why not?

    
            Placing a using directive in an interface file would force any
            programmer #includeing it to use the indicated namespace.
            That's just rude and possibly error inducing!
    
    

    What should you do instead?

    
            Place the std:: syntax in front of all library symbols used
            within the interface file (except the old C constants whose
            identifiers are formed in ALL_CAPS!).  Or, in special cases, a
            using directive can be placed inside an inline
            function definition — thus limiting its scope of effectiveness
            to that function alone.