In this lab we'll get away from VisualWorks for a while and try playing with C++. Hopefully, after doing this lab you'll have a better feel for
The procedure is pretty simple. Save the buggy C++ source code and call it library.cc just so that we can refer to it by that name throughout this document.
Then open up a UNIX shell window on your workstation. You will use it to compile (and maybe edit) the program.
First thing you should do is read over the whole program. Figure out
what it's trying to do. Look at the class declarations. Look at the
class constructors and other methods. Look at the main()
function. Spend some time (about 5 to 15 minutes) just looking and
building a picture of what the program does in your mind.
In case you are not yet familiar with C++, here are some things you need to watch out for:
Author *by_author;
This is saying that by_author is of type reference
to Author. It should hold a pointer to an
Author object. When there is a member function you need
to invoke, you use the arrow operator ``->'',
for example
by_author->some_member_function(and_possibly_parameters)
Take a look at another line of code:
CatalogCard *cards[NUMBER_CARDS];
Here cards is an array, and each element of the array is
a reference to CatalogCard.
++ mean? Well it is an increment by 1
operator. The following line of code
i ++;
is equivalent to
i = i + 1;
And if the ++ operator follows a variable, the operation
is executed after everything else in its immediate scope.
For example
cards[nbooks ++] = card;
is equivalent to
cards[nbooks] = card,
nbooks ++;
Quick, what does a -- operator put in front of a variable do?
It will be helpful if now you grab a sheet of paper and draw the diagram describing the classes in this program. Look at this diagram and see if you can spot some of the things wrong with this source code.
After you have spent the time looking and drawing, dig into the code. Try compiling it (see below). If it doesn't work, fix what you think is wrong with it, and try again. Repeat this process until the program executes correctly. There are a number of debugging techniques that one can learn and derive along the way, but this isn't really the time or place to go into that.
To compile the program source code into something you can execute on the machine, you will use the a C++ compiler. It is generally called g++ and should be in your default path. It will generate an executable called a.out by default. To run it, simply type the name of the executable and hit the RETURN key. To make the executable a bit easier to identify, you can pass an alternate name to the compiler. If you called the source code library.cc like we suggested, then the following will compile and execute the program:
g++ library.cc -o library
library
If you did that and had the compiler print a whole bunch of lines on your screen, then the program still has compile-time errors in it. Try to interpret the error messages, edit the source, and try again.
After you have debugged successfully, you should be able to see output that looks like:
-----------------------------------------------Title: A Connecticut Yankee Author: Real Name: Clemens, Samuel L. Pen Name: Mark Twain Lived 1835 to 1910 There are 4 books by this author in the library. Pages: 352 Catalog: Fiction Row 7 Status: Checked out
Now you need to change the code a little so as to make the titles of other books written by the same author be listed too. For example, it should look like:
-----------------------------------------------Title: A Connecticut Yankee Author: Real Name: Clemens, Samuel L. Pen Name: Mark Twain Lived 1835 to 1910 There are 4 books by this author in the library. The other 3 book(s) are: What Is Man? Tom Sawyer Detective Adventures of Huckleberry Finn Pages: 352 Catalog: Fiction Row 7 Status: Checked out
Some hints are in the comments of the program.
You need to turn in your result as well as your fixed code. You can get a result file using this:
library > result
Then, mail the result and source code with this command:
cat library.cc result | elm -s "TI,Lab6,0123456789" cs2390@prism.gatech.edu
where you should fill in your own student number.
News Page | CS2390 Sum'97 Home Page | MMC-CaMILE | STABLE
Questions/comments/concerns to guzdial@cc.gatech.edu
Page last updated 7/31/97; 4:54:05 PM