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:
-
Make a directory for this lab.
- Start the editor (e.g., jGrasp, VIM, Emacs) of your choose.
- 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.)
-
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.)
-
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.
-
In the terminal/command-shell window,
build the application by entering
g++ Test.cpp
-
What files were created?
-
Execute the application by entering either
./a
or
./a.out
(depending on the name of the file that was
created).
-
Delete/remove all files other than those ending in .h and .cpp
-
Compile
Test.cpp
by entering g++ -c Test.cpp
-
What files were created?
-
Link the application (creating an executable named
Test
) by entering g++ Test.o -o Test
-
What files were created?
-
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.
-
Delete the semicolon at the end of
Shopper.h
-
Compile
Shopper.cpp
by entering
g++ -c Shopper.cpp
-
What errors were generated?
-
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".
-
Read and understand
ShopperInOneFile.cpp
(which contains
both the declarations and the implementations for the Shopper class).
-
Read and understand
AnotherClassThatUsesShopper.cpp
which does nothing but #include ShopperInOneFile.cpp
-
Compile
ShopperInOneFile.cpp
-
Compile
AnotherClassThatUsesShopper.cpp
-
Read and understand
Oops.cpp
(which #include
s
both ShopperInOneFile.cpp
and
AnotherClassThatUsesShopper.cpp
).
-
Compile
Oops.cpp
-
What errors were generated?
-
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.
-
Delete/remove all files other than those ending in .h and .cpp
-
Read and understand
Driver.cpp
.
-
Compile the
Shopper
class by entering
g++ -c Shopper.cpp
-
What files were created?
-
Compile the
Driver
by entering
g++ -c Driver.cpp
-
What files were created?
-
Link the two object files by entering
g++ Driver.o Shopper.o -o Driver
-
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.
-
What steps would you need to perform build
Driver
if you made
a change to Shopper.h
?
-
Why is this tedious?
-
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
.
-
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
-
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.
-
What files does
Driver.o
depend on?
-
What needs to be done to create
Driver.o
?
-
Add a description block for
Driver.o
to your makefile.
-
What lines did you add?
-
Complete the makefile.
-
What's in your final makefile?
-
Delete/remove all files other than those ending in .h and .cpp
-
Use the make utility by entering
make
-
What files were created?
-
Change a comment in
Shopper.h
and re-save the file.
-
Use the make utility by entering
make
-
What steps were performed?