Chapter 02

The Anatomy of a C++ Program

Preprocessor directives, namespaces, functions, and standard I/O with iostream.

In this chapter

  1. Preprocessor Directives
  2. Namespaces
  3. Functions
  4. IOStream
1

Preprocessor Directives (#)

Lines beginning with # are Preprocessor Directives — they run before the code compiles.

The most common directive is #include, which inserts the contents of another file at that line.

// Angle brackets = standard library
#include <iostream>

// Quotes = custom/local headers
#include "Editor.h"

2

Namespaces

Names given to sections of code to avoid naming conflicts.


3

Functions


4

IOStream

To read an entire line including spaces, use getline():

string fullName;
getline(cin, fullName); // reads until newline, not just first word
← Chapter 1 ↑ Index Chapter 3 →