\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{sadieweaver} \pdfinfo{ /Title (c-101-midterm-3.pdf) /Creator (Cheatography) /Author (sadieweaver) /Subject (C++ 101 Midterm 3 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}{56787D} \definecolor{LightBackground}{HTML}{F4F6F6} \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++ 101 Midterm 3 Cheat Sheet}}}} \\ \normalsize{by \textcolor{DarkBackground}{sadieweaver} via \textcolor{DarkBackground}{\uline{cheatography.com/86436/cs/20098/}}} \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}sadieweaver \\ \uline{cheatography.com/sadieweaver} \\ \end{tabulary} \vfill \columnbreak \begin{tabulary}{5.8cm}{L} \SetRowColor{FootBackground} \mymulticolumn{1}{p{5.377cm}}{\bf\textcolor{white}{Cheat Sheet}} \\ \vspace{-2pt}Published 27th July, 2019.\\ Updated 14th August, 2019.\\ 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}{istream ostream}} \tn % Row 0 \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{`friend ostream\& operator\textless{}\textless{}(ostream\& out, room\& r) \{ out \textless{}\textless{} "Width:" \textless{}\textless{} r.width \textless{}\textless{} ", Length:" \textless{}\textless{} r.length; return out; \}`} \tn % Row Count 3 (+ 3) % Row 1 \SetRowColor{white} \mymulticolumn{1}{x{5.377cm}}{`friend istream\& operator\textgreater{}\textgreater{}(istream\& in, room\& r) \{ cout \textless{}\textless{} "Enter width: " \textless{}\textless{} endl; in \textgreater{}\textgreater{} r.width; cout \textless{}\textless{} "Enter length: " \textless{} endl; in \textgreater{}\textgreater{} r.length; return in; \}`} \tn % Row Count 7 (+ 4) \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{x{2.4885 cm} x{2.4885 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{5.377cm}}{\bf\textcolor{white}{Strings vs C-strings (ch8)}} \tn % Row 0 \SetRowColor{LightBackground} `\#include \textless{}string\textgreater{}` & `\#include \textless{}cstring\textgreater{}` \tn % Row Count 1 (+ 1) % Row 1 \SetRowColor{white} \mymulticolumn{2}{x{5.377cm}}{{\bf{Reading in:}}} \tn % Row Count 2 (+ 1) % Row 2 \SetRowColor{LightBackground} & \textgreater{}\textgreater{} stops reading at the first space \tn % Row Count 4 (+ 2) % Row 3 \SetRowColor{white} `string input;`\{\{nl\}\}`getline(cin, input);` & `char input{[}100{]}`\{\{nl\}\}`cin.getline(input, 100);` \tn % Row Count 7 (+ 3) % Row 4 \SetRowColor{LightBackground} `string s1;` & empty c-string: `char s{[}20{]} = "";` \tn % Row Count 9 (+ 2) % Row 5 \SetRowColor{white} \mymulticolumn{2}{x{5.377cm}}{{\bf{Returning:}}} \tn % Row Count 10 (+ 1) % Row 6 \SetRowColor{LightBackground} & returning c-strings: return type of pointer. \{\{nl\}\} `char* funct()\{...return...\}` \tn % Row Count 15 (+ 5) % Row 7 \SetRowColor{white} \mymulticolumn{2}{x{5.377cm}}{for both: make sure that if you return something in a function if it's a variable defined in the function that is was defined dynamically or is `static`.} \tn % Row Count 19 (+ 4) % Row 8 \SetRowColor{LightBackground} \mymulticolumn{2}{x{5.377cm}}{{\bf{Operations:}}} \tn % Row Count 20 (+ 1) % Row 9 \SetRowColor{white} `s.length()` or `s.size()` & `strlen(s)` - s is a c-string \{\{nl\}\} cannot use .length() or .size() \tn % Row Count 24 (+ 4) % Row 10 \SetRowColor{LightBackground} `s2 = s1;` & `strcpy(s2, s1)` - copy s1 to s2 \tn % Row Count 26 (+ 2) % Row 11 \SetRowColor{white} `s1 == s2;` & `strcmp(s1, s2) == 0;` - tests for equality \tn % Row Count 29 (+ 3) % Row 12 \SetRowColor{LightBackground} `s1 += s3` & `strcat(char* s1, const char* s2);` \tn % Row Count 31 (+ 2) \end{tabularx} \par\addvspace{1.3em} \vfill \columnbreak \begin{tabularx}{5.377cm}{x{2.4885 cm} x{2.4885 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{5.377cm}}{\bf\textcolor{white}{Strings vs C-strings (ch8) (cont)}} \tn % Row 13 \SetRowColor{LightBackground} \mymulticolumn{2}{x{5.377cm}}{string s to double `stod(s);`} \tn % Row Count 1 (+ 1) % Row 14 \SetRowColor{white} \mymulticolumn{2}{x{5.377cm}}{`char* s` to `double`: `strtod(s, nullptr);`} \tn % Row Count 2 (+ 1) % Row 15 \SetRowColor{LightBackground} \mymulticolumn{2}{x{5.377cm}}{{\bf{Functions:}}} \tn % Row Count 3 (+ 1) % Row 16 \SetRowColor{white} \mymulticolumn{2}{x{5.377cm}}{{\bf{Random:}}} \tn % Row Count 4 (+ 1) % Row 17 \SetRowColor{LightBackground} & c-strings have a null termination character '/0'. So to define a c-string to hold 20 it's `char input{[}21{]};` \tn % Row Count 10 (+ 6) % Row 18 \SetRowColor{white} same thing as \textgreater{} & c++ doesn't check indexes to validate if something is within bounds \tn % Row Count 14 (+ 4) % Row 19 \SetRowColor{LightBackground} \mymulticolumn{2}{x{5.377cm}}{`int n = 3; cout \textless{}\textless{} (char)(n + '0') \textless{}\textless{} endl;` prints 3.} \tn % Row Count 16 (+ 2) % Row 20 \SetRowColor{white} \mymulticolumn{2}{x{5.377cm}}{`char c = 'D'; cout \textless{}\textless{} (c - 'A') \textless{}\textless{} endl;` prints 3.} \tn % Row Count 18 (+ 2) % Row 21 \SetRowColor{LightBackground} \mymulticolumn{2}{x{5.377cm}}{`char name{[}100{]}; char* get\_name() \{ . . . return \seqsplit{\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_;} \}` return `name`} \tn % Row Count 21 (+ 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}{Constructors}} \tn % Row 0 \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{{\bf{Default:}} `foo()` no arguments\{\{nl\}\}`foo f1;` or `foo* f2 = new foo;`} \tn % Row Count 2 (+ 2) % Row 1 \SetRowColor{white} \mymulticolumn{1}{x{5.377cm}}{{\bf{Conversion:}} `foo(int i)` one argument to be turned into the class object\{\{nl\}\}`foo f1(3);` or `foo* f2 = new foo(3);`} \tn % Row Count 5 (+ 3) % Row 2 \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{{\bf{General:}} `foo(int x, int y)` anything with more than one} \tn % Row Count 7 (+ 2) % Row 3 \SetRowColor{white} \mymulticolumn{1}{x{5.377cm}}{{\bf{Copy:}} `foo(foo\& f)` pointer argument} \tn % Row Count 8 (+ 1) % Row 4 \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{{\bf{Move:}} `foo(foo\&\& f)` double pointer} \tn % Row Count 9 (+ 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}{Constructor and Initializer List Examples}} \tn % Row 0 \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{Choose the best C++ class named person that has: A private member field for the person's first name; A private member field for the person's last name; A private member field for the person's age; and a public constructor:} \tn \mymulticolumn{1}{x{5.377cm}}{\hspace*{6 px}\rule{2px}{6px}\hspace*{6 px}`class Person\{ string first; string last; int age; \{\{nl\}\}public: person(string f, string l, int a) : first(f), last(l), age(a) \{\} \};`} \tn % Row Count 8 (+ 8) % Row 1 \SetRowColor{white} \mymulticolumn{1}{x{5.377cm}}{UML: {\bf{Student}}\{\{nl\}\}-name: string \{\{nl\}\} -gpa:double \{\{nl\}\}+Student(n: string, g: double)} \tn \mymulticolumn{1}{x{5.377cm}}{\hspace*{6 px}\rule{2px}{6px}\hspace*{6 px}`class Student\{\{nl\}\}\{private: \{\{nl\}\}string name;\{\{nl\}\}double gpa;\{\{nl\}\}public:\{\{nl\}\}Student(string n, double g) : name(n), gpa(g) \{\} \};`} \tn % Row Count 13 (+ 5) % Row 2 \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{Write a single constructor that works as default, conversion, and general: {\bf{UML fraction}}\{\{nl\}\}-numerator:int\{\{nl\}\}-denominator:int\{\{nl\}\}+fraction(n: int, d:int) \{\{nl\}\}(default values: n = 0, d = 1)} \tn \mymulticolumn{1}{x{5.377cm}}{\hspace*{6 px}\rule{2px}{6px}\hspace*{6 px}`fraction(int n = 0, int d = 1) : numerator(n), denominator(d) \{\}`} \tn % Row Count 19 (+ 6) % Row 3 \SetRowColor{white} \mymulticolumn{1}{x{5.377cm}}{UML: {\bf{Foo}}\{\{nl\}\}-count:int\{\{nl\}\}+running: bool (set running to true)\{\{nl\}\}+Foo(a\_count: int)\{\{nl\}\}-my\_helper(arg: int) : char} \tn \mymulticolumn{1}{x{5.377cm}}{\hspace*{6 px}\rule{2px}{6px}\hspace*{6 px}`class Foo\{\{nl\}\}\{private: \{\{nl\}\}int count;\{\{nl\}\}char my\_helper(int arg)\{\{nl\}\}public:\{\{nl\}\}bool running = true;\{\{nl\}\}Foo(int a\_count) : count(a\_count) \{\} \};`} \tn % Row Count 26 (+ 7) \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{5.377cm}}{\bf\textcolor{white}{Extras}} \tn % Row 0 \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{Only technical difference between structures and classes:} \tn \mymulticolumn{1}{x{5.377cm}}{\hspace*{6 px}\rule{2px}{6px}\hspace*{6 px}features in classes are private by default, features in structures are public by default} \tn % Row Count 4 (+ 4) % Row 1 \SetRowColor{white} \mymulticolumn{1}{x{5.377cm}}{Constructors name is} \tn \mymulticolumn{1}{x{5.377cm}}{\hspace*{6 px}\rule{2px}{6px}\hspace*{6 px}the same as the name of the class} \tn % Row Count 6 (+ 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}{Facts}} \tn % Row 0 \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{+: public\{\{nl\}\}-: private\{\{nl\}\}\#: protected\{\{nl\}\}underlined:static} \tn % Row Count 2 (+ 2) % Row 1 \SetRowColor{white} \mymulticolumn{1}{x{5.377cm}}{Auto: Stack\{\{nl\}\}Dynamic: Heap} \tn % Row Count 3 (+ 1) % Row 2 \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{defining functions outside of a class: `return-type \seqsplit{class::function-name(arguments)`} with a prototype included in class file.} \tn % Row Count 6 (+ 3) \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{x{1.8308 cm} x{1.3731 cm} x{1.3731 cm} } \SetRowColor{DarkBackground} \mymulticolumn{3}{x{5.377cm}}{\bf\textcolor{white}{Member/Friend Summary}} \tn % Row 0 \SetRowColor{LightBackground} member/nonmbr & Implicit Args & Explicit Args \tn % Row Count 2 (+ 2) % Row 1 \SetRowColor{white} {\bf{Unary Member:}} & 1 & 0 \tn % Row Count 4 (+ 2) % Row 2 \SetRowColor{LightBackground} {\bf{Unary Friend:}} & 0 & 1 \tn % Row Count 6 (+ 2) % Row 3 \SetRowColor{white} {\bf{Binary Mbr:}} & 1 & 1 \tn % Row Count 7 (+ 1) % Row 4 \SetRowColor{LightBackground} {\bf{Binary Friend:}} & 0 & 2 \tn % Row Count 9 (+ 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}{mbr/ nonmbr in and out a class}} \tn % Row 0 \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{Mbr defined in the class} \tn \mymulticolumn{1}{x{5.377cm}}{\hspace*{6 px}\rule{2px}{6px}\hspace*{6 px}`foo operator+(foo f)\{...\}`} \tn % Row Count 2 (+ 2) % Row 1 \SetRowColor{white} \mymulticolumn{1}{x{5.377cm}}{Mbr defined out the class} \tn \mymulticolumn{1}{x{5.377cm}}{\hspace*{6 px}\rule{2px}{6px}\hspace*{6 px}`foo foo::operator+(foo f)\{...\}`} \tn % Row Count 4 (+ 2) % Row 2 \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{Nonmbr defined in the class} \tn \mymulticolumn{1}{x{5.377cm}}{\hspace*{6 px}\rule{2px}{6px}\hspace*{6 px}`friend foo operator+(foo f1, foo f2) \{...\}`} \tn % Row Count 6 (+ 2) % Row 3 \SetRowColor{white} \mymulticolumn{1}{x{5.377cm}}{Nonmbr defined out the class} \tn \mymulticolumn{1}{x{5.377cm}}{\hspace*{6 px}\rule{2px}{6px}\hspace*{6 px}`foo operator+(foo f1, foo f2) \{...\}`} \tn % Row Count 8 (+ 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}{Command-line}} \tn % Row 0 \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{main function definition to allow a program to access command line arguments: \{\{nl\}\} `main(int argc, char* argv{[}{]})`} \tn % Row Count 3 (+ 3) % Row 1 \SetRowColor{white} \mymulticolumn{1}{x{5.377cm}}{A program is named "my\_program" and is executed from the command line as my\_program file1 file2 file3 file4 If the program is written in C++ and the arguments are passed in to main, what is the value of argc and what is stored in argv{[}2{]}? \{\{nl\}\} argc = 5, argv{[}2{]} = file2} \tn % Row Count 9 (+ 6) \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} % That's all folks \end{multicols*} \end{document}