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.
Reinforces concepts learned in today meeting:
- C++ classes, fields & member functions
- Access modifiers (public & private)
- Dynamic memory allocation in C++
- Alternate constructors
- Destructors
Today’s exercise is a variation of what we did in the last exercise. Rather than having a vector inside the GradeList
class, we will have a dynamically allocated array that gets resized, as necessary. You will also create an alternate constructor (with default parameters) and (later) a destructor.
Part 1
Pull the starter code for this exercise from the public repo by taking the following steps:
-
Log into ugrad, then navigate to the public repo and type
git pull
to synchronize your local repo and working copy with the remote repo. -
Copy the starter code for today from the public repo
exercises/ex28
into your personal repo where desired. Then navigate to your personal repo folder for this exercise. You should see files namedMakefile
,grade_list.[h/cpp]
, andmain[1/2].cpp
inside.
Part 2
Read the instructional comments in grade_list.h
and grade_list.cpp
. Read main1.cpp
. It shows an example of how to use the given code for the GradeList
class.
Try to compile main1.cpp
. It doesn’t work because several things are missing from the GradeList
class, including definitions of the constructor, the add
functions, and the clear
function. Follow the instructions in the comments in grade_list.h
and grade_list.cpp
to write these missing functions for which we have provided headers within the grade_list.h
file. You should not change anything in the grade_list.h
file. Try to not reinvent the wheel when writing the add functions.
You can compile and run the code using commands make main1
and ./main1
.
The capacity of the GradeList
object should never be
less than one. You should check for this when implementing the
constructor.
When calling the constructor, the input parameter specifies the
target capacity
not the number of elements stored in the
GradeList
object.
When implementing the clear
member function you should
end up with a GradeList
object with zero elements but with
a capacity of one.
Part 3
Run valgrind
on main1.cpp
. Add a destructor to the GradeList
class in order to prevent the memory leak. This should free the memory for the array. If that does not take care of everything, consider whether you wrote the clear function properly and if the add function is leaking memory when you resize.
Part 4
Try to compile main2.cpp
. It doesn’t work because several things are missing from the GradeList
class, including methods begin()
and end()
that mimic the behaviour of an iterator using actual pointers, as well as a default constructor. Add the missing pieces to the GradeList
class (in .h
and/or .cpp
files). When you modify the GradeList
class do not change the access modifier (e.g. private
) of any of the members. Check to make sure this program does not have any memory leaks either, using valgrind
again.
You can compile and run the code using commands make main2
and ./main2
.
Remember to add and commit to your local repo copy as your work. Push
to your remote repo when finished. [No need to submit to Gradescope to
check your solution.] Use exit
to logout from your ugrad
account when finished. If you continue to work on the program after
class, make sure to keep your repo updated as well!