This lab's purpose is to show how names and scopes work in C++. This will be demonstrated with the compiling and testing of several C++ programs. These programs were downloaded and instructions followed off Richard Botting's assignment website.
This program will test the scope of the redefinitions of integer "i". It has a global scope defined as 2 and two more inner scopes.
i=1 i=2 i=3 i=4 i=4 i=4
It appears that i has been redefined in different scopes with later scopes overwriteing the original value of "i", defined in the global scope.
i=1 i=1 j=2 i=1 j=2 k=3 i=1 j=2
Like the last one, but this time there are locally declared variables like "k". Only blocks that the variable was declared in are able to call the variables and they can call global variables like i. Otherwise, these local variables cannot be referred to outside of its scope.
i=1 i=2 i=3 i=3 i=2 i=4 i=2
Here we have a globally-scoped variable "i" that is defined globally to 2. Then there are two locally-declared variables also named "i" that equals different values. In the end, the global variable "i" still equals 2 after the other "i"s were declared.
i=1 i=2 i=3 i=4 i=3 i=2
This program also demonstrates variables of the same name but different scopes carrying different values.