\documentclass[10pt,a4paper]{article} % Packages \usepackage{fancyhdr} % For header and footer \usepackage{multicol} % Allows multicols in tables \usepackage{tabularx} % Intelligent column widths \usepackage{tabulary} % Used in header and footer \usepackage{hhline} % Border under tables \usepackage{graphicx} % For images \usepackage{xcolor} % For hex colours %\usepackage[utf8x]{inputenc} % For unicode character support \usepackage[T1]{fontenc} % Without this we get weird character replacements \usepackage{colortbl} % For coloured tables \usepackage{setspace} % For line height \usepackage{lastpage} % Needed for total page number \usepackage{seqsplit} % Splits long words. %\usepackage{opensans} % Can't make this work so far. Shame. Would be lovely. \usepackage[normalem]{ulem} % For underlining links % Most of the following are not required for the majority % of cheat sheets but are needed for some symbol support. \usepackage{amsmath} % Symbols \usepackage{MnSymbol} % Symbols \usepackage{wasysym} % Symbols %\usepackage[english,german,french,spanish,italian]{babel} % Languages % Document Info \author{blakecromar} \pdfinfo{ /Title (c.pdf) /Creator (Cheatography) /Author (blakecromar) /Subject (C++ Cheat Sheet) } % Lengths and widths \addtolength{\textwidth}{6cm} \addtolength{\textheight}{-1cm} \addtolength{\hoffset}{-3cm} \addtolength{\voffset}{-2cm} \setlength{\tabcolsep}{0.2cm} % Space between columns \setlength{\headsep}{-12pt} % Reduce space between header and content \setlength{\headheight}{85pt} % If less, LaTeX automatically increases it \renewcommand{\footrulewidth}{0pt} % Remove footer line \renewcommand{\headrulewidth}{0pt} % Remove header line \renewcommand{\seqinsert}{\ifmmode\allowbreak\else\-\fi} % Hyphens in seqsplit % This two commands together give roughly % the right line height in the tables \renewcommand{\arraystretch}{1.3} \onehalfspacing % Commands \newcommand{\SetRowColor}[1]{\noalign{\gdef\RowColorName{#1}}\rowcolor{\RowColorName}} % Shortcut for row colour \newcommand{\mymulticolumn}[3]{\multicolumn{#1}{>{\columncolor{\RowColorName}}#2}{#3}} % For coloured multi-cols \newcolumntype{x}[1]{>{\raggedright}p{#1}} % New column types for ragged-right paragraph columns \newcommand{\tn}{\tabularnewline} % Required as custom column type in use % Font and Colours \definecolor{HeadBackground}{HTML}{333333} \definecolor{FootBackground}{HTML}{666666} \definecolor{TextColor}{HTML}{333333} \definecolor{DarkBackground}{HTML}{329C91} \definecolor{LightBackground}{HTML}{F2F8F8} \renewcommand{\familydefault}{\sfdefault} \color{TextColor} % Header and Footer \pagestyle{fancy} \fancyhead{} % Set header to blank \fancyfoot{} % Set footer to blank \fancyhead[L]{ \noindent \begin{multicols}{3} \begin{tabulary}{5.8cm}{C} \SetRowColor{DarkBackground} \vspace{-7pt} {\parbox{\dimexpr\textwidth-2\fboxsep\relax}{\noindent \hspace*{-6pt}\includegraphics[width=5.8cm]{/web/www.cheatography.com/public/images/cheatography_logo.pdf}} } \end{tabulary} \columnbreak \begin{tabulary}{11cm}{L} \vspace{-2pt}\large{\bf{\textcolor{DarkBackground}{\textrm{C++ Cheat Sheet}}}} \\ \normalsize{by \textcolor{DarkBackground}{blakecromar} via \textcolor{DarkBackground}{\uline{cheatography.com/218070/cs/48101/}}} \end{tabulary} \end{multicols}} \fancyfoot[L]{ \footnotesize \noindent \begin{multicols}{3} \begin{tabulary}{5.8cm}{LL} \SetRowColor{FootBackground} \mymulticolumn{2}{p{5.377cm}}{\bf\textcolor{white}{Cheatographer}} \\ \vspace{-2pt}blakecromar \\ \uline{cheatography.com/blakecromar} \\ \end{tabulary} \vfill \columnbreak \begin{tabulary}{5.8cm}{L} \SetRowColor{FootBackground} \mymulticolumn{1}{p{5.377cm}}{\bf\textcolor{white}{Cheat Sheet}} \\ \vspace{-2pt}Not Yet Published.\\ Updated 24th June, 2026.\\ Page {\thepage} of \pageref{LastPage}. \end{tabulary} \vfill \columnbreak \begin{tabulary}{5.8cm}{L} \SetRowColor{FootBackground} \mymulticolumn{1}{p{5.377cm}}{\bf\textcolor{white}{Sponsor}} \\ \SetRowColor{white} \vspace{-5pt} %\includegraphics[width=48px,height=48px]{dave.jpeg} Measure your website readability!\\ www.readability-score.com \end{tabulary} \end{multicols}} \begin{document} \raggedright \raggedcolumns % Set font size to small. Switch to any value % from this page to resize cheat sheet text: % www.emerson.emory.edu/services/latex/latex_169.html \footnotesize % Small font. \begin{tabularx}{17.67cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{17.67cm}}{\bf\textcolor{white}{The -\textgreater{} operator}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{17.67cm}}{Dog{\emph{ d = new Dog("Rex"); \newline \newline d-\textgreater{}bark(); // shorthand \newline (}}d).bark(); // same thing} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \SetRowColor{LightBackground} \mymulticolumn{1}{x{17.67cm}}{-\textgreater{} operator — shorthand for dereferencing a pointer and accessing a member. Equivalent to (*pointer).member.} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{17.67cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{17.67cm}}{\bf\textcolor{white}{Building a class with a header file}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{17.67cm}}{// Foo.h \newline struct Foo \{ \newline \textless{}Type\textgreater{} \textless{}attributeName\textgreater{}; \newline \newline static \textless{}ReturnType\textgreater{} \textless{}functionName\textgreater{}(\textless{}Type\textgreater{} \textless{}variable\textgreater{}); \newline static \textless{}ReturnType\textgreater{} \textless{}functionName\textgreater{}(\textless{}Type\textgreater{} \textless{}variable\textgreater{}); \newline static \textless{}ReturnType\textgreater{} \textless{}functionName\textgreater{}(\textless{}Type\textgreater{} \textless{}variable\textgreater{}); \newline \}; \newline \newline // Foo.cpp \newline \#include "Foo.h" \newline \newline \textless{}ReturnType\textgreater{} Foo::\textless{}functionName\textgreater{}(\textless{}Type\textgreater{} \textless{}variable\textgreater{}) \newline \{ \newline // implementation \newline \} \newline \newline \textless{}ReturnType\textgreater{} Foo::\textless{}functionName\textgreater{}(\textless{}Type\textgreater{} \textless{}variable\textgreater{}) \newline \{ \newline // implementation \newline \} \newline \newline \textless{}ReturnType\textgreater{} Foo::\textless{}functionName\textgreater{}(\textless{}Type\textgreater{} \textless{}variable\textgreater{}) \newline \{ \newline // implementation \newline \}} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{17.67cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{17.67cm}}{\bf\textcolor{white}{enum class}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{17.67cm}}{enum class \textless{}EnumName\textgreater{} \{ \textless{}Val1\textgreater{}, \textless{}Val2\textgreater{}, \textless{}Val3\textgreater{} \}; \newline \newline \textless{}EnumName\textgreater{} \textless{}var\textgreater{} = \textless{}EnumName\textgreater{}::\textless{}Val\_n\textgreater{}; // where n is an Val in the definition.} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \SetRowColor{LightBackground} \mymulticolumn{1}{x{17.67cm}}{You can think of these as a variable, but ahead of time it is constrained to a specific set of values.} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{17.67cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{17.67cm}}{\bf\textcolor{white}{Definition of a Dangling Pointer}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{17.67cm}}{int{\emph{ createNumber() \{ \newline int x = 42; // lives on the stack \newline return \&x; // return its address \newline \} // x is destroyed here \newline \newline int main() \{ \newline int}} p = createNumber(); \newline *p; // ❌ dangling — x is gone, memory no longer belongs to you \newline \}} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \SetRowColor{LightBackground} \mymulticolumn{1}{x{17.67cm}}{Dangling pointer — a pointer that points to memory that no longer belongs to you. The object it pointed to has been destroyed, but the pointer still holds its old address.} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{17.67cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{17.67cm}}{\bf\textcolor{white}{Address of Operator}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{17.67cm}}{int x = 42; \newline int* ptr = \&x; // ptr holds the memory address of x} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{17.67cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{17.67cm}}{\bf\textcolor{white}{Definition of a Caller}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{17.67cm}}{int add(int a, int b) \{ // callee \newline return a + b; \newline \} \newline \newline int main() \{ // caller \newline add(1, 2); \newline \}} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \SetRowColor{LightBackground} \mymulticolumn{1}{x{17.67cm}}{Caller — the function that calls another function.} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{17.67cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{17.67cm}}{\bf\textcolor{white}{static\_cast}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{17.67cm}}{targetType result = static\_cast\textless{}targetType\textgreater{}(sourceValue);} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \SetRowColor{LightBackground} \mymulticolumn{1}{x{17.67cm}}{A C++ operator that explicitly converts a value from one type to another at compile time, making the conversion visible and intentional in the code. It is safer than a C-style cast because the compiler checks that the conversion is valid. It is used when you need to convert between related types such as int and float, or long and int.} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{17.67cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{17.67cm}}{\bf\textcolor{white}{Direct versus copy initialization of a constructor}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{17.67cm}}{T b(a); // direct initialization \newline T b = a; // copy initialization} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \SetRowColor{LightBackground} \mymulticolumn{1}{x{17.67cm}}{Direct initialization T b(a) and copy initialization T b = a are two syntax styles that both invoke the copy constructor to create a new object from an existin…Direct initialization T b(a) and copy initialization T b = a are two syntax styles that both invoke the copy constructor to create a new object from an existing one. They produce the same result — a new object constructed as a copy of a.} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{17.67cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{17.67cm}}{\bf\textcolor{white}{Operator=}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{17.67cm}}{T\& operator=(const T\& other) \{ \newline return *this; \newline \}} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \SetRowColor{LightBackground} \mymulticolumn{1}{x{17.67cm}}{operator= defines what happens when you use = on an object.operator= defines what happens when you use = on an object. It takes a reference to another object of the same type and returns a reference to itself via return *this, enabling chained assignments like c = b = a.} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{17.67cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{17.67cm}}{\bf\textcolor{white}{Default parameter values}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{17.67cm}}{// In C++, function parameters can be given default values, meaning the caller does not need to provide them \newline // explicitly. Default parameters must appear at the end of the parameter list, so no non-defaulted parameter \newline // can follow a defaulted one. If the caller omits a defaulted argument, the default value is used \newline // automatically; otherwise the caller's value takes precedence. \newline \newline void foo(int a, int b, int c = 10); // c defaults to 10 \newline void bar(int a, int b = 5, int c = 10); // b defaults to 5, c to 10 \newline // void baz(int a = 1, int b, int c); // ERROR: non-default after default \newline \newline // Valid calls: \newline foo(1, 2); // c = 10 \newline foo(1, 2, 3); // c = 3 \newline \newline bar(1); // b = 5, c = 10 \newline bar(1, 2); // b = 2, c = 10 \newline bar(1, 2, 3); // b = 2, c = 3} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{17.67cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{17.67cm}}{\bf\textcolor{white}{static}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{17.67cm}}{static variableName = \textless{}value\textgreater{};} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \SetRowColor{LightBackground} \mymulticolumn{1}{x{17.67cm}}{Signals that a value belongs to the class, not any instance, and is fixed at compile time.} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{17.67cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{17.67cm}}{\bf\textcolor{white}{const placed after a member function}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{17.67cm}}{class Object \{ \newline int m\_value = 10; \newline \newline public: \newline int getValue() const \{ return m\_value; \} // const: can only read \newline void setValue(int v) \{ m\_value = v; \} // non-const: can modify \newline \};} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \SetRowColor{LightBackground} \mymulticolumn{1}{x{17.67cm}}{A const placed after a method signature means the function is read-only — it cannot modify any {\bf{member variables}} of the object. It acts as a guardrail, forcing the compiler to catch accidental modifications inside the function. Use it on any method that only needs to read state, such as getters, to make your intent clear and your code safer.} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{17.67cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{17.67cm}}{\bf\textcolor{white}{Definition of a Non-Static Member Function}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{17.67cm}}{class Dog \{ \newline public: \newline std::string name; \newline \newline void bark() \{ \newline std::cout \textless{}\textless{} name \textless{}\textless{} " says woof!\textbackslash{}n"; \newline \} \newline \}; \newline \newline int main() \{ \newline Dog d; \newline d.name = "Rex"; \newline \newline d.bark(); // prints "Rex says woof!" \newline \}} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \SetRowColor{LightBackground} \mymulticolumn{1}{x{17.67cm}}{A non-static member function is a function that belongs to a specific instance of a class, and always operates on that instance through this.} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{17.67cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{17.67cm}}{\bf\textcolor{white}{Passing Functions (Callbacks)}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{17.67cm}}{\#include \textless{}iostream\textgreater{} \newline \newline void sayHello() \newline \{ \newline std::cout \textless{}\textless{} "Hello!" \textless{}\textless{} std::endl; \newline \} \newline \newline void execute(void (*callback)()) \newline \{ \newline callback(); \newline \} \newline \newline int main() \newline \{ \newline execute(\&sayHello); // pass the function, not call it \newline \}} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \SetRowColor{LightBackground} \mymulticolumn{1}{x{17.67cm}}{In C++, functions can be passed as parameters to other functions using function pointers, allowing you to decide what code runs without hardcoding it. The receiving function holds onto the address of the passed function and calls it at the right time. This is the foundation of event-driven programming, where you say "call this function when something happens"} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{17.67cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{17.67cm}}{\bf\textcolor{white}{Defnition of a Thread}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{17.67cm}}{\#include \textless{}thread\textgreater{} \newline \#include \textless{}iostream\textgreater{} \newline \newline void doWork() \{ \newline std::cout \textless{}\textless{} "thread running\textbackslash{}n"; \newline \} \newline \newline int main() \{ \newline std::thread t(doWork); // thread starts here \newline t.join(); // main waits for it to finish \newline \}} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \SetRowColor{LightBackground} \mymulticolumn{1}{x{17.67cm}}{An independent sequence of execution within a program. The OS can run multiple threads concurrently, each doing their own work, while sharing the same memory.} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{17.67cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{17.67cm}}{\bf\textcolor{white}{L values (Left Values)}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{17.67cm}}{int x = 10; // x is an lvalue \newline x = 20; // valid: lvalue on the left of = \newline \newline int y = x + 1; // (x + 1) is an rvalue — temporary, no fixed address \newline int z = 42; // 42 is an rvalue literal \newline // \&(x + 1); // ERROR: cannot take address of an rvalue} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \SetRowColor{LightBackground} \mymulticolumn{1}{x{17.67cm}}{An lvalue (locator value) is an expression that refers to a memory location and can appear on the left-hand side of an assignment. It represents an object that persists beyond a single expression — it has an identifiable address in memory.} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{17.67cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{17.67cm}}{\bf\textcolor{white}{Definition of a Static Method}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{17.67cm}}{class MathHelper \{ \newline public: \newline static int add(int a, int b) \{ \newline return a + b; \newline \} \newline \}; \newline \newline int result = MathHelper::add(3, 5); // 8} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \SetRowColor{LightBackground} \mymulticolumn{1}{x{17.67cm}}{A static method is a function that belongs to the class itself rather than to any instance of it. It has no access to instance attributes and can be called without ever creating an object.} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{17.67cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{17.67cm}}{\bf\textcolor{white}{Definition of a Signature}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{17.67cm}}{\#include \textless{}iostream\textgreater{} \newline \newline void greet(std::string name) \{ // signature: greet(std::string) \newline std::cout \textless{}\textless{} "Hello, " \textless{}\textless{} name \textless{}\textless{} "!\textbackslash{}n"; \newline \} \newline \newline int main() \{ \newline greet("Alice"); // → Hello, Alice! \newline \}} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \SetRowColor{LightBackground} \mymulticolumn{1}{x{17.67cm}}{A function signature in C++ is the part of a function declaration that identifies it uniquely to the compiler. It consists of: \newline \newline Function name \newline Parameter types (number, order, and types)} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{17.67cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{17.67cm}}{\bf\textcolor{white}{Reference declaration (in type declarations)}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{17.67cm}}{int x = 42; \newline int\& ref = x; // ref IS x — same memory location \newline \newline ref = 100; // x is now 100} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{17.67cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{17.67cm}}{\bf\textcolor{white}{bitwise OR assignment operator}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{17.67cm}}{|= \newline \newline // Example \newline \newline bool result = false; // 0 \newline result |= false; // 0 | 0 = 0 \newline result |= false; // 0 | 0 = 0 \newline result |= true; // 0 | 1 = 1 \newline result |= false; // 1 | 0 = 1 — stays true \newline result |= false; // 1 | 0 = 1 — stays true \newline // result is true} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \SetRowColor{LightBackground} \mymulticolumn{1}{x{17.67cm}}{Bitwise OR assignment (|=) — takes the existing bits of the left operand, ORs them column by column with the bits of the right operand, and stores the result b…Bitwise OR assignment (|=) — takes the existing bits of the left operand, ORs them column by column with the bits of the right operand, and stores the result back into the left operand. Can only turn bits on, never off.} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{17.67cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{17.67cm}}{\bf\textcolor{white}{Constant Member Functions}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{17.67cm}}{class Foo \{ \newline int m\_value = 0; \newline public: \newline int getValue() const \{ return m\_value; \} // read → const \newline void setValue(int n) \{ m\_value = n; \} // write → no const \newline \};} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \SetRowColor{LightBackground} \mymulticolumn{1}{x{17.67cm}}{Guarantees the method will not modify any member variables.} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{17.67cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{17.67cm}}{\bf\textcolor{white}{Macros}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{17.67cm}}{// Creation \newline \#define OPERATION(input) input {\emph{ multiplier \newline \newline // Usage \newline int result = OPERATION(value); // expands to: int result = value }} multiplier;} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \SetRowColor{LightBackground} \mymulticolumn{1}{x{17.67cm}}{A preprocessor directive defined with \#define that is expanded before the compiler sees the code. It is not a function or a class, and can be used for text substitution, constants, or conditional compilation. Macros are considered old fashioned in modern C++ but are still used in cases where compile time behaviour is needed.} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{17.67cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{17.67cm}}{\bf\textcolor{white}{The Two Main Ways of Initializing a Function}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{17.67cm}}{// 1. Default then assign \newline std::thread t; \newline t = std::thread(foo); \newline \newline // 2. Declare and initialise in one line \newline std::thread t(foo);} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{17.67cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{17.67cm}}{\bf\textcolor{white}{Passing This to Other Objects}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{17.67cm}}{\#include \textless{}iostream\textgreater{} \newline \newline class B \{ \newline public: \newline void hello(class A{\emph{ a); \newline \}; \newline \newline class A \{ \newline public: \newline int number = 42; \newline \newline void send() \{ \newline B b; \newline b.hello(this); // passing this to B \newline \} \newline \}; \newline \newline void B::hello(A}} a) \{ \newline std::cout \textless{}\textless{} a-\textgreater{}number \textless{}\textless{} "\textbackslash{}n"; // prints 42 \newline \} \newline \newline int main() \{ \newline A a; \newline a.send(); \newline \}} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \SetRowColor{LightBackground} \mymulticolumn{1}{x{17.67cm}}{Anytime a class the keyword "this" is implicit and automatic. Once a class has to pass data from the class into another object the "this" keyword has to be passed.} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{17.67cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{17.67cm}}{\bf\textcolor{white}{Endless For-Loop}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{17.67cm}}{for (;;)} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{17.67cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{17.67cm}}{\bf\textcolor{white}{Lambda Functions}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{17.67cm}}{{[}\&\textless{}OBJECT\textgreater{}{]} \{ \newline \textless{}CODE\textgreater{}; \newline \textless{}CODE\textgreater{}; \newline \}} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \SetRowColor{LightBackground} \mymulticolumn{1}{x{17.67cm}}{The brackets tell the lambda which variables from the surrounding scope it's allowed to use, and how it remembers them. \newline \newline By default, a lambda body cannot see any local variable from the function it's written inside. The capture list is how you grant access, one variable (or one default rule) at a time.} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{17.67cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{17.67cm}}{\bf\textcolor{white}{Constructing an obect in C++}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{17.67cm}}{TypeName \seqsplit{variableName(constructorArguments);}} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \SetRowColor{LightBackground} \mymulticolumn{1}{x{17.67cm}}{This is how it would look in Python: \newline variable\_name = \seqsplit{ClassName(constructor\_arguments)}} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{17.67cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{17.67cm}}{\bf\textcolor{white}{Dereferencing a Pointer}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{17.67cm}}{int x = 42; \newline int{\emph{ p = \&x; // p holds the address of x \newline \newline }}p; // dereference — follow the pointer to get 42} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \SetRowColor{LightBackground} \mymulticolumn{1}{x{17.67cm}}{The * in front of a pointer means "go to the address this pointer holds and give me what's there."} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{17.67cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{17.67cm}}{\bf\textcolor{white}{Range Based For-Loops}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{17.67cm}}{for (const \textless{}type\textgreater{}\& \textless{}element\textgreater{} : \textless{}collection\textgreater{}) \{ \newline // use \textless{}element\textgreater{} \newline \}} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \SetRowColor{LightBackground} \mymulticolumn{1}{x{17.67cm}}{A range-based for loop is a loop that iterates over every element in a collection, from the first to the last, without needing to manually manage an index or i…A range-based for loop is a loop that iterates over every element in a collection, from the first to the last, without needing to manually manage an index or iterator. \newline \newline {\bf{const}} — omit if you need to modify \textless{}element\textgreater{} \newline {\bf{\&}} — omit if you want a copy of each element rather than a reference} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{17.67cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{17.67cm}}{\bf\textcolor{white}{Template Functions}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{17.67cm}}{template \textless{}typename T\textgreater{} \newline T add(T a, T b) \{ \newline return a + b; // T must support the + operator \newline \}} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{17.67cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{17.67cm}}{\bf\textcolor{white}{Try Catch}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{17.67cm}}{try \{ \newline // code that might throw an exception \newline \} catch (const std::exception\& e) \{ \newline // handle exception \newline \}} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \SetRowColor{LightBackground} \mymulticolumn{1}{x{17.67cm}}{catch(...). This is for any unkown exception that can't be caught by an std::exception.} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{17.67cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{17.67cm}}{\bf\textcolor{white}{Template Aliases}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{17.67cm}}{// The template \newline template \textless{}typename T\textgreater{} \newline class Box \{ \newline T contents; \newline \}; \newline \newline // Without an alias — verbose \newline Box\textless{}int\textgreater{} myBox; \newline \newline // The alias \newline using IntBox = Box\textless{}int\textgreater{}; \newline \newline // With the alias — cleaner, same thing \newline IntBox myBox;} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{17.67cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{17.67cm}}{\bf\textcolor{white}{=delete}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{17.67cm}}{class T \{ \newline T(const T\&) = delete; // copying forbidden \newline \}; \newline \newline T a; \newline T b(a); // compiler error} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \SetRowColor{LightBackground} \mymulticolumn{1}{x{17.67cm}}{= delete explicitly forbids a function from being used, turning a potential runtime crash into a compile time error.= delete explicitly forbids a function from being used, turning a potential runtime crash into a compile time error.} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{17.67cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{17.67cm}}{\bf\textcolor{white}{constexpr}} \tn \SetRowColor{white} \mymulticolumn{1}{x{17.67cm}}{constexpr \textless{}datatype\textgreater{} variableName = \textless{}value\textgreater{};% Row Count 1 (+ 1) } \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \SetRowColor{LightBackground} \mymulticolumn{1}{x{17.67cm}}{constexpr declares a value that is fixed and known at compile time, meaning the compiler bakes it directly into the binary rather than storing it in memory. This eliminates any runtime cost of looking up the value, since the compiler simply substitutes it wherever it appears in the code. It also makes intent explicit — clearly signaling to other developers that this value is a compile time constant that will never change.} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{17.67cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{17.67cm}}{\bf\textcolor{white}{Factory Method}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{17.67cm}}{class MyClass \{ \newline public: \newline static MyClass fromInput(std::string input) \{ \newline MyClass object; \newline object.m\_value = std::stoi(input); \newline return object; \newline \} \newline private: \newline int m\_value; \newline \}; \newline \newline MyClass result = \seqsplit{MyClass::fromInput(input);}} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \SetRowColor{LightBackground} \mymulticolumn{1}{x{17.67cm}}{A static method that constructs and returns an object, hiding the construction details from the caller.} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{17.67cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{17.67cm}}{\bf\textcolor{white}{Member Initializer List}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{17.67cm}}{class Car \{ \newline std::string m\_brand; \newline int m\_year; \newline \newline public: \newline Car(std::string brand, int year) \newline : m\_brand(brand) // initialize m\_brand with brand \newline , m\_year(year) // initialize m\_year with year \newline \{\} \newline \};} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \SetRowColor{LightBackground} \mymulticolumn{1}{x{17.67cm}}{A way to set a class's member variables before the constructor body runs. Uses a colon after the constructor parameters, with each member and its starting value in parentheses. std::move can be used instead of copying when transferring ownership of data. \newline \newline {\bf{What goes in the ():}} The value you want the member to start with. This is usually the matching constructor parameter, but can also be a literal value like 0 or "default".} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{17.67cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{17.67cm}}{\bf\textcolor{white}{Commenting Out Unused Parameters in C++}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{17.67cm}}{// You must accept both parameters to match the signature \newline void process(int /{\emph{unused}}/, int used) \newline \{ \newline std::cout \textless{}\textless{} used; // unused not needed, but must be in signature \newline \}} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \SetRowColor{LightBackground} \mymulticolumn{1}{x{17.67cm}}{When a function signature is fixed and you can't change it, but don't need all the parameters, comment out the name to suppress unused warnings while keeping the type.} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \end{document}