CIS 3142 Advice — Chapter 15

CISC 3142
Programming Paradigms in C++
Part II: Basic Facilities
Chapter 15 - Source Files and Programs

Overview

Source files and program organization

Topics

Here is a writeup on separate compilation

Separate Compilation

Linkage

Header Files
Header files are used to provide the compiler about program entities contained within other translation units. For the purposes of the follwong discussion, assume we have a module (m.h and m.cpp) of C-style (non-member) functions, a class C (in c.h/c.cpp), and an app (in app.cpp), and we're compiling app.cpp

What can go in a header file:

All of the above either are entities that may be needed during the compilation of a source file wishing to use the facilities of the translation unit corresponding to the header file.

What shouldn't go into a header file:

Source files (i.e., .cpp) should never be included
The One-Definition Rule (ODR)
There can be at most one defintion of a class, template, etc in a program

Phrased slightly differenly:

Two definitions of a class, template, or inline function are accepted as examples of the same unique definition if and only if:

For practical purposes, this means that the above definitions should go into a header file which can then be included by each translation unit (i.e., file) and thus is guaranteed to be identical across all such files.
Standard Library Headers

Using Header Files

Include Guards
Meant to prevent multiple inclusion of same header within a translation unit, which may lead to duplicate definition errors
// node.h

class Node {
	…
};
// list.h

#include "node.h"

class List {
public:
	…
	void append(int val);
	…
private:
	Node *head;
};
// list.cpp

#include "node.h"
#include "list.h"		// node.h is being brought into list.cpp a second time

…

void List::append(int val) {…}

…
Two basic ways of solving this problem:

Programs

Program Termination
A program terminates by:

Advice

Code Used in this Lecture