Skip to content

C++ Basics

Glossary

143 terms from C++ Basics, in plain words, each with the chapter that teaches it.

#

#include
A preprocessor line that pastes the named file in, word for word. It brings in headers such as `<iostream>`.
#pragma once
A line at the top of a header meaning "include this file only once per source file". Without it, including the header twice causes a redefinition error.

A

Address
The number that says where a value lives in memory. `&qty` gives the address of the variable `qty`.
Address sanitizer
A compiler option (`-g -fsanitize=address,undefined`) that makes a program stop at a bad memory access or undefined behavior and print a report naming the line.
argc and argv
The two parameters that let `main` receive command-line arguments. `argc` counts the words typed, including the program's name, and `argv` holds them. In `./orders menu.txt`, `argv[1]` is `menu.txt`.
Argument
The actual value you pass to a function when you call it. In `money(475)`, 475 is the argument.
Arrow operator
The `->` operator reaches a member through a pointer: `p->name` means `(*p).name`. For a map iterator, `it->first` is the key and `it->second` the value.
Assignment
Replacing a variable's current value with a new one, as in `bags = 4;`. The variable's type never changes.
auto
A keyword that asks the compiler to work out a variable's type from its initial value. `auto total = 3 * 1600;` makes `total` an `int`.

B

Block
A group of statements between `{` and `}`. Everything inside the braces of `main` is one block.
bool
A type that holds either `true` or `false`. A comparison such as `bags >= 5` gives a `bool`.
Brace initialization
Giving a variable its first value in braces, as in `int qty{2};`. It refuses values that would be narrowed, such as putting 4.5 in an `int`.
break
A keyword that jumps out of a loop or a `switch` at once.
Build directory
The folder (named with `-B build`) where CMake puts everything it generates, so your source files stay clean. You can delete it to start over.
Build system
A tool that keeps the list of files in a project and rebuilds only the parts that changed. CMake is a common free build system for C++.

C

C++
A compiled programming language, created in the 1980s, used for fast programs such as games and browsers. It has classes and a large standard library.
Case-sensitive
Treating uppercase and lowercase letters as different. In C++, `Cout` is not the same name as `cout`.
Cast
An explicit conversion of a value to another type. `static_cast<double>(cups)` gives `cups` as a `double` and leaves `cups` itself alone.
catch
The block after a `try` that handles one kind of thrown exception. It runs only if a matching exception is thrown inside the `try`.
char
A type that holds a single character, written in single quotes such as `'L'`.
Clang
A free C++ compiler, whose C++ command is `clang++`. On a Mac, `g++` also runs Clang.
Class
A type you define that bundles data with the functions that work on it, and can hide that data. `Stock` is a class that keeps a bag count valid.
CMake
The usual free build system for C++. It reads `CMakeLists.txt` in your project folder and builds the program with two commands.
Command
An instruction you type into a terminal instead of clicking, such as `cd` or `g++`.
Command-line arguments
The words typed after a program's name when you run it, such as `menu.txt` in `./build/orders menu.txt`. A program reads them through `argc` and `argv`.
Comment
Text for people that the compiler ignores. `//` runs to the end of the line and `/* */` can span lines.
Comparison operators
Operators that compare two values and give a `bool`: `==`, `!=`, `<`, `>`, `<=`, `>=`. Remember `==` tests equality, while one `=` assigns.
Compiler
A program that translates source code into machine code and saves it as an executable. The two main free C++ compilers are GCC and Clang.
Compound assignment
Short forms that change a variable using its own value: `cups += 5` means `cups = cups + 5`, and `-=` and `*=` work alike.
Condition
A `bool` expression in parentheses that decides whether an `if` block runs or a loop repeats, such as `stock == 0`.
const
A keyword that makes a variable unchangeable after it is initialized. It is also used for read-only references and member functions.
const member function
A member function marked `const` that promises not to change the object, as in `int count() const`. Only these can be called on a `const` object.
const reference
A reference that passes the original value without copying it and promises not to change it, such as `const std::string& name`.
constexpr
Like `const`, but the value must be known when the program is compiled. It suits fixed prices such as `bag_cents`.
Constructor
A member function with the class's name and no return type that runs when an object is made, to set it up safely. It can throw to stop a bad object from existing.
Container
An object that holds many values, such as `std::vector`, `std::array` or `std::map`.
continue
A keyword that skips the rest of the current pass of a loop and goes on to the next one.

D

Dangling
A reference or pointer to something that no longer exists, such as a local variable after its function has returned. Using it is a bug.
Declaration
A statement that introduces a name and its type, such as `int bags = 3;`. For a function, the first line ending in a semicolon instead of a body is its declaration, also called a prototype.
Default argument
A value a parameter takes when the call leaves it out, as in `int qty = 1`. Defaults go at the end of the parameter list.
Default constructor
A constructor that takes no arguments, used for `Stock unknown;`. A constructor whose parameters all have defaults also counts.
Default member value
A starting value written next to a member, such as `int count_{0};`. It is used when a constructor doesn't set that member.
Dereference
Reaching the value a pointer points to by writing `*` in front of it. `*p = 5;` writes 5 into the variable `p` points at.
Destructor
A member function named `~` plus the class name that runs automatically when an object ends. It is where an object gives back what it holds.
do ... while
A loop that checks its condition after the block, so the block runs at least once. It suits asking until an answer is valid.
double
A type for numbers with a decimal point. It holds most decimals only approximately, so it is not used for money.

E

Encapsulation
Hiding a class's data with `private` so only its own functions can change it. That way `Stock` can never hold a count of -4.
Endless loop
A loop whose condition never becomes false, so it runs forever. Press Ctrl+C in the terminal to stop it.
Error
Code the compiler can't understand. You get no executable until you fix it.
Escape sequence
A backslash code in a string for something you can't type directly, such as `\n` for a new line.
Exception
An object thrown to report an error to the caller, which can catch it with `catch`. If nothing catches it, the program ends.
Executable
The file a compiler produces, which you run like any other app. You compile once and run it as often as you like.
Exit status
The number a program hands back to the system when it ends: 0 means it worked and anything else means something went wrong. `echo $?` shows it.
explicit
A keyword on a one-argument constructor that stops C++ from silently converting a value into that class. With `explicit Money(int)`, you must write `Money{5}`, not a bare 5.
Extension
An add-on for VS Code. The C/C++ extension from Microsoft colors your code and underlines mistakes.

F

Fall-through
What happens in a `switch` when a case has no `break`: the program carries on into the next case.
Flag
An option for a command, usually starting with a dash, such as `-o hello` or `-Wall`.
for loop
A loop that puts counting in one line: a start (run once), a condition (checked before each pass) and a step (run after each pass).
Function
A named group of instructions with parameters and a return type, which you write once and call anywhere. Every program starts in the function `main`.
Function call
Using a function by writing its name and arguments, such as `money(475)`. The body runs and the call is replaced by the returned value.
Function overloading
Giving several functions the same name when their parameters differ in number or type. The compiler picks the version that matches the arguments.

G

g++
The C++ command of the free GCC compiler collection, which translates C++ source files into an executable program. It takes flags such as `-std=c++20` and `-Wall`, and `-o` names the output file.
Global variable
A variable declared outside every function, so any code in the program can read and change it. Because that makes it hard to tell what changed a value, globals tend to hide bugs and are best avoided.

H

Header file
A file of ready-made code or declarations that you `#include`, such as `<iostream>` or your own `money.h`. Your own headers are included with quotes, library ones with angle brackets.

I

if / else if / else
Keywords that run a block only when a condition is true. `else if` tests another condition, and `else` catches the rest.
Increment and decrement
`++` adds 1 to a variable and `--` takes 1 away, as in `cups++;`.
Index
A position number in a string or container, starting at 0. `s[0]` is the first character.
Initialize
Giving a variable its first value as you declare it. Assignment later replaces that value.
int
The type for whole numbers, 4 bytes on current computers, holding up to about 2.1 billion either side of zero. `long long` is the 8-byte version for bigger amounts.
Integer division
Dividing one `int` by another, which throws away the remainder, so 7 / 2 gives 3. `%` gives the remainder.
Iterator
A position in a container. `.begin()` is the first position and `.end()` is just past the last, which `find` returns when it finds nothing.

K

Key and value
A `std::map` stores pairs: you look up a value by its key, such as a menu code giving a price. Each key appears once.

L

Linker
The build stage that joins the compiled pieces and the standard library into one executable. `undefined reference to` is a linker error.
Local variable
A variable declared inside a function, parameters included, that exists only while the function runs.
Logical operators
Operators that combine `bool` values: `&&` means "and", `||` "or" and `!` "not".
Loop
Code that repeats a block, such as `while`, `for` or `do ... while`.

M

Machine code
The numbered instructions, such as "add these two numbers", that a processor understands directly. A compiler produces it from source code.
main
The function where every program starts. It returns an `int` exit status, 0 for success.
Member
A variable inside a struct or class, reached with a dot such as `latte.name`.
Member function
A function that belongs to a class or value and is called after a dot, such as `order.size()` or `decaf.add(8)`.
Member initializer list
The part of a constructor after a colon, such as `: name_{name}, count_{count}`, that gives each member its first value before the body runs.
Memory leak
Memory taken with `new` and never given back with `delete`.
MSYS2
A Windows toolkit, installed from msys2.org, that provides a Unix-like terminal and package manager along with tools such as `g++` and CMake. It is a common way to get a C++ compiler on Windows.

N

Namespace
A named group of names that keeps the library's names apart from yours. `std::cout` means `cout` from the namespace `std`.
Narrowing
A conversion that loses information, such as putting 4.5 into an `int`. Brace initialization refuses it with an error.
new and delete
Older C++ keywords: `new` makes a value that lives until you `delete` it. Forgetting `delete` causes a memory leak, and modern C++ avoids both.
Ninja
A small, fast build tool that CMake can use to run the compiler and linker. CMake generates the build files and Ninja carries out the compile steps.
Non-member function
A function written outside a class. `operator<<` for printing must be one, because its left side is `std::cout`, not your object.
nullptr
The pointer value meaning "no address", pointing at nothing. Dereferencing it crashes the program, so check for it first.

O

Object
One value of a class type. `Stock decaf;` and `Stock espresso;` are two objects, each with its own data.
Operator overloading
Defining what an operator such as `+` or `<<` does for your own class by writing a function named `operator+` and so on. Do it only when the meaning is obvious.
Overflow
A result too big for its type. For a signed `int` it is undefined behavior.

P

Parameter
A function's variable that receives a value when the function is called. In `money(int cents)`, `cents` is the parameter.
Pass by value
Giving a function a copy of the argument, so changing the parameter leaves the caller's variable alone.
Pointer
A variable that holds an address, written `int* p`. Use one instead of a reference when "nothing" is a possible answer or it must point at different things over time.
Preprocessor
The first build stage. It handles lines starting with `#`, such as `#include`, which it pastes in.
private and public
Access labels in a class. `public:` members can be used by any code, while `private:` members only by the class's own member functions.
Program
A list of instructions for a computer, written as plain text called source code.

R

RAII
The idea that an object takes a resource, such as an open file, in its constructor and gives it back in its destructor. File streams, strings and vectors work this way, so they clean up even during an error.
Range-based for
A loop that runs its body once for each element of a container, as in `for (int c : prices)`, with no index to get wrong.
Reference
Another name for an existing variable, written `int& r = x;`. As a parameter it is the caller's own variable, so changes reach the caller.
Return type
The type of value a function gives back, written before its name. `return` hands the result back to the caller.

S

Scope
The region where a name can be used. A variable declared inside a block exists only until the block's closing brace.
Scope resolution operator
The `::` symbol that says which scope a name belongs to, as in `Stock::sell` or `std::cout`.
sizeof
An operator that gives a type's size in bytes. `sizeof(int)` is 4 on current computers.
Source code
The plain text of a program, written by a person in a language like C++. A compiler turns it into machine code.
Source file
A `.cpp` file that holds the definitions, meaning the bodies of functions declared in a header.
Stack unwinding
The destroying of a block's objects, in reverse order, as an exception leaves it. This is why RAII objects such as files still close during errors.
Standard library
The large set of ready-made code that comes with every C++ compiler, with names under `std::`.
Statement
A single instruction in C++, which ends with a semicolon.
std::array
A container that holds a fixed number of values, with the size written after the type, as in `std::array<int, 5>`. It has no `push_back`.
std::cerr
A second screen output kept for error messages, separate from `std::cout`.
std::cin
The keyboard input stream. `std::cin >> qty` reads one word or number, and it counts as true when the read succeeded.
std::cout
The screen output stream. `<<` sends whatever is on its right to it.
std::exception
The standard exception classes from `<stdexcept>`: `invalid_argument` for a nonsensical value, `out_of_range` for a value outside its range, and `runtime_error` for a failure while running. All are kinds of `std::exception`, and `.what()` gives the message.
std::find
A function from `<algorithm>` that returns the position of a value in a range, or the end if it's missing. Strings and maps also have their own `find`.
std::format
A function from `<format>` that builds a string from a pattern. Each `{}` placeholder takes the next value, and codes after a colon set width, alignment, zero padding and decimals.
std::getline
A function that reads a whole line, spaces included, into a string.
std::ifstream and std::ofstream
File streams from `<fstream>`: `std::ifstream` reads a file and `std::ofstream` writes one. They close the file by themselves when they go out of scope.
std::map
A container of key and value pairs found by key, such as menu codes and prices. It keeps keys sorted, and `std::unordered_map` is similar but unsorted.
std::ostream
The type of all output streams, including `std::cout`, `std::cerr` and every `std::ofstream`. A function taking one can print to the screen or a file.
std::sort
A function from `<algorithm>` that puts a range in order, smallest first, using `<`.
std::stoi and std::stod
Functions that convert text to an `int` or `double`, with `std::to_string` going the other way. `std::stoi("twelve")` throws an exception.
std::string
The type that holds text in a variable, from `<string>`. `find` returns `std::string::npos` ("no position") when text isn't found.
std::vector
The most-used container: values of one type in order, in a list that grows with `push_back`. `[i]` doesn't check the index, but `.at(i)` does.
std::ws
A helper that skips whitespace such as the leftover newline after `>>`. Use `std::cin >> std::ws` whenever `getline` follows `>>`.
String literal
Fixed text in double quotes in the source code, such as `"Latte"`.
struct
A way to make a new type that groups several members, such as `Item` with a code, name and price. Its members are public by default, unlike a class.
Structured binding
Naming the parts of a pair at once, such as `[code, cents]` in a loop over a map.
switch
A statement that picks a block by matching a whole number or `char` against fixed `case` values, with `default` for when nothing matches.

T

Template
Code with a type filled in later, written in angle brackets, as in `std::vector<int>`.
Terminal
A window where you type commands instead of clicking. You use it to compile and run programs.
this
Inside a member function, a pointer to the object the function was called on. `*this` is the object itself, which `operator+=` returns.
throw
The keyword that raises an exception, ending the current function and jumping to the first matching `catch`.
try
A block whose thrown exceptions can be handled by the `catch` blocks that follow it.
Type
The kind of value a variable holds, such as `int` or `std::string`.

U

Undefined behavior
Code the language gives no rules for, so anything can happen. Examples are overflowing an `int` or going past the end of a vector with `[]`.

V

Variable
A named place that holds a value of a particular type.
void
The return type of a function that gives nothing back, such as `print_rule`.
VS Code
Visual Studio Code, a free code editor from Microsoft. With its C/C++ extension it colors code and underlines mistakes, which makes it handy for writing C++ programs.

W

Warning
A compiler message about code that is legal but probably a mistake. You still get an executable, but you should fix it. `-Wall -Wextra` turn warnings on.
while loop
A loop that runs its block for as long as its condition is true.
Whole cents
Storing money as a whole number of cents in an `int` so sums are exact, then printing with `/ 100` and `% 100`. A `double` holds most decimals only approximately.