CS35: Data Structures and Algorithms

Week 02 Lab Notes

Lab 02 Agenda

Clone lab02

This is the same as lab02. 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, you can find the clone link for your repo on the web interface.
$ cd ~/cs35/labs/
$ git clone <link you got from github> ./lab02
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  

Using Makefiles

So far, we have always compiled our projects using the clang compiler directly, even when the project consisted of multiple files. This becomes rather inefficient for large projects, so today, we introduce a much more efficient way of compiling projects: Makefiles.

Go to the ~/examples/week-03/dog folder and execute the command ls to get the list of files. Just as for Lab01, there are three .cpp files here that must be compiled together. We could compile them using

$ clang++ -std=c++11 -o dog dog.cpp poodle.cpp main.cpp
But instead, this time, we will use a Makefile. Feel free to open the Makefile with your favorite text editor to see what is there, but we will not explain it this time. For now, you only need to know that you can compile everything in the folder by executing the command
$ make
and you can clean up the folder by erasing all the .o files and the executable file dog by executing the command
$ make clean

Inheritance and Polymorphism

Last week, we talked about basic Object Oriented Programming (OOP) in C++. For lab this week, we will extend these basic concepts with Inheritance and Polymorphism. Inheritance allows us to define derived classes that inherit functionality from a parent base class. Polymorphism allows methods called on a pointer to a base class to call implementations defined in a derived class.

Let's look at some examples in the ~/examples/week-03/dog/ folder. There are also examples in the ~/examples/week-03/shape/ folder.

Polymorphism requires us to define some methods as virtual or pure virtual. The virtual keyword is added to the front of a method declaration in the base class to indicate it may be implemented differently in a derived class. Appending =0; to the method declaration makes a method pure virtual. These methods are not implemented at all in the base class and must be implemented in the derived class.

In our examples, Poodle and Dalmatian are derived classes of the base Dog class. Each of these derived classes, declare the pure virtual methods of the base class as normal methods. To indicate these classes inherit from the Dog base class we use the following class definition syntax:

class Poodle : public Dog { 
 /* body */
};
In main.cpp we show how to use polymorphism to get Dog pointers to act like Poodle and Dalmatian.

File I/O

See examples in the ~/examples/week-03/fileIO folder.

You will need to use something similar to parse your input files for lab02.

Working with Partners

In this lab, you and your partner will be sharing a common git repo on github. We will grade the final version that appears on github at the Sunday deadline. In addition to the usual add, commit, push cycle, you will need to use git pull to grab changes that your partner has pushed to your shared repo.

You should get in the habit of running git pull before starting your edits to make sure you have the latest copy. When are temporarily done with your edits, add, commit, push so your partner can know that you've made changes. In general, you and your partner should be working on the lab together, but it is important that each of you have access to the latest version of the code.

In the event of concurrent edits, you may encounter your first merge conflict. Do not panic. Instead, please carefully read the merge conflict guide for details on how to resolve conflicts and merge successfully. Do not blindly add, commit, and push a file that has a merge conflict as you will mostly break your code, and your partners code.