Course Web Pages

Related Links

Free eBooks


Scope

Beginning C++ Through Game Programming, by Michael Dawson.

Scoping, Part 1

 scoping.cpp


Scoping, Part 2


Global Reach, Part 1

 global_reach.cpp


Global Reach, Part 2

 global_extern.zip


Global Reach, Part 3


Exercises

These problems are from an MIT course made available through MIT's OpenCourseware

For these questions, you are encouraged to use a computer.

  1. Below is a sample program. Use it to answer the following question: What happens if we declare the same name twice within a block, giving it two different meanings?
    code from mit problem

    Hints: Did your program compile? If so, what does it print? If not, what error message do you get?

     

  2. Below is a sample program. Use it to answer the following question: What happens if we declare an variable in a block, and then redeclare that same variable in a block nested within that block?
    code from mit problem

    Hints: Did your program compile? If it does, what does the program output? If not, what error message does it produce?

     

  3. Below is a sample program. Use it to answer the following question: Suppose a variable has two different declarations, one in an outer block and one in a nested inner block. If the name is accessed within the inner block, which declaration is used?
    code from mit problem

     

  4. Below is a sample program. Use it to answer the following question: Suppose a variable has two different declarations, one in an outer block and one in a nested inner block. If the name is accessed within the outer block, but after the inner block, which declaration is used?
    code from mit problem

     

  5. Below is a sample program that will not compile. Why not? By moving which line can we get the code to compile?
    code from mit problem