C++ Program Structure
A C++ program is a collection of variable definitions, function definitions, and function calls. When it starts, it executes initialization code and calls main().
Declarations vs. Definitions
This distinction is fundamental and must be understood precisely:
| Declaration | Definition | |
|---|---|---|
| What it says | ”This thing exists somewhere" | "Make it here” |
| Allocates storage? | No | Yes |
| Can appear many times? | Yes | No (ODR) |
| Keyword | extern (for variables) | (no keyword needed) |
One-Definition Rule (ODR): each function or variable may be defined exactly once across all translation units in a program. The linker complains if it finds more than one.
A definition is always also a declaration. If the compiler hasn’t seen a name before and you write int x;, it treats this as both declaration and definition (allocates storage).
Function Declarations (Prototypes)
int func1(int, int); // declaration — semicolon, no body
int func1(int length, int width); // same declaration, names optional but helpfulForm: return_type name(arg_type, arg_type);
C++ gotcha: int func2() means “takes NO arguments.” In C it meant “takes ANY arguments.” This is a type-safety improvement in C++.
Function Definitions
int func1(int length, int width) { // definition — body in braces
return length * width;
}- Braces replace the semicolon
- Argument names are required in definitions if you use the arguments
Variable Declarations
int a; // declaration AND definition (allocates an int)
extern int a; // declaration only (says "a is defined elsewhere")
extern int func1(int length, int width); // extern is optional for functionsHeader Files
A header file contains external declarations for a library. Convention: .h extension (e.g. headerfile.h).
Two forms of #include:
#include <iostream> // angle brackets: search the implementation's include path
#include "local.h" // double quotes: search current directory first, then include pathStandard C++ Include Format
No .h extension — this gives the templatized version:
#include <iostream> // Standard C++ (preferred)
#include <string>
#include <fstream>
#include <vector>C library headers: prepend c, drop .h:
#include <cstdlib> // was <stdlib.h>
#include <cstdio> // was <stdio.h>Important: don’t mix .h and non-.h forms for the same header in one program — they are different (template vs. non-template) versions.
Namespaces
Large programs break identifier names into separate namespaces to avoid collision. All Standard C++ library declarations live in the std namespace.
using namespace std; // expose all names from std for this fileusing namespace std;in a source file: fineusing namespace std;in a header file: dangerous (forces it on every file that includes the header)- Old
#include <iostream.h>implicitly includedusing namespace std;; that’s why the.hform still “works”
For now: put using namespace std; at the top of every .cpp file.
Program Structure
#include <iostream>
using namespace std;
int main() {
// primary code goes here
return 0; // 0 = success; non-zero = error code
}main()always returnsintin C++ (unlike C where void was permitted)- Free-form language: compiler ignores whitespace and newlines; semicolon ends each statement
- A statement can span multiple lines
Comments
/* C-style: can span
multiple lines */
// C++ style: single line only — preferred in this bookUse // for one-liners; /* */ when you need multi-line comment blocks.
flowchart LR A[Declaration] -->|introduces name| B[Compiler knows it exists] C[Definition] -->|allocates storage| D[Linker can resolve references] C --> A E[Header file] -->|#include pastes declarations| B F[using namespace std] -->|exposes std names| G[Can use cout, string, vector without std::]
Key Points
- Every name used in C++ must be declared before use — the compiler cannot assume anything
- ODR: declare as many times as needed; define exactly once
externis required to declare a variable without defining it;externis optional (but superfluous) for function declarations- Argument names in declarations are ignored by the compiler but help human readers
main()= mandatory entry point; alwaysint main()- Standard C++ headers: no
.h; C-compatibility headers:cprefix, no.h