1. Goals for this week
-
Quick review of C++ programming, including C++
vector
andstring
, inheritance, and exceptions -
Quick demo of Gradescope for HW1
-
Progress check-in and help with Lab 1
2. Starting Point Code
Start by creating a week02
directory in
your cs44/weeklylabs
subdirectory and copying over some files:
# if you have not yet made your cs44/weeklylabs subdirectory do this first:
mkdir cs44
cd cs44
pwd
/home/you/cs44/
mkdir weeklylabs
cd ~/cs44/weeklylabs
mkdir week02
cd week02
pwd
/home/you/cs44/weeklylabs/week02
cp ~newhall/public/cs44/week02/* .
ls
The README.md
file includes some instructions for how to run these
examples.
3. C++ Object Oriented Programming
We are going to look at an example program that uses C++ vector
and string
classes, passes a pointer to a vector to functions, and
uses inheritance. The inheritance part is least applicable to your
current lab assignment, but we will see it used in some later labs
this semester.
3.1. Inheritance
In Worker.[cpp,h]
is definitions of a base class representing
workers. In Manager.[cpp,h]
is a derived class from Worker
that
adds additional data members and overloads virtual functions. We’ll start by
looking at Worker.h
together, then Manager.h
, and then main_workers.cpp
to see an example program that creates a vector of Worker
and Manager
objects.
Let’s briefly look at the code together, then try running
it (with and without a command line option specifying the number of Worker
objects to create):
make
./workers
./workers 5 # run with a command line arg
3.1.1. Compiling with debug output
In Worker.cpp
and Manager.cpp
are debugging output statements
that print information about which methods are called and the
order of destructors invoked in the examples that uses inheritance.
If you look in the Manager.cpp
and Worker.cpp
files,
you will see code like the following that conditionally prints out
debugging output based on the value of verbose
, which is set by
the preprocessor depending on if DEBUG
is defined or not:
// if DEBUG is defined set verbose to 1, else to 0
#ifdef DEBUG
static const int verbose = 1;
#else
static const int verbose = 0;
#endif
// the value of verbose is used to enable/disable debug output
if(verbose) {
std::cout << " In Manager :" << Name() << " constructor\n";
}
To enable this debugging output, we need to define
DEBUG
in Worker.h
(uncomment the #define DEBUG
in Worker.h
),
and then re-compile and run:
// uncomment to enable debug output
//#define DEBUG
When run, you will now see the debug output:
make
./workers
To disable this debugging output, just comment out the definition of
DEBUG
in Worker.h
, and recompile.
3.2. gdb breakpoints in methods
Let’s also run this in gdb
and see how to set a break point in a method
function. Tip: gdb
will help us with auto completion and listing options:
gdb ./workers
(gdb) break main
(gdb) run
# use ' to get gdb's help w/finding name of method in which we want to break
(gdb) break 'Manager:: <hit TAB here to see options>
3.3. C++ Exceptions
In my_exception.h
, there is an example of defining a C++ Exception class
(MyException
) as well as some examples of throwing (throw
) and
catching (try/catch
blocks) in main_exception.cpp
. Exceptions are the main
place we will see the use of const
and pass-by-reference style parameters
this semester (reference parameters have the same semantics as pass-by-pointer
parameters, but their syntax look like pass-by-value).
Let’s start by opening my_exception.h
and see the class definition,
and then main_exception.cpp
to see how to catch and throw exceptions.
We will run the program in a few ways:
make
./exceptions # triggers test for num command line args, prints usage, exits
./exceptions 10 0 # run without any exceptions
./exceptions 10 1 # run w/catch and throw exceptions (std and MyException)
4. Using Gradescope
For class Homework assignments and Quizzes, we will be using Gradescope. We will briefly run through a few features and demonstrate how you will use it for Homework 1. Here are the complete instructions for reference later:
-
Log in to Gradescope
-
Under Your Courses select CPSC 044. Any open homeworks and quizzes will be viewable here.
-
Select the current homework/quiz you want to complete by clicking on the Name field (e.g., Homework 1: Disk Storage and Buffer Manager).
-
You may now enter your answers using the provided text boxes. For certain questions, you will have an upload option if you want to write/draw your response and upload an image or PDF. Your submission must be legible to be graded. This is a perfectly acceptable option if it is more effective and/or efficient for you.
-
Be sure to save answers periodically by clicking Save next to questions as you complete/update your responses. You can also click Save All Answers at the bottom of the page. Don’t worry about this being final - you can edit all answers up until the deadline.
-
When you are done, click on Submit & View Submission. You may edit and resubmit up until the deadline by going to the assignment and clicking Resubmit in the bottom right corner. View Submission History if you want to revert to an older version.
Some additional features:
-
Gradescope allows the use of Markdown and LaTeX (using double dollar signs to surround items in Math mode, e.g., $$e=mc^2$$). This is not required.
-
For homeworks, you will be able to work in groups. One person will control the group submission - pick this person before beginning. All members of the group are expected to have worked on all problems together - do not "divide and conquer". For Homework 1, you can work in groups up to size 4.
-
To select group members, you will first need to click on Submit & View Submission at the bottom to get to the screen. You can do this at any time (remember, submitting is not final, you can still work on your responses).
-
After hitting Submit & View Submission, you will see information about the submission to the right. Click on View or edit group under GROUP. Alternatively, you can click Group Members at the bottom of the submission screen to edit your list of group members.
-
Select all group members under the Add Student dropdown menu. Click Add when done.
-
It is an academic integrity violation to work with other groups or members of other groups to come up with solutions. If you decide not to work in a group anymore after solving a few problems, you will need to work on your own or with a subset of the original group.
-
-
-
We will grade and post grades on Gradescope. In addition, solutions for Homeworks will be viewable there after the deadline.
-
We are new to Gradescope, so please let us know if something is not working as expected.
-
Quizzes will use Gradescope in a similar format, except that you cannot communicate with anyone other than the instructors, and the quiz will be timed.
5. C++ Programming
As you work on the current lab assignment, remember the C++ programming, debugging tools, code style guide, and links to further help from the Wed Lab from Week 1 page.