Today's lecture and studio focus on type declaration and usage issues that may arise when specifying and using different expressions, variables, and parameters. These issues are essential to the generic programming paradigm that we will consider for the rest of this semester, and addressing them establishes foundations for how type-related issues can be managed in C++ which we will expand on in later topics.
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.
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 the CMakeLists.txt from one of your previous studios into that
directory, and add a source file for your program's main function. As you
work on this studio, please update the CMakeLists.txt as needed so that it will build an
executable program called studio12 from the files in that directory.
Add a source file to your directory, and in it define a main function that declares the following variables on separate lines of code:
const int
variable initialized with the value 0int variable
initialized with the value 1const pointer to a
const int variable initialized with the address of the
const int variableconst pointer to a
const int variable initialized with the address of the non-const
int variableconst pointer to a non-const
int variable initialized with the address of the
const int variableconst pointer to a
non-const int variable initialized with the address of the
non-const int variableconst int variable initialized with the address of the
const int variableconst int variable initialized with the address of the non-const
int variableint variable initialized with the address of the
const int variableint variable initialized with the address of the
non-const int variable
The main function should then (in separate lines of code) print to cout
the values of the const int and non-const int variables,
and for each of the pointers the address it contains followed by the value of what
it points to.
Try to build your program, and comment out any lines that will not compile. As the answer to this exercise please show the lines that had to be commented out, and for each of them explain briefly why it could not be compiled.
In your main function, after the output statements that remain, write separate
statements (on separate lines of code) that use the prefix ++
operator (which is built-in for int and pointer types) to modify
the value of each of the variables whose declarations were not commented out,
and for the pointer variables add a separate statement that modifies what the
pointer points to.
Try to build your program, and again comment out any of the newly added lines that will not compile. As the answer to this exercise please show the lines that had to be commented out, and for each of them explain briefly why it could not be compiled.
In your main function, after the declaration of each of the integer and pointer
variables, add another declaration (on a separate line of code) for another
variable that is declared using the auto type specifier and is
initialized with the original variable.
For each of those variables that was declared using the auto type
specifier, use the prefix ++ operator to determine whether or not
it can be modified (and in the case of pointer types also to determine whether
or not what it points to can be modified).
Try to build your program, and again comment out any of the newly added lines
that will not compile.
As the answer to this exercise please explain whether or not either low-level
const or top-level const properties were discarded in any of the declarations
that used the auto type specifier and if they were show the
declaration and explain briefly which of them was discarded and how you know
that.
In your main function, replace the auto type specifier with the
the decltype type specifier in each of the declarations that
you added in the previous exercise.
For each of the variables in those declarations, again use the prefix
++ operator to determine whether or not it can be modified
(and in the case of pointer types also to determine whether
or not what it points to can be modified).
Try to build your program, and again comment out any of the newly added lines
that will not compile. As the answer to this exercise please explain whether
(and if so how) the use of the decltype type specifier differed from
the use of the auto type specifier, in terms of whether or not
either low-level const or top-level const properties were discarded in any of
the declarations.
Add typedefs for the following types to your main program: int,
const int, const pointer to int,
const pointer to const int, non-const pointer to
int, and non-const pointer to const int.
In your main function, replace the type portion of the declaration of each of the integer and pointer variables with the appropriate one of those type aliases.
Compile and run your program, and as the answer to this exercise please show the declarations that use those type aliases.
For this studio, please turn in the following: