Exercise 30
Info

This is an in-class exercise. An exercise page like this one will contain a brief description but is intended to be supplemented by discussion during our meeting time. Complete the exercise to the best of your ability in the time given. Feel free to talk with other students as you work, and do not be afraid to ask questions if you get stuck. Aim to complete as much as possible during our meeting. You are encouraged to work at home to complete what you do not get through today, and ask questions over Piazza or in office hours.

Learning Objectives

Reinforces concepts learned in today meeting:

  • C++ classes, fields & member functions
  • Linked lists in C++
  • Operator overloading, friend functions, rule of three

Part 1

Pull the starter code for this exercise from the public repo by taking the following steps:

  1. Log into ugrad, navigate into our public repo, then type git pull to synchronize your local repo and working copy with the remote repo.

  2. Copy the files from the public class repository /exercises/ex30/ directory to your personal repo. Navigate to your personal repo copy – you should see files named Makefile, README, int_node.h, int_set.[h/cpp], and main1.cpp inside.

Part 2

Read main1.cpp. It shows an example of how to use the given code for the int_set class. You can compile and run the code using commands make main1 and ./main1. Some of the statements in main1.cpp will not compile yet because of missing functions in int_set.cpp. You can comment them out to check that the supplied code is working correctly.

Follow the instructional comments in README and int_set.cpp to write some missing functions for which we have provided headers. You should not change anything in the int_set.h file. Do not reinvent the wheel when implementing these functions! That is, consider how code reuse can make your job easier.

Part 3

Run valgrind on main1.cpp by calling valgrind --leak-check=full ./main1. Fix any memory leaks that it finds. Finally, add a new line in main1.cpp which assigns an existing non-empty set to itself, e.g. s = s; Investigate what occurs using valgrind! See if you are able to eliminate any problems that occur.