PageRenderTime 61ms CodeModel.GetById 25ms RepoModel.GetById 1ms app.codeStats 0ms

/week_1/lecture_1.tex

https://gitlab.com/IanHG/cpp_course
LaTeX | 353 lines | 231 code | 38 blank | 84 comment | 0 complexity | baf4123fb335ad1353a0576a9ec0ff34 MD5 | raw file
  1. %\documentclass[10pt,t,professionalfonts]{beamer}
  2. \documentclass[t]{beamer}
  3. \usetheme{CambridgeUS}
  4. \input{header.tex}
  5. \input{codetypeset.tex}
  6. \input{ls.tex}
  7. \begin{document}
  8. \title{Hello World! Basic \cppname{} syntax}
  9. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  10. %
  11. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  12. \begin{frame}
  13. \titlepage
  14. \end{frame}
  15. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  16. %%
  17. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  18. %\begin{frame}
  19. % {Why learn \cppname{}}
  20. %\cppname{} is a multi-paradigm programming language
  21. %\begin{itemize}
  22. % \item
  23. % Procedural/Imperative
  24. % \item
  25. % Functional
  26. % \item
  27. % Object-Oriented (OO)
  28. % \item
  29. % Generic (templates)
  30. % \item
  31. % Static typing and nominal typing
  32. % \item
  33. % A compiled language (Speed)
  34. %\end{itemize}
  35. %\end{frame}
  36. %\begin{frame}
  37. % {\cppname{}}
  38. % \verticalspace
  39. % Uses C syntax \\
  40. % \verticalspace
  41. % Procedural language (program consists of a number of sub procedures
  42. % that are performed sequentially)
  43. %\end{frame}
  44. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  45. %
  46. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  47. \begin{frame}[fragile]
  48. {The minimal \cppname{} program}
  49. \includecpp{examples/minimal/main.cpp}
  50. \verticalspace
  51. Every \cppname{} program must define a \icpp{main} function \\
  52. \verticalspace
  53. The \icpp{main} function must \emph{always} return an \icpp{int} \\
  54. \verticalspace
  55. The program will execute \emph{statements} between the curly braces
  56. \icpp{\{} and \icpp{\}} \\
  57. \verticalspace
  58. \icpp{//} indicates developer comments and will be ignored by the compiler \\
  59. \verticalspace
  60. Uses C syntax \\
  61. \verticalspace
  62. Procedural language (program consists of a number of sub procedures
  63. that are performed sequentially)
  64. \end{frame}
  65. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  66. %
  67. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  68. \begin{frame}[fragile]
  69. {Compiled languages}
  70. \begin{figure}
  71. \centering
  72. \includegraphics[width=\linewidth]{../figures/compile_process_cut}
  73. \end{figure}
  74. \cppname{} is a compiled programming language \\
  75. \verticalspace
  76. Compiles high-level langage to low-level machine code \\
  77. \verticalspace
  78. Compiling and running are two separate steps \\
  79. \verticalspace
  80. Compiling the code creates a \emph{binary} / \emph{executable} file \\
  81. \begin{terminal}[Compile procedure]
  82. $ ls # check contents of directory
  83. main.cpp
  84. $ g++ main.cpp # compile our program
  85. $ ls # we now see the executable 'a.out'
  86. a.out main.cpp
  87. $ ./a.out # running the executable
  88. \end{terminal}
  89. \end{frame}
  90. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  91. %
  92. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  93. \begin{frame}
  94. {Hello World \cppname-style}
  95. \includecpp[Hello World]{examples/helloworld/helloworld.cpp}
  96. \verticalspace
  97. The operator \icpp{<<} writes the rhs argument to the lhs argument \\
  98. \verticalspace
  99. \icpp{cout} is the standard output stream \\
  100. \verticalspace
  101. In \cppname{} the operator \icpp{;} means \emph{execute} statement \\
  102. \verticalspace
  103. When compiled and run will print \iterminal{Hello World!} to terminal \\
  104. \end{frame}
  105. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  106. %
  107. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  108. \begin{frame}[fragile]
  109. {Compile and run \iterminal{helloworld.cpp}}
  110. Compile with \iterminal{g++} compiler and run executable
  111. \begin{terminal}
  112. $ g++ helloworld.cpp
  113. $ ./a.out
  114. Hello World!
  115. \end{terminal}
  116. Executable is named \iterminal{a.out} \\
  117. \verticalspace
  118. We can rename it using the compiler option \icpp{-o <filename>}
  119. \begin{terminal}
  120. $ g++ -o helloworld.x helloworld.cpp
  121. $ ./helloworld.x
  122. Hello World!
  123. \end{terminal}
  124. \end{frame}
  125. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  126. %
  127. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  128. \begin{frame}[fragile]
  129. {Reading input}
  130. \vspace{-0.5cm}
  131. \includecpp
  132. [Hello World 2]{examples/helloworld/helloworld2.cpp}
  133. \vspace{-0.5cm}
  134. \begin{terminal}
  135. $ g++ helloworld2.cpp
  136. $ ./a.out
  137. Please enter your name:
  138. ian
  139. Hello World! ian
  140. \end{terminal}
  141. \end{frame}
  142. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  143. %
  144. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  145. \begin{frame}[fragile]{Types}
  146. \begin{cpp}[Some basic types]
  147. bool // Boolean value, possible values are true and false (1 and 0)
  148. char // single character e.g. 'a', 'q', and '7'
  149. int // signed integer e.g. 100, 1337, and -42
  150. double // floating point number, double precision e.g. 236.11 and -3.14
  151. \end{cpp}
  152. \verticalspace
  153. Built-in \cppname{} types \\
  154. \includecpplines[Declaration and assignment]{examples/declaration/main.cpp}{6}{15}
  155. \end{frame}
  156. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  157. %
  158. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  159. \begin{frame}[fragile]{Operators}
  160. \begin{cpp}[Arithmetic operators]
  161. x+y // plus
  162. +x // unary plus
  163. x-y // minus
  164. -x // unary minus
  165. x*y // multiply
  166. x/y // divide
  167. x%y // remainder
  168. \end{cpp}
  169. \includecpplines[Operator example]{examples/operators/main.cpp}{5}{12}
  170. \end{frame}
  171. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  172. %
  173. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  174. \begin{frame}[fragile]{Comparision operators}
  175. \begin{cpp}[Comparison operators]
  176. x==y // equal, true if x is equal to y
  177. x!=y // not equal, true if x is not equal to y (opposite of equal)
  178. x<y // less than, true if x is less than y
  179. x>y // greater than, true if x is greater than y
  180. x<=y // less than or equal, true if x is greather than or equal to y
  181. x>=y // greater than or equal, true if x is less than or equal to y
  182. \end{cpp}
  183. \verticalspace
  184. Comparison operators return \icpp{bool} type, thus they are either
  185. \icpp{true} or \icpp{false} (\icpp{1} or \icpp{0})
  186. \includecpplines[Not-equal-to]{examples/neq/main.cpp}{5}{7}
  187. \end{frame}
  188. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  189. % Scopes
  190. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  191. \begin{frame}[fragile]{Scopes}
  192. Curly braces \icpp{\{ \}} denotes scopes in \cppname{}. \\
  193. \verticalspace
  194. Only variables declared inside a given scope or in a surrounding scope
  195. can be accessed! \emph{(local variables)}
  196. \includecpplines[Scope]{examples/scope/main.cpp}{11}{24}
  197. \end{frame}
  198. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  199. %
  200. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  201. \begin{frame}[fragile]{if-then-else}
  202. \begin{cpp}[if-then-else structure]
  203. if(£\emph{condition}£)
  204. {
  205. // execute if condition is true
  206. }
  207. else
  208. {
  209. // execute if condition is false
  210. }
  211. \end{cpp}
  212. \verticalspace
  213. Execute body of code depending on a \emph{condition} \\
  214. \verticalspace
  215. \emph{condition} is binary, either \icpp{true} or \icpp{false} (think \icpp{bool}) \\
  216. \verticalspace
  217. Can check integer, floating-point, pointers, etc., \icpp{0} is \icpp{false} and everything else is \icpp{true}
  218. \end{frame}
  219. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  220. %
  221. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  222. \begin{frame}[fragile]{Example}
  223. \includecpplines[Example: If 1]{examples/if/if1.cpp}{5}{14}
  224. \includecpplines[Example: If 2]{examples/if/if2.cpp}{5}{10}
  225. \end{frame}
  226. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  227. %
  228. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  229. \begin{frame}[fragile]{if-then-else extended else-if}
  230. \begin{cpp}[if-then-else-if-then-else]
  231. if(£\emph{condition1}£)
  232. {
  233. // execute if condition1 is true
  234. }
  235. else if(£\emph{condition2}£)
  236. {
  237. // execute if condition1 is false and condition2 is true
  238. }
  239. else
  240. {
  241. // execute if condition1 and condition2 are both false
  242. }
  243. \end{cpp}
  244. \begin{cpp}[if no else (OK, we get it already!!!)]
  245. if(£\emph{condition}£)
  246. {
  247. // execute if conditions is true
  248. }
  249. \end{cpp}
  250. \end{frame}
  251. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  252. %
  253. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  254. \begin{frame}[fragile]{for-loop}
  255. \begin{cpp}[for structure]
  256. for(£\emph{initialize}£; £\emph{condition}£; £\emph{increment}£)
  257. {
  258. // execute body as long as condition is true
  259. // after code has been executed, do increment
  260. }
  261. \end{cpp}
  262. \includecpplines[Example: Adding some integers]{examples/for/for.cpp}{5}{10}
  263. \verticalspace
  264. \icpp{sum += i} is shorthand for \icpp{sum = sum + i} \\
  265. \verticalspace
  266. \icpp{++i} increments \icpp{i} by \icpp{1} and returns incremented value \\
  267. \end{frame}
  268. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  269. %
  270. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  271. \begin{frame}[fragile]{while-loop}
  272. \begin{cpp}[while structure]
  273. while(£\emph{condition}£)
  274. {
  275. // execute body as long as condition is true
  276. }
  277. \end{cpp}
  278. \includecpplines[Adding some integers using while]{examples/while/while.cpp}{5}{12}
  279. \end{frame}
  280. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  281. %
  282. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  283. \begin{frame}[fragile]{do-while}
  284. \begin{cpp}[do-while-loop]
  285. do
  286. {
  287. // execute once, then execute while condition is true
  288. }
  289. while(£\emph{condition}£);
  290. \end{cpp}
  291. \verticalspace
  292. Old form, can always be reformulated to a standard \icpp{while} loop \\
  293. \verticalspace
  294. Preferable to use the standard form!
  295. \end{frame}
  296. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  297. % VECTOR CLASS
  298. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  299. \begin{frame}[fragile]
  300. {Standard Template Library (STL)}
  301. STL implements alot of different functionality. \\
  302. \verticalspace
  303. Especially interesting to us is the vector class
  304. \includecpp[Using std::vector]{examples/std_vector/main.cpp}
  305. \end{frame}
  306. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  307. %
  308. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  309. \begin{frame}
  310. \begin{center}
  311. \Large
  312. Now lets get cracking at some problems!
  313. \end{center}
  314. \end{frame}
  315. \end{document}