The University of Iowa
The College of Liberal Arts and Sciences
Department of Computer Science

Programming Languages and Tools:

CS:3210:0001

Lecture/Lab #22

Programming with C++

Modularity: separate compilation

Warm up

  • What's a common name for all of the following constructs?

#include, #define / #undef, #ifdef / #endif, #error

  • When is the preprocessor called?

  • What's the result of the preprocessing step?

Separate compilation

source-1.cpp

source-2.cpp

Object file 1

Object file 2

Executable

Compilation

(includes preprocessing)

Linking

source-3.cpp

Object file 3

Ability to organize a program into a set of independent physical "modules"

Separate compilation (cont.)

point.cpp

point.hpp

point interface

point implementation

#include <point.hpp>

chess_piece.cpp

use point

#include <point.hpp>

Separate compilation (cont.)

  • Headers can include other headers.

  • Headers allows us to ensure that the entity definitions are the same in each file.

  • Because a header might be included more than once, we need to write headers in a way that is safe even if the header is included multiple times.

  • A .cpp file with all the header files included is called a translation unit.

Exercise 1

Factor out `car` base class into a separate .cpp file

  • Open the exercise template

  • Write your code, press Run to test

  • When you're done, grab your Repl's link and send it as a direct message to me (agurtovoy)

  • Click on the corresponding option in the "Lab22 exercises" poll in #general

Programming with C++, Fall 2019, Lecture #22

By Aleksey Gurtovoy

Programming with C++, Fall 2019, Lecture #22

Modularity: separate compilation

  • 665