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, and submit on Gradescope to check your solution. 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)
Part 1
Pull the starter code for this exercise from the public repo by taking the following steps:
-
Navigate to the public repo and run
git pull
to update your clone of the public repository, and then copy theexercises/ex27
directory into your personal git repository. -
Confirm that you can see the template files for today’s exercise by typing
ls exercises/ex27
– you should see files namedMakefile
,README
,grade_list.[h/cpp]
, andmain[1/2/3].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. You can compile and run the code using commands make main1
and ./main1
.
Follow the instructions in the comments in grade_list.h
and grade_list.cpp
. Specifically, the comments will ask you to implement the mean and median member functions by putting a function definition for each in the grade_list.cpp
file. You should not change anything in the grade_list.h
file.
After you’ve implemented these, uncomment the lines at the end of main1.cpp
to check your code is running correctly.
Part 3
Follow the instructions in the comments in main2.cpp
. Specifically, the comments will ask you to fix the code, which does not work as written. You may modify the GradeList
class but do not change the access modifier (e.g. private
) of any of the members.
You can compile and run the code using commands make main2
and ./main2
.
Hints: think about the best way to access the private variables such that data that should not be changed, cannot be changed by main2
. Also consider the efficiency of your code and whether it is doing unnecessary copying. Discuss possible approaches with your classmates and instructor.
Part 4
Follow the instructions in the comments in main3.cpp
. Specifically, the comments will ask you write some new code using the GradeList
class. You should not need to modify the GradeList
class for this part.
You can compile and run the code using commands make main3
and ./main3
.
Remember to add and commit to your local repo copy as your work. Push to your remote repo when finished. Also scp and 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!