CSE 4208: Studio 7

Namespaces


In today's studio you will explore how to access types that already are scoped within existing namespaces, how to declare and define types within new namespaces that you will introduce, and how to access those types once you have done that.

Please complete the following required exercises. I encourage you to please work in groups of 2 or 3 people on each studio (and the groups are allowed to change from studio to studio) though if you would prefer to complete any studio by yourself that is also allowed.

As you work through these exercises, please record your answers, and when you finish them please log into Canvas, select this course in this semester, and then upload a .txt file containing your answers on the Canvas page for this studio assignment. Only one submission per team, please, and if you need to re-submit it the person who originally submitted the studio should please be the one to do that.

Make sure that the name of each person who worked on these exercises is listed in the first answer, and make sure you number each of your responses so it is easy to match your responses with each exercise.


Required Exercises

  1. As the answer to the first exercise, list the names of the people who worked together on this studio.
  2. First, ssh into shell.cec.wustl.edu using your WUSTL Key id and password and then use qlogin to log into one of the Linux Lab machines and then confirm that the version of g++ there is correct (as you did in Studio 0). Then cd into your directory for this course this semester, create a new subdirectory for this studio, and cd into it.

    Copy over the header files and source files for the Weekday and Pressure enumerated types from Studio 4 into your directory for this studio, and in those files make sure that (1) any library types that appear in the header files are all explicitly scoped with their namespaces (e.g., std::ostream) and (2) each of the source files says using namespace std; and those types are used in the source files without namespace scoping (e.g., just ostream).

    Add a new source file to your directory for this studio, and in it say using namespace std;, then below that define a main function that declares variables of the Weekday and Pressure types that are initialized to their lowest possible values and iterates through each of their possible values printing each one to the standard output stream (cout) with spaces in between them and a line break (by inserting the endl manipulator into cout) after the highest value has been output for each of them.

    Compile and run your program, and as the answer to this exercise, please show the output it produced.

  3. Replace the using namespace std; statement in each of your source (.cpp) files with the appropriate using statements that don't use the keyword namespace and instead use the name of the namespace, the scoping operator, and the name of the type.

    Build and run your program and confirm that it produced the same output as before. As the answer to this exercise, please show the newly added using statements.

  4. Wrap the declarations and definitions for your Weekday and Pressure enumerated types, and for the operators and functions for them, within the a common namespace (called something like enumerations) that you open and close within each of the appropriate source and header files. Without modifying the source file for your main program, try to build your code and as the answer to this exercise please show the compilation errors that occurred.

  5. Then, add an appropriate using namespace statement to the source file for your program, so that you are able to build and run the program successfully. As the answer to this exercise please (1) show the statement that you added, and (2) explain briefly why you didn't have to add that statement to the source files that contain the definitions for the Weekday and Pressure enumerated types.

  6. In the source file for your main program, replace the using namespace statement you added for the previous exercise with individual using statements that don't use the keyword namespace and instead use the name of the namespace, the scoping operator, and the name of the type. As the answer to this exercise, please show the statements that you added.

Things to Turn In:

For this studio, please turn in the following: