ICS 45C Fall 2021
Exercise Set 5

Due date and time: Friday, November 5, 11:59pm

Getting started

First of all, be sure that you've read the Reinforcement Exercises page, which explains everything you'll need to know, generally, about how the reinforcement exercises will work this quarter. Make sure you read that page before you continue with this one.

Before you begin work on these reinforcement exercises, there's a chore that you'll need to complete on your ICS 45C VM to get it set up to proceed.

Refreshing your ICS 45C VM environment

Even if you previously downloaded your ICS 45C VM, you will probably need to refresh its environment before proceeding with these exercises. Log into your VM and issue the command ics45c version to see what version of the ICS 45C environment you currently have stored on your VM. Note, in particular, the timestamp; if you see a version with a timestamp older than the one listed below, you'll need to refresh your environment by running the command ics45c refresh to download the latest one before you proceed with these exercises.

2021-10-30 13:48:51
Exercise Set 5 template added

If you're unable to get outgoing network access to work on the ICS 45C VM — something that afflicts a handful of students each quarter — then the ics45c refresh command won't work, but an alternative approach is to download the latest environment from the link below, then to upload the file on to your ICS 45C VM using SCP. (See the Project #0 write-up for more details on using SCP.) Once the file is on your VM, you can run the command ics45c refresh_local NAME_OF_ENVIRONMENT_FILE, replacing NAME_OF_ENVIRONMENT_FILE with the name of the file you uploaded; note that you'd need to be in the same directory where the file is when you run the command.

Creating your project directory on your ICS 45C VM

A project template has been created specifically for this set of exercises, containing a similar structure to the basic template you saw in Project #0. Among other things, it contains a version of the gather script that's different from the ones in the projects, so you'll absolutely need to use the set5 template for these exercises, as opposed to the basic one.

Decide on a name for your project directory, then issue the command ics45c start YOUR_CHOSEN_PROJECT_NAME set5 to create your new project directory using the set5 template. (For example, if you wanted to call your project directory set5, you would issue the command ics45c start set5 set5.)

Problem 1 (2 points)

Suppose that you've implemented a class called FGH and that it has only one private member variable, a pointer x that points to an object of another class called JKL. It is not stated in particular here what problems FGH or JKL are intended to solve, nor the reason why FGH is storing the pointer x in a member variable. (To avoid biasing your answer to the questions below, I'm not specifying these things, because the questions ask you to explore various possibilities, focusing on why we would make certain design decisions instead of others.)

Given these assumptions and no others, answer the following questions about them.

  1. Under what circumstances would it be necessary to implement a destructor in the class FGH? Under what circumstances would it be fine to leave it out?
  2. Suppose you felt that you needed to implement a destructor in FGH. Under what circumstances would it also be necessary to implement a copy constructor in the class FGH? Under what circumstances would it be fine to leave it out?
  3. Suppose you felt that you needed to implement a destructor in FGH. Under what circumstances would it also be necessary to overload the assignment operator in the class FGH? Under what circumstances would it be fine not to overload it?

What to submit

Add one PDF file to your problems directory with this name: problem1.pdf, which contains your answers to these questions.

Problem 2 (2 points)

We saw that a copy constructor in a class X is normally declared with its parameter's type as const X&. We also saw that a compiler will not be willing to compile a copy constructor whose first parameter is instead declared with the type X.

Suppose that the compiler was willing to compile it instead and that it worked according to the usual rules of C++. What would you expect to be the effect of writing a copy constructor this way and then calling it?

What to submit

Add one PDF file to your problems directory with this name: problem2.pdf, which contains your answers to this question.

Problem 3 (2 points)

We saw that it's common for an overloaded assignment operator in a class X to be declared with the return type X&. However, this turns out not be to an absolute requirement in C++; a program can compile with the return type being something different.

Suppose that you wrote a class X with each of the following assignment operators declared. For each of them, briefly explain why making the specified design choice, rather than the usual one, would be problematic.

  1. void operator=(const X& x)
  2. X operator=(const X& x)

Answer these separately; in each case, only one of the assignment operators is declared, never both of them.

Note that it wouldn't be an honest attempt to answer this question to say something like "Because C++ programmers usually declare an assignment operator as X& operator=(const X& x) instead." This question isn't asking what the usual design choices are; this question is about why.

What to submit

Add one PDF file to your problems directory with this name: problem3.pdf, which contains your answers to these two questions.

Problem 4 (4 points)

Create a C++ header file problems/problem4.hpp and a C++ source file problems/problem4.cpp.

In the header file, you'll write the interface of a class called DynamicPair. In the source file, write the implementation of that class. In deciding what belongs in the header file and what belongs in the source file, differentiate the way we did in the Classes and Well-Behaved Classes examples, with the basic goal being to say as little as possible in the header file while still making it possible to use the class in source files other than problems/problem4.cpp.

The DynamicPair class should meet the following requirements:

Your implementation should be as simple as possible but no simpler. Only write things that need to be written in order to meet these requirements. We'll be happy to help you to understand these requirements, but we will not be able to answer questions about how to meet them in C++ or what your design should look like; that's what the question is asking you to consider for yourself.

What to submit

One C++ header file named problem4.hpp and one C++ source file named problem4.cpp, which you'll need to create in the problems directory, and which contain your declaration and implementation of the DynamicPair class.


In Canvas, you'll find a separate submission area for each problem. Submit your solution to each problem into the appropriate submission area. Be sure that you're submitting the correct file into the correct area (i.e., submitting your Problem 1 solution to the area for Problem 1, and so on). Under no circumstances will we offer credit for files submited in the incorrect area.

Submit each file as-is, without putting it into a .tar.gz file or arranging it in any other way. (There is a gather script in the set5 template, but there's no need to use it.) If we asked for a PDF, for example, all we want is a PDF; no more, no less. If you submit something other than what we asked for, we will not be offering you any credit on the submission. There are no exceptions to this rule.

Of course, you should also be aware that you're responsible for submitting precisely the version of your work that you want graded. We won't regrade an exercise simply because you submitted the wrong version accidentally, and we won't be able to offer any credit on exercises that you forgot to submit.

Can I submit after the deadline?

Unlike some of the projects in this course, the reinforcement exercises cannot be submitted after the deadline; there is no late policy for these. Each is worth only 3% of your grade, so it's not a disaster if you miss one of them along the way.

Can I work on this outside of the VM?

Yes, but be aware that you must submit the files in the appropriate format, and that any C++ code you submit must compile and run on the ICS 45C VM.