/week_1/lecture_1.tex
LaTeX | 353 lines | 231 code | 38 blank | 84 comment | 0 complexity | baf4123fb335ad1353a0576a9ec0ff34 MD5 | raw file
- %\documentclass[10pt,t,professionalfonts]{beamer}
- \documentclass[t]{beamer}
- \usetheme{CambridgeUS}
- \input{header.tex}
- \input{codetypeset.tex}
- \input{ls.tex}
- \begin{document}
- \title{Hello World! Basic \cppname{} syntax}
- %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
- %
- %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
- \begin{frame}
- \titlepage
- \end{frame}
- %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
- %%
- %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
- %\begin{frame}
- % {Why learn \cppname{}}
- %\cppname{} is a multi-paradigm programming language
- %\begin{itemize}
- % \item
- % Procedural/Imperative
- % \item
- % Functional
- % \item
- % Object-Oriented (OO)
- % \item
- % Generic (templates)
- % \item
- % Static typing and nominal typing
- % \item
- % A compiled language (Speed)
- %\end{itemize}
- %\end{frame}
- %\begin{frame}
- % {\cppname{}}
- % \verticalspace
- % Uses C syntax \\
- % \verticalspace
- % Procedural language (program consists of a number of sub procedures
- % that are performed sequentially)
- %\end{frame}
- %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
- %
- %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
- \begin{frame}[fragile]
- {The minimal \cppname{} program}
- \includecpp{examples/minimal/main.cpp}
-
- \verticalspace
- Every \cppname{} program must define a \icpp{main} function \\
- \verticalspace
- The \icpp{main} function must \emph{always} return an \icpp{int} \\
- \verticalspace
- The program will execute \emph{statements} between the curly braces
- \icpp{\{} and \icpp{\}} \\
- \verticalspace
- \icpp{//} indicates developer comments and will be ignored by the compiler \\
- \verticalspace
- Uses C syntax \\
- \verticalspace
- Procedural language (program consists of a number of sub procedures
- that are performed sequentially)
- \end{frame}
- %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
- %
- %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
- \begin{frame}[fragile]
- {Compiled languages}
- \begin{figure}
- \centering
- \includegraphics[width=\linewidth]{../figures/compile_process_cut}
- \end{figure}
-
- \cppname{} is a compiled programming language \\
- \verticalspace
- Compiles high-level langage to low-level machine code \\
- \verticalspace
- Compiling and running are two separate steps \\
- \verticalspace
- Compiling the code creates a \emph{binary} / \emph{executable} file \\
- \begin{terminal}[Compile procedure]
- $ ls # check contents of directory
- main.cpp
- $ g++ main.cpp # compile our program
- $ ls # we now see the executable 'a.out'
- a.out main.cpp
- $ ./a.out # running the executable
- \end{terminal}
- \end{frame}
- %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
- %
- %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
- \begin{frame}
- {Hello World \cppname-style}
- \includecpp[Hello World]{examples/helloworld/helloworld.cpp}
- \verticalspace
- The operator \icpp{<<} writes the rhs argument to the lhs argument \\
- \verticalspace
- \icpp{cout} is the standard output stream \\
- \verticalspace
- In \cppname{} the operator \icpp{;} means \emph{execute} statement \\
- \verticalspace
- When compiled and run will print \iterminal{Hello World!} to terminal \\
- \end{frame}
- %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
- %
- %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
- \begin{frame}[fragile]
- {Compile and run \iterminal{helloworld.cpp}}
- Compile with \iterminal{g++} compiler and run executable
- \begin{terminal}
- $ g++ helloworld.cpp
- $ ./a.out
- Hello World!
- \end{terminal}
- Executable is named \iterminal{a.out} \\
- \verticalspace
- We can rename it using the compiler option \icpp{-o <filename>}
- \begin{terminal}
- $ g++ -o helloworld.x helloworld.cpp
- $ ./helloworld.x
- Hello World!
- \end{terminal}
- \end{frame}
- %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
- %
- %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
- \begin{frame}[fragile]
- {Reading input}
- \vspace{-0.5cm}
- \includecpp
- [Hello World 2]{examples/helloworld/helloworld2.cpp}
- \vspace{-0.5cm}
- \begin{terminal}
- $ g++ helloworld2.cpp
- $ ./a.out
- Please enter your name:
- ian
- Hello World! ian
- \end{terminal}
- \end{frame}
- %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
- %
- %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
- \begin{frame}[fragile]{Types}
- \begin{cpp}[Some basic types]
- bool // Boolean value, possible values are true and false (1 and 0)
- char // single character e.g. 'a', 'q', and '7'
- int // signed integer e.g. 100, 1337, and -42
- double // floating point number, double precision e.g. 236.11 and -3.14
- \end{cpp}
- \verticalspace
- Built-in \cppname{} types \\
- \includecpplines[Declaration and assignment]{examples/declaration/main.cpp}{6}{15}
- \end{frame}
- %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
- %
- %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
- \begin{frame}[fragile]{Operators}
- \begin{cpp}[Arithmetic operators]
- x+y // plus
- +x // unary plus
- x-y // minus
- -x // unary minus
- x*y // multiply
- x/y // divide
- x%y // remainder
- \end{cpp}
- \includecpplines[Operator example]{examples/operators/main.cpp}{5}{12}
- \end{frame}
- %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
- %
- %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
- \begin{frame}[fragile]{Comparision operators}
- \begin{cpp}[Comparison operators]
- x==y // equal, true if x is equal to y
- x!=y // not equal, true if x is not equal to y (opposite of equal)
- x<y // less than, true if x is less than y
- x>y // greater than, true if x is greater than y
- x<=y // less than or equal, true if x is greather than or equal to y
- x>=y // greater than or equal, true if x is less than or equal to y
- \end{cpp}
- \verticalspace
- Comparison operators return \icpp{bool} type, thus they are either
- \icpp{true} or \icpp{false} (\icpp{1} or \icpp{0})
- \includecpplines[Not-equal-to]{examples/neq/main.cpp}{5}{7}
- \end{frame}
- %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
- % Scopes
- %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
- \begin{frame}[fragile]{Scopes}
- Curly braces \icpp{\{ \}} denotes scopes in \cppname{}. \\
- \verticalspace
- Only variables declared inside a given scope or in a surrounding scope
- can be accessed! \emph{(local variables)}
- \includecpplines[Scope]{examples/scope/main.cpp}{11}{24}
- \end{frame}
- %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
- %
- %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
- \begin{frame}[fragile]{if-then-else}
- \begin{cpp}[if-then-else structure]
- if(£\emph{condition}£)
- {
- // execute if condition is true
- }
- else
- {
- // execute if condition is false
- }
- \end{cpp}
- \verticalspace
- Execute body of code depending on a \emph{condition} \\
- \verticalspace
- \emph{condition} is binary, either \icpp{true} or \icpp{false} (think \icpp{bool}) \\
- \verticalspace
- Can check integer, floating-point, pointers, etc., \icpp{0} is \icpp{false} and everything else is \icpp{true}
- \end{frame}
- %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
- %
- %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
- \begin{frame}[fragile]{Example}
- \includecpplines[Example: If 1]{examples/if/if1.cpp}{5}{14}
-
- \includecpplines[Example: If 2]{examples/if/if2.cpp}{5}{10}
- \end{frame}
- %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
- %
- %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
- \begin{frame}[fragile]{if-then-else extended else-if}
- \begin{cpp}[if-then-else-if-then-else]
- if(£\emph{condition1}£)
- {
- // execute if condition1 is true
- }
- else if(£\emph{condition2}£)
- {
- // execute if condition1 is false and condition2 is true
- }
- else
- {
- // execute if condition1 and condition2 are both false
- }
- \end{cpp}
- \begin{cpp}[if no else (OK, we get it already!!!)]
- if(£\emph{condition}£)
- {
- // execute if conditions is true
- }
- \end{cpp}
- \end{frame}
- %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
- %
- %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
- \begin{frame}[fragile]{for-loop}
- \begin{cpp}[for structure]
- for(£\emph{initialize}£; £\emph{condition}£; £\emph{increment}£)
- {
- // execute body as long as condition is true
- // after code has been executed, do increment
- }
- \end{cpp}
-
- \includecpplines[Example: Adding some integers]{examples/for/for.cpp}{5}{10}
- \verticalspace
- \icpp{sum += i} is shorthand for \icpp{sum = sum + i} \\
- \verticalspace
- \icpp{++i} increments \icpp{i} by \icpp{1} and returns incremented value \\
- \end{frame}
- %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
- %
- %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
- \begin{frame}[fragile]{while-loop}
- \begin{cpp}[while structure]
- while(£\emph{condition}£)
- {
- // execute body as long as condition is true
- }
- \end{cpp}
-
- \includecpplines[Adding some integers using while]{examples/while/while.cpp}{5}{12}
- \end{frame}
- %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
- %
- %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
- \begin{frame}[fragile]{do-while}
- \begin{cpp}[do-while-loop]
- do
- {
- // execute once, then execute while condition is true
- }
- while(£\emph{condition}£);
- \end{cpp}
-
- \verticalspace
- Old form, can always be reformulated to a standard \icpp{while} loop \\
- \verticalspace
- Preferable to use the standard form!
- \end{frame}
- %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
- % VECTOR CLASS
- %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
- \begin{frame}[fragile]
- {Standard Template Library (STL)}
- STL implements alot of different functionality. \\
- \verticalspace
- Especially interesting to us is the vector class
- \includecpp[Using std::vector]{examples/std_vector/main.cpp}
- \end{frame}
- %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
- %
- %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
- \begin{frame}
- \begin{center}
- \Large
- Now lets get cracking at some problems!
- \end{center}
- \end{frame}
- \end{document}