C++ CPE cheat sheet
C++ CPE · Keep the key ideas within reach.
C++ Essentials 1 (CP101 / CPPE1): Core Language and Program Structure
Translation pipeline: preprocessor (text substitution: #include, #define, conditional compilation) → compiler (each source file to an object file) → linker (combines objects and libraries into an executable); a missing definition is a link error, a syntax or type error is a compile error.
Program entry: int main() is the required entry point; returning 0 (or falling off the end of main) signals success. std::cout/std::cin require ; std::endl flushes the stream, '\n' does not.
Fundamental types and sizes (typical): bool 1, char 1, short ≥2, int ≥2 (usually 4), long ≥4, long long ≥8, float 4, double 8, long double ≥8. sizeof yields std::size_t; signed integer overflow is undefined behavior.
Integer division truncates toward zero: 7/2 == 3, -7/2 == -3. The % operator requires integral operands and takes the sign of the left operand: -7 % 2 == -1. Mixing int and double promotes to double.
Variables: declaration vs definition; const makes an object read-only and must be initialized; constexpr requires a compile-time constant initializer. Uninitialized local variables of built-in type hold indeterminate values - reading them is undefined behavior.
References vs pointers: a reference is an alias, must be bound at initialization, cannot be reseated, and has no null state; a pointer holds an address, can be nullptr, reassigned, and dereferenced with * / addressed with &.
Namespaces: std:: qualifies standard names; using namespace std; injects all names into scope (avoid in headers). Scope resolution operator :: accesses a namespace or global name shadowed by a local.
Control flow: if/else, switch (integral or enum controlling expression, case labels must be constant, break exits, fall-through is default), while, do-while (body runs at least once), for, and range-for over a sequence.
Functions: pass-by-value copies, pass-by-reference (&) aliases, pass-by-const-reference (const &) avoids copies while forbidding modification; default arguments appear only in the declaration; overloading resolves by parameter list, not return type.
C++ Essentials 2 (CPP102 / CPPE2): Classes, Memory, and the Standard Library
Class anatomy: access specifiers public/protected/private (default private for class, public for struct); constructors share the class name and have no return type; a member initializer list initializes members before the constructor body runs and is required for const members and references.
Object lifecycle: construction order follows declaration order of members, not the initializer-list order; destruction is reverse of construction. A destructor (~T()) releases resources; virtual destructors are required when deleting derived objects through a base pointer.
Copy vs move: the compiler-generated copy constructor and copy assignment perform memberwise copies; declaring a destructor suppresses implicit moves. The rule of three/five: if you define one of destructor, copy ctor, copy assignment (plus move ctor, move assignment), define them all.
Inheritance and polymorphism: derived-to-base conversion is implicit; virtual functions dispatch through a base pointer/reference at run time; override (C++11) catches signature mismatches; a pure virtual (= 0) makes the class abstract and non-instantiable.
Dynamic memory: new allocates and constructs, delete destroys and frees; new[] pairs with delete[]. Mismatched forms are undefined behavior. Prefer std::unique_ptr (sole ownership, movable) and std::shared_ptr (reference-counted) from ; std::make_unique/make_shared avoid raw new.
Operator overloading: member operator functions take one fewer parameter (the implicit *this); free functions allow conversion on the left operand; stream insertion is a free function returning std::ostream&. Assignment operators should return *this and guard against self-assignment.
STL containers: std::vector (contiguous, amortized O(1) push_back, invalidation on reallocation), std::list (O(1) splice, no random access), std::map (ordered, O(log n)), std::unordered_map (hash, average O(1)), std::array (fixed size, no heap).
Iterators and algorithms: begin()/end() define a half-open range [begin, end); provides std::sort, std::find, std::count, std::for_each, std::transform; erase on a sequence container returns the next valid iterator, and erase-remove idiom removes matching elements.
Exceptions: try/catch/throw; throw by value, catch by const reference; stack unwinding runs destructors of fully constructed locals; a noexcept function that throws calls std::terminate; catch(...) catches everything. Exceptions crossing a destructor during unwinding terminate the program.