Lab 7


Lab #7 - Working With C++

Lab #7
Debug, Compile, and Run a C++ Program



What We're Gonna Do:

In this lab we'll get away from Squeak 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 that you find at this link as plain text. Call it campaign.cc just so that we can refer to it by that name throughout this document. Save it onto your H: Drive, because we will be using that to do all our work.

Then open up another window on your workstation and log into one of the cc machines. This will be the system you use for reading mail from your cc account, which most likely is lennox or gaia. If you don't know all the details about this, ask your neighbors or the TA.

Debugging the program

First thing you should do is read over the whole program. Figure out what it's trying to do. Read the comments and look at the class declarations. Look at the class constructors and other methods. Look at the main() function. Spend enough time (about 15 minutes) just looking and building a picture of what the program does in your mind.

If you feel that you need extra help, here are a couple of links here that should be useful. The first one is a C++/SmallTalk comparison. The second one is an on-line C++ tutorial.

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.

You don't have to read everything in the comparison file. Just glance over the important stuff, and if you know C++ already, you may not have to read it at all. It's more of a refresher.

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. Always try to fix the top error first, because it may also be causing errors underneath it. Lather, rinse, repeat. 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.

This program has a number of syntactic errors. If you can fix it and make it compile, it should work correctly, because there is no logic error in this lab. However, if you change the code too much, you may enter in additional errors yourself that aren't there.

How to Compile and Run

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 it can be invoked by typing "/usr/local/bin/g++". It will generate an executable called a.out by default. To run it, simply type the name of the executable and hit return. To make the executable a bit easier to identify, you can pass an alternate name to the compiler. If you called the source code campaign.cc like we suggested, then the following will compile and execute the program:
       /usr/local/bin/g++ -o campaign campaign.cc
       campaign 

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.

Turnin

So what do we turn in? For this lab, you should turn in a copy of your fixed source code.


Back to the top | Over to CoWeb