CS44 Weekly Lab: week 2

C++ Exceptions, reference parameters, const

A reminder that Week 1 Lab Topics included lots of information about C++ programming tools. All the links to documentation are also available off my help pages


Start by creating a w02 subdirectory in your cs44/weeklylabs subdirectory into which you will copy over some files:
  cd ~/cs44/weeklylabs
  mkdir w02 
  cd w02
  pwd
  cp ~newhall/public/cs44/week02/* .
  ls


C++ Reference Parameters
In general, don't use C++ style reference parameters in code you write. However, reference parameters are used in some of the library code that we will use in subsequent lab assignments.

Let's look at the example code in refparams.cpp and talk about the semantics and syntax of C++ Reference Parameters. Then try running it to see what is going on.

Some more information about references see Chapt. 11

C++ Exceptions
Let's look at the example code in exceptions.cpp. This shows some examples of throw, try-catch, and ignoring exceptions.

This example also uses catch by reference. This is an example of one of the few cases when C++ reference types add value (in catch by pointer it is not clear if the catch block should delete the thrown exception object or not).

Some more information about C++ exceptions

const
const is a modifier that indicates that the value of the thing it modifies cannot be changed. It can be very useful, but it also can be very frustrating to get right and use. You will see it some library code in this class, but I suggest that with the exception of using it to define constants in your program (vs. #define), you avoid using it in code you write from scratch in this class.

Let's look at some examples of its use in consts.cpp. Note: this code does not compile due to errors in using const.

Some more information about const see Chapt. 8