CSE 283 Introduction to Object Oriented Design

Barbara Nostrand, Ph.D.

CSE 283 Labs
Welcome to the site for CSE 283 Labs!


Lab 4: C++ Project

Prev | Next | Lab 4



1. Practice writing sequential programs in C++.
2. Practice writing library header files for C++.
3. Practice writing make files.


Both C++ and Java support separate compilation and libraries of program modules.  However,  they provide this support in rather different ways.  While Java software systems consist of a colleciton of class files which are interpretted by the Java Virtual Machine,  executable C++ programs are single files which are directly executed by the computer hardware.  Make files are a mechanism for automating compiling and linking together program modules.  While this is useful for Java,  this ability to automatically sequence compiling and linking quickly becomes necessary for developing C++ programs. 


Implement a version of Metric in C++.  As this is intended to be a library file to be used by other programs,  you need to write a number of files in addition to metric.cpp

  • A metric.doc dcoument file containing an English language specification for each of the library functions sufficient to allow people to use the module correctly. 
  • A metric.h header file with skeletons for each of the public functions in the metric.cpp source file. '
  • A driver.cpp file which will contain a main function for test purposes. 
  • A makefile to automate building an executable version of the system. 

Using Header Files

Typically,  larger software systems are built from a collection of source code files.  In Java,  we achieve this by associating each public class definition with its own source code (.java) file and class (.class) file.  A large system in Java is constructed by using import to inform the Java compiler where to find class files stored in system libraries.  C and C++ are used to construct a single directly executable binary file.  Further,  C and C++ strictly require that variables and functions be declared before they can be used.  This makes it difficult or even impossible to construct large C and C++ systems without some way to declare functions before they are defined.  We can achieve this by including prototypes for each of the the functions used at the beginning of C and C++ source code files.  This might look like:

   int test();  
   int foo();
    int foo() {   
      return test();  
    int test() {      
      return 5;  

If you want to use a compiled library of C or C++ functions,  then you need to include a prototype for each of the functions that you use at the beginning of your own C or C++ source code file.  This is messy and unreliable as you would have to make sure to exactly copy long lists of funciton prototypes at the beginning of each of your source code files.  The C and C++ compilers simplify this by allowing you to include a header file.  Header files have the same name as the corresponding C (.c) or C++ (.cpp) file,  but have a .h extension.  Each time you write a C or C++ source code file,  you should also write a header file which includes a prototype for each of the functions defined in the C or C++ file.  Finally,  you should include this header file at the begining of the C or C++ file.  Consequently,  in our example above we would have: 

// test.cpp
    #include "test.h"
    int foo() {   
      return test();  
    int test() {      
      return 5;  
// test.h
    int foo();
    int test();

You have probably noticed that there are two kinds of #include statement.  In the example above,  we include the header file test.h by specifying the path to the header file within quotation marks.  In this case,  you must include a complete file name with the .h file extension.  Header files such as iostream that correspond to system libraries are included into your source code within angle brackets and without a file extension:

#include <iostream>	// Defines cin, cout, <<, >>
using namespace std;	// Assumes std:: prefix -- simplifies std::cin to cin

    cout << "Hello World!";
    return 0;

Writing a Makefile

Makefiles automate building executable programs in unix.  Makefiles are always called either makefile or Makefile.  While there are several utilities available to automatically create makefiles,  none of the projects in this course are large enough to merit their use.  Begin by reading Automating Program Compilation - Writing Makefiles by Guy Keren.  Base your makefile on the Multi-Source Example.  In UNIX, makefiles are executed by typing make at the command line prompt.  This will use the file called makefile in the current unix directory to automatically sequence unix commands.  In UNIX,  the names of executable files of any sort do not have file extensions.  Although written in a scripting language and not directly executable,  makefiles also do not have file extensions. 

# top-level to compile the whole program.
all: test

# test is made of several source files.
# the first line states that the gcc command depends on compilation of main and metric.
# the second line links main.o and metric.o to produce an executable file called test.
test: driver.o metric.o
          gcc driver.o metric.o -o test
# the first line states that the gcc command depends upon main.c and metric.h
# the second line compiles main.c to produce main.o
# main.c "includes" metric.h
driver.o: driver.c metric.h
        gcc -g -Wall -c driver.c
# the first line states that the gcc command depends upon metric.c and metric.h
# the second line compiles metric.c to produce metric.o
# metric.c "includes" metric.h
metric.o: metric.c metric.h
          gcc - g - Wall -c metric.c
# rule for deleting temporary files
          /bin/rm -f *.o

Building Your Program

In JGRASP,  selecting either C or C++ enables two new buttons.  The plus button compiles a C or C++ source file,  but does not link the program to produce an executable file.  The double plus button both compiles and links a C or C++ source file and produces an executable file.  However,  most large C and C++ programs occupy several files which must be separately compiled and then linked together to produce a single executable file.  This process is controlled by a makefile.  The hammer button in JGRASP processes a makefile to build a C or C++ program.  From the UNIX command line,  you simply type make which reads and processes the makefile in the current directory. 

Don't forget to clean up your directory when you are all finished. 

Prev | Next | Lab 4




Lab 0

Lab 1

Lab 2

Lab 3

Lab 4

Lab 5

Lab 6

Lab 7-8

Lab 9

Lab 10

Lab 11

Lab 12

Lab 13

Last modified: 2007 OCT 28