C++ OOP Quick Reference¶
The whole language surface this course uses, on one page. If you remember only one thing from this sheet, remember the build command: it is the one that catches the rest for you.
Pairs with Module 1 — C++ and the OOP mindset
Build and run¶
g++ -std=c++17 -Wall -Wextra -Werror file.cpp -o file # one file
./file # Windows: file.exe
g++ -std=c++17 -Wall -Wextra -Werror a.cpp b.cpp -o prog # several files, one program
| Flag | What it does |
|---|---|
-std=c++17 |
The language version this course teaches. Without it you get the compiler's default, which may be older |
-Wall -Wextra |
The warnings that matter |
-Werror |
Promotes every warning to an error, so a warning cannot be ignored |
-c |
Compile only, no linking. Warnings live here |
-o name |
Name the output |
The stages are separate, and the error tells you which one failed. Compile errors name a file and a line; link errors name no line, because no single file is wrong — see Common Compiler Errors.
A class, in full¶
#pragma once // in a header: paste-once guard
#include <string>
class Account
{
public:
explicit Account(std::string owner, int opening); // constructors
~Account(); // destructor
Account(const Account& other); // copy
Account& operator=(const Account& other); // copy assign
Account(Account&& other) noexcept; // move
Account& operator=(Account&& other) noexcept; // move assign
int balance() const { return balance_; } // query: const
void deposit(int amount); // command: not const
std::string owner() const { return owner_; }
private:
std::string owner_;
int balance_ = 0; // default member initialiser
};
Rules that carry most of the marks
- Data members are private; behaviour is public.
- Every member that does not modify the object is
const. - Every invariant is established by a constructor and preserved by the public interface.
- The five special members are written only when the class owns a resource directly. Otherwise do not mention them at all — that is the Rule of Zero.
The keywords that change behaviour¶
| Keyword | Meaning | Where it goes |
|---|---|---|
virtual |
Dispatch at run time from the object's real type | Base-class function declaration only |
override |
"I intend to override — verify it" | Derived-class declaration, after the parameter list |
final |
No further overriding (function) or no deriving (class) | Same position as override |
const |
Does not modify the object; callable on a const object |
After the parameter list, in declarations and definitions |
explicit |
Blocks implicit conversion by a one-argument constructor | Before the constructor |
static |
One copy per class, not per object; no this |
Member declaration |
noexcept |
Promises not to throw. Breaking the promise calls std::terminate |
After the parameter list |
= default |
Use the compiler's version | On a special member |
= delete |
Forbid it; using it becomes a compile error | On a special member |
mutable |
This member may change in a const function |
Before the member type |
friend |
Grants one named function or class access to private members | Inside the class |
virtual, override and final are not used in the out-of-class definition:
void Account::deposit(int amount) // no "virtual" here
{
if (amount <= 0) { throw std::invalid_argument("deposit must be positive"); }
balance_ += amount;
}
Relationships between classes¶
| Relationship | Meaning in one line | In code |
|---|---|---|
| Dependency | Uses it for one operation, does not keep it | A parameter or a local variable |
| Association | Knows about it and keeps a reference | A pointer or reference member |
| Aggregation | Has it, but does not own it — it can outlive the whole | A non-owning pointer/reference, or weak_ptr |
| Composition | Has it and owns it — it dies with the whole | A value member, or unique_ptr |
| Inheritance | Is a it — substitute anywhere the base is promised | class Derived : public Base |
If you cannot say which of these five it is, you do not yet know who destroys what — and that is the design decision, not the diagram.
The three questions for every class¶
- What is its one responsibility? One sentence, no "and".
- What invariant does it protect, and where? If the answer is "nowhere in particular", it has none, and any state is legal — which is usually a bug waiting to be found.
- What does it own? Values and
unique_ptrs own; raw pointers and references observe.
Terms you will meet in error messages¶
| In a message | Means |
|---|---|
this argument discards qualifiers |
You called a non-const member on a const object |
is private within this context |
You reached a private member from outside |
was hidden |
A derived function with the same name hides the base's — often a missing virtual or const |
does not override |
Name, parameters or const do not match the base function exactly |
cannot declare variable ... to be of abstract type |
You tried to create an object with a pure virtual function |
no declaration matches |
The out-of-class definition's signature differs from the declaration |
ld returned ... exit status |
A link error: a symbol is missing or duplicated. No file or line, by design |
Related: Class Design · Constructors and Destructors · Common Compiler Errors · Glossary