CS35: Data Structures and Algorithms

Lab 04 Notes

Lab 04 Agenda

Clone lab04

This is the same as lab03. Remember to run ssh-add when you log in, so you don't get repeatedly asked for you ssh password. Go to the CS35 github org, to find the clone link for your repo on the web interface.
$ cd ~/cs35/labs
$ git clone <link you got from github> ./lab04
You should also update your examples repository by going in your examples directory and pulling the updated version of the repository
$ cd ~/cs35/examples/
$ git pull

Setup Symbolic Link

Just like in lab01, we establish a symbolic link to a shared directory that contains files to test your program. You can create the link by executing the following commands:
$ cd ~/cs35/labs/lab04
$ ln -s /usr/local/doc/lab04-data/ ./test_data

Note the path /usr/local/doc/lab04-data/ is local only to the machines on the CS network and will not work if you clone your code to your personal computer.

ADT Paths

Throughout the semester, we will also use a shared directory in which we will write new ADTs as we cover them in class. You will need this folder to access the ADT for lists.

The ADT can be included directly in your code using, e.,g.

#include <cs35/list.h>
You cannot edit this file directly, but clang will know where to look to find this file. The full path is at /usr/local/include/cs35/list.h, but we will also be posting the code on the web in the adts folder. Clicking on e.g., list.php will allow you to browse the sourse with syntax highlighting.

We will add more ADTs to that folder as we go through the material of the course.

Exceptions

In Lab 02 you may have seen the following line in the starter code:

throw runtime_error("Not yet implemented: Text::getName()");
This line throws an exception which, so far, made the program crash. Exceptions are often used to indicate undesirable behavior on the part of the user or the program.

Just like in Python and Java, it is possible to catch these exception to make your program recover gracefully. You can find an example of exceptions being used and caught in your examples repository. In your lab assignment this week, your program will have to throw exceptions when certain events occur. Just note that in order to throw and catch exceptions, you will need to include the exception library:

#include <stdexcept>

The list.h ADT includes in the documentation the set of situations in which you must throw an exception. The unit tests we provide this week will check that you review this documentation and throw exceptions when appropriate. For example, there are several operations that would not work on an empty list. Your implementation needs to throw exceptions in these cases.

Templates

We showed you in class how to write a class for an ArrayList of strings. Writing a class for an ArrayList of int, long, float, Points, etc. would require copy pasting the exact same code, but changing the type contained in the array. Doing this again and again every time we need a list for a new type would get tiresome fast.

C++ provides a construct that enables a programmer to write classes or functions parameterized with a type, called a template, so that we can write the code once, and then use it for any type for which the code would be valid (e.g. the type has to allow for the operations performed on the variables).

We give you examples of a templated function and a templated class in your examples repository. The syntax is a little strange at first, especially for templated classes, but, like all C++ syntax, you'll eventually get used to it. For example:

template <typename T>
T gimmeMax(T a, T b)
is a function that takes as input two elements of the same type and returns one element of that type. The type of the input parameters is not specified, and most of the time, the compiler will be able to automatically detect the type of input given to the function and execute the code of the template without further indications. When indicating the code
gimmeMax(5, 10);
the compiler automatically detects that the input parameters are integers, and executes the template with all the type indicators T replaced by int. This automatic type detection is not fool-proof though, and it will sometimes be necessary to indicate the type of the inputs, as follows:
gimmeMax<string>("hello", "world");

The syntax for template class declaration is similar, but the syntax for the definition of methods is decidedly goofy. For example, here is the beginning of the definition of one of the methods of the class Mypair from the examples repo:

template <typename T1, typename T2>
T1 Mypair<T1,T2>::getFirst()
We gave you that part of the code for most of the methods you will need to implement for your lab assignment, but take note of the syntax, you will probably have to use it at some point.

When declaring a variable of a class described by a template, it is very strongly recommended that you do not rely on the automatic detection of the type, and that you always indicate the types that should be used in the template, as follows:

Mypair<string, float> mp("fish", 3.5);
One last details about templates: it is common practice to always put templates in a .h file, not in a .cpp file (sometimes, all the templates are placed in a special .inl file that is included in a .h file). This is because the compiler cannot compile templates until if figures out the types with which the templates are used. So since they are not "compilable" code, they are usually left in a .h file.

Many of the data structures we implement for the remainder of the semester will use these templates. In particular, the linked list class you will implement for the lab assignment this week will be a templated class.

Observe that list.h and linkedList.h are templated classes. They store a list of a generic type T. At the bottom of linkedList.h, we have the line

#include "linkedList-inl.h"
Normally, a header file does not have this line at the end and instead has a corresponding .cpp file that implements the class. But Since the implementation of the templated code must be included in the header file for compiler reasons, we typically see this trick with templated code.

Inspecting linkedList-inl.h shows that this file looks pretty much like a .cpp file, but with some extra templating syntax. Recall in a .cpp file, every implementation of method was prefixed by the name of the class and two colons, e.g., Text::open(). The syntax is similar here, but there is the added syntax of declaring the method and class to be templated.

template <typename T>
LinkedLis<T>::LinkedList() { ... }

Questions regarding template syntax are strongly encouraged.

Lab 04

That's it for in lab exercises. Hop over to the Lab 04 Writeup to begin the lab.