Lab: Skills - Developing C++ Programs


Instructions: Answer as many of the following questions as you can during the lab period. If you are unable to complete the assignment during the lab period it is strongly recommended that you complete it on your own.

You do not need to submit anything; this lab is voluntary.

Getting Ready: Before going any further, you should:

  1. Make a directory for this lab.
  2. Start the editor (e.g., jGrasp, VIM, Emacs) of your choose.
  3. Open a terminal/command-shell window. (In the Windows lab, click on the MSYS icon for the Unix-like shell, or click on Start-Run and enter cmd.exe for the Windows shell. In the OS X lab, click on the Finder and then Places-Applications-Utilities-Terminal. In the Linux lab, click on Applications-Accessories-Terminal.)
  4. Download the following files:
    to your working directory. (In most browsers, the easiest way to do this is by right-clicking on each of the links above.)
  5. Briefly review the following documents:

1. Compiling, Linking and Executing a Simple Application: This part of the lab will make sure that you can compile, link and execute a simple application.
  1. In the terminal/command-shell window, build the application by entering g++ Test.cpp
  2. What files were created?

  3. Execute the application by entering either ./a or ./a.out (depending on the name of the file that was created).
  4. Delete/remove all files other than those ending in .h and .cpp
  5. Compile Test.cpp by entering g++ -c Test.cpp
  6. What files were created?

  7. Link the application (creating an executable named Test) by entering g++ Test.o -o Test
  8. What files were created?

  9. Execute the application by entering ./Test
2. Experiencing a Common Mistake: This part of the lab will give you some experience with a mistake that is commonly made by beginning C++ programmers.
  1. Delete the semicolon at the end of Shopper.h
  2. Compile Shopper.cpp by entering g++ -c Shopper.cpp
  3. What errors were generated?

  4. Replace the semicolon at the end of Shopper.h.
3. Using the Preprocessor to Manage Includes: This part of the lab will help you undertand why it is necessary to manage "includes".
  1. Read and understand ShopperInOneFile.cpp (which contains both the declarations and the implementations for the Shopper class).
  2. Read and understand AnotherClassThatUsesShopper.cpp which does nothing but #include ShopperInOneFile.cpp
  3. Compile ShopperInOneFile.cpp
  4. Compile AnotherClassThatUsesShopper.cpp
  5. Read and understand Oops.cpp (which #includes both ShopperInOneFile.cpp and AnotherClassThatUsesShopper.cpp).
  6. Compile Oops.cpp
  7. What errors were generated?

  8. Why were these errors generated?

4. Building a More Complicated Application: This part of the lab will help you understand how to compile and link an application that involves multiple classes/files.
  1. Delete/remove all files other than those ending in .h and .cpp
  2. Read and understand Driver.cpp.
  3. Compile the Shopper class by entering g++ -c Shopper.cpp
  4. What files were created?

  5. Compile the Driver by entering g++ -c Driver.cpp
  6. What files were created?

  7. Link the two object files by entering g++ Driver.o Shopper.o -o Driver
  8. What files were created?

5. Using make: This part of the lab will help you understand why the make utility is useful and how to use it.
  1. What steps would you need to perform build Driver if you made a change to Shopper.h?

  2. Why is this tedious?

  3. The make utility can (and should) be used to significantly reduce the tedium of the build process. To get started, create/edit a file named makefile.
  4. The first line of the makefile needs to indicate that Driver depends on Driver.o and Shopper.o. To do so, add the following line to the makefile:
    Driver: Driver.o Shopper.o
        
  5. The second line of the makefile needs to indicate what needs to be done to create Driver if anything it depends on changes. To do so, add the following line to the makefile:
        g++ Driver.o Shopper.o -o Driver
        
    Note: This line should start with a tab.
  6. What files does Driver.o depend on?

  7. What needs to be done to create Driver.o?

  8. Add a description block for Driver.o to your makefile.
  9. What lines did you add?

  10. Complete the makefile.
  11. What's in your final makefile?

  12. Delete/remove all files other than those ending in .h and .cpp
  13. Use the make utility by entering make
  14. What files were created?

  15. Change a comment in Shopper.h and re-save the file.
  16. Use the make utility by entering make
  17. What steps were performed?

Copyright 2010