\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{skinker} \pdfinfo{ /Title (c-exam-1.pdf) /Creator (Cheatography) /Author (skinker) /Subject (C++ Exam 1 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}{FFA852} \definecolor{LightBackground}{HTML}{FFF4E9} \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++ Exam 1 Cheat Sheet}}}} \\ \normalsize{by \textcolor{DarkBackground}{skinker} via \textcolor{DarkBackground}{\uline{cheatography.com/201011/cs/42497/}}} \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}skinker \\ \uline{cheatography.com/skinker} \\ \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 23rd February, 2024.\\ 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{multicols*}{3} \begin{tabularx}{5.377cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{5.377cm}}{\bf\textcolor{white}{The Big Three}} \tn % Row 0 \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{{\bf{Copy Constructor}} {\emph{Used to construct an object from another, existing object}}} \tn % Row Count 2 (+ 2) % Row 1 \SetRowColor{white} \mymulticolumn{1}{x{5.377cm}}{`className(const className \&original) \{\{\{nl\}\} // copy over everything from \{\{nl\}\} original to this \}`} \tn % Row Count 5 (+ 3) % Row 2 \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{{\bf{Copy Assignment Operator}} {\emph{Used to copy one object into another object}}} \tn % Row Count 7 (+ 2) % Row 3 \SetRowColor{white} \mymulticolumn{1}{x{5.377cm}}{`ClassName\& operator=(const ClassName\& \{\{nl\}\}original) \{\{\{nl\}\} //same as copy constructor\{\{nl\}\} return *this \} `} \tn % Row Count 10 (+ 3) % Row 4 \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{{\bf{Destructor}} {\emph{Used when an object is destroyed—when it falls out of scope, or when delete is called on a pointer to an object}}} \tn % Row Count 13 (+ 3) % Row 5 \SetRowColor{white} \mymulticolumn{1}{x{5.377cm}}{`\textasciitilde{}ExampleClass()\{ \{\{nl\}\} delete item\_name \{\{nl\}\} //or more complicated code \}`} \tn % Row Count 15 (+ 2) \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{5.377cm}}{\bf\textcolor{white}{Deep and Shallow Copy}} \tn % Row 0 \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{{\bf{Deep Copy}}} \tn % Row Count 1 (+ 1) % Row 1 \SetRowColor{white} \mymulticolumn{1}{x{5.377cm}}{copies the data itself - allocate more space and clean it up (use destructors)} \tn % Row Count 3 (+ 2) % Row 2 \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{Deep copy takes two steps: \{\{nl\}\} 1. Allocate space for the duplicate data \{\{nl\}\}`int* deep = new int{[}5{]};` \{\{nl\}\}2. Copy the data values from the original location `\{\{nl\}\}for (int i = 0; i \textless{} 5; i++) \{ \{\{nl\}\} deep{[}i{]} = ptr{[}i{]}; \}`} \tn % Row Count 8 (+ 5) % Row 3 \SetRowColor{white} \mymulticolumn{1}{x{5.377cm}}{{\bf{Shallow Copy}}} \tn % Row Count 9 (+ 1) % Row 4 \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{copies pointers} \tn % Row Count 10 (+ 1) % Row 5 \SetRowColor{white} \mymulticolumn{1}{x{5.377cm}}{Create the array \{\{nl\}\} `int* ptr = new int{[}5{]}; \{\{nl\}\}for (int i = 0; i \textless{} 5; i++) \{ \{\{nl\}\} ptr{[}i{]} = i * 2; \{\{nl\}\} // Set values to 0, 2, 4, 6, 8`\{\{nl\}\}Shallow copy the array \{\{nl\}\} `int* shallow = ptr;`} \tn % Row Count 15 (+ 5) \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{5.377cm}}{\bf\textcolor{white}{Vectors and Arrays}} \tn % Row 0 \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{What does `.push\_back()` do in a vector?} \tn % Row Count 1 (+ 1) % Row 1 \SetRowColor{white} \mymulticolumn{1}{x{5.377cm}}{What does `.at()` do in a vector?} \tn % Row Count 2 (+ 1) % Row 2 \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{What does `{[} {]}` do in a vector?} \tn % Row Count 3 (+ 1) % Row 3 \SetRowColor{white} \mymulticolumn{1}{x{5.377cm}}{How do you sort an array/vector?} \tn % Row Count 4 (+ 1) \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{5.377cm}}{\bf\textcolor{white}{Vectors}} \tn % Row 0 \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{Vectors are dynamic, meaning you can make changes to them while the program is running. Vectors are particularly helpful when you don't know how large your collection of elements will become.} \tn % Row Count 4 (+ 4) % Row 1 \SetRowColor{white} \mymulticolumn{1}{x{5.377cm}}{In order to use vectors, you must include `\#include \textless{}vector\textgreater{}` in the header of your program.} \tn % Row Count 6 (+ 2) % Row 2 \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{vector structure : `vector\textless{}int\textgreater{} vec\_name(3);`} \tn % Row Count 7 (+ 1) % Row 3 \SetRowColor{white} \mymulticolumn{1}{x{5.377cm}}{`name.push\_back(data)` adds whatever is in the parantheses to the end of the vector} \tn % Row Count 9 (+ 2) % Row 4 \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{To add an element to a specific index in the vector: \seqsplit{`vec\_name.insert(vector.begin()+1}, 50);` adds 50 to index 1} \tn % Row Count 12 (+ 3) % Row 5 \SetRowColor{white} \mymulticolumn{1}{x{5.377cm}}{To remove an element from the end of a vector: `vec\_name.pop\_back()`} \tn % Row Count 14 (+ 2) % Row 6 \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{To erase a specific index: \seqsplit{`vec\_name.erase(vec\_name.begin()+1);`} erases the element at the index 1} \tn % Row Count 16 (+ 2) % Row 7 \SetRowColor{white} \mymulticolumn{1}{x{5.377cm}}{`vec\_name.at(3)` accesses the element at index 3, use this to modify specific elements} \tn % Row Count 18 (+ 2) \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{5.377cm}}{\bf\textcolor{white}{Simple (Return types, Loops, \& Conditionals)}} \tn % Row 0 \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{How do you create functions in C++?} \tn \mymulticolumn{1}{x{5.377cm}}{\hspace*{6 px}\rule{2px}{6px}\hspace*{6 px}`returnType Function(int var\_name)\{\}` \{\{nl\}\} {\emph{put `ClassName::` beforehand if working in source file}}} \tn % Row Count 4 (+ 4) % Row 1 \SetRowColor{white} \mymulticolumn{1}{x{5.377cm}}{What are the return types of these functions?} \tn \mymulticolumn{1}{x{5.377cm}}{\hspace*{6 px}\rule{2px}{6px}\hspace*{6 px}void, string, int, unsigned int, vector\textless{}data type\textgreater{} (usually done with pointer), boolean, array (usually done with pointer)} \tn % Row Count 8 (+ 4) % Row 2 \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{Is it possible to return any data type?} \tn \mymulticolumn{1}{x{5.377cm}}{\hspace*{6 px}\rule{2px}{6px}\hspace*{6 px}yes, as long as it is declared in the beginning of the function} \tn % Row Count 11 (+ 3) % Row 3 \SetRowColor{white} \mymulticolumn{1}{x{5.377cm}}{How do you write if conditionals and for loops in C++?} \tn \mymulticolumn{1}{x{5.377cm}}{\hspace*{6 px}\rule{2px}{6px}\hspace*{6 px}`if(condition)\{\} \{\{nl\}\} for(int i = 0; i \textless{} condition; i++)\{\}`} \tn % Row Count 15 (+ 4) % Row 4 \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{What is the difference between if, else if, and while loops?} \tn % Row Count 17 (+ 2) % Row 5 \SetRowColor{white} \mymulticolumn{1}{x{5.377cm}}{What are some of the algebraic and comparison operators in C++?} \tn \mymulticolumn{1}{x{5.377cm}}{\hspace*{6 px}\rule{2px}{6px}\hspace*{6 px}+, -, =, \textless{}, \textgreater{}, ==, !=, \&\&, ||} \tn % Row Count 20 (+ 3) % Row 6 \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{How do you use them with different data types?} \tn \mymulticolumn{1}{x{5.377cm}}{\hspace*{6 px}\rule{2px}{6px}\hspace*{6 px}must be used to compare 2 of the same data type, unless you use an operator overload} \tn % Row Count 23 (+ 3) \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{5.377cm}}{\bf\textcolor{white}{Pointers and references functions}} \tn % Row 0 \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{What are pointers?} \tn \mymulticolumn{1}{x{5.377cm}}{\hspace*{6 px}\rule{2px}{6px}\hspace*{6 px}Pointers tell us the location of something else. A pointer is a variable that holds the memory address of another variable. A pointer needs to be dereferenced with the * operator to access the memory location it points to. `int* name` creates a pointer to an int} \tn % Row Count 7 (+ 7) % Row 1 \SetRowColor{white} \mymulticolumn{1}{x{5.377cm}}{What are references?} \tn \mymulticolumn{1}{x{5.377cm}}{\hspace*{6 px}\rule{2px}{6px}\hspace*{6 px}References act as a stand-in (or alias) for another variable. A reference is the variable that it references. Any changes to a reference change the original. They don't use memory addresses, no dereferencing.} \tn % Row Count 13 (+ 6) % Row 2 \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{How do you pass objects by references and pointers?} \tn \mymulticolumn{1}{x{5.377cm}}{\hspace*{6 px}\rule{2px}{6px}\hspace*{6 px}using `FuncName(int \&var\_name)\{\}} \tn % Row Count 16 (+ 3) % Row 3 \SetRowColor{white} \mymulticolumn{1}{x{5.377cm}}{What do you need to take into account when creating a function that intends to swap two integers, a and b?} \tn \mymulticolumn{1}{x{5.377cm}}{\hspace*{6 px}\rule{2px}{6px}\hspace*{6 px}make sure to pass by reference so the values can be changed} \tn % Row Count 21 (+ 5) % Row 4 \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{What does `-\textgreater{}` do in pointers?} \tn \mymulticolumn{1}{x{5.377cm}}{\hspace*{6 px}\rule{2px}{6px}\hspace*{6 px}Indirect membership operator (For pointers to objects)} \tn % Row Count 24 (+ 3) \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{x{1.00694 cm} x{1.78503 cm} x{1.78503 cm} } \SetRowColor{DarkBackground} \mymulticolumn{3}{x{5.377cm}}{\bf\textcolor{white}{Vectors and Arrays}} \tn % Row 0 \SetRowColor{LightBackground} {\bf{Method/Types}} & {\bf{Vector}} & {\bf{Array}} \tn % Row Count 2 (+ 2) % Row 1 \SetRowColor{white} Create & `vector\textless{}type\textgreater{} var(num)` or `vector\textless{}type\textgreater{} var\{element, element…\} ` & `type var{[}num{]}` or `type var{[}{]} = \{element, element…\}` \tn % Row Count 7 (+ 5) % Row 2 \SetRowColor{LightBackground} Find number of \seqsplit{elements} & `var.size()` & `sizeof(var)/sizeof(var{[}0{]})` \tn % Row Count 10 (+ 3) % Row 3 \SetRowColor{white} Access an element & \seqsplit{`var.at(index)`} & `var{[}index{]}` \tn % Row Count 13 (+ 3) % Row 4 \SetRowColor{LightBackground} Modify an element & `var.at(index) = element` & `var{[}index{]} = element` \tn % Row Count 16 (+ 3) % Row 5 \SetRowColor{white} Add an element & \seqsplit{`var.push\_back(element)`} or \seqsplit{`var.insert(var.begin()+index}, element)` & n/a \tn % Row Count 21 (+ 5) % Row 6 \SetRowColor{LightBackground} Remove an element & \seqsplit{`var.pop\_back()`} or \seqsplit{`var.erase(var.begin()+index)`} & n/a \tn % Row Count 25 (+ 4) % Row 7 \SetRowColor{white} for loop & `for (int i = 0; i \textless{} var.size(); i++) \{\{nl\}\}\{cout \textless{}\textless{} var.at(i);\}` & `for (int i = 0; i \textless{} sizeof(var)/sizeof(var{[}0{]}); i++)\{\{nl\}\} \{cout \textless{}\textless{} var{[}i{]};\}` \tn % Row Count 31 (+ 6) \end{tabularx} \par\addvspace{1.3em} \vfill \columnbreak \begin{tabularx}{5.377cm}{x{1.00694 cm} x{1.78503 cm} x{1.78503 cm} } \SetRowColor{DarkBackground} \mymulticolumn{3}{x{5.377cm}}{\bf\textcolor{white}{Vectors and Arrays (cont)}} \tn % Row 8 \SetRowColor{LightBackground} \seqsplit{Enhanced} for loop & `for (type i : var) \{cout \textless{}\textless{} i\}` & `for (type i : var) \{cout \textless{}\textless{} i\}` \tn % Row Count 3 (+ 3) % Row 9 \SetRowColor{white} Common \seqsplit{compatible} types & integer, double, boolean, strings & int, double, boolean, strings \tn % Row Count 6 (+ 3) \hhline{>{\arrayrulecolor{DarkBackground}}---} \end{tabularx} \par\addvspace{1.3em} % That's all folks \end{multicols*} \end{document}