C++ SS22: Vorlesung Stand 03.04., Proto-Übungszettel 4

This commit is contained in:
2022-04-03 23:52:10 +02:00
parent 2f97b8ef91
commit f5ecb8a8de
4 changed files with 560 additions and 5 deletions
+157
View File
@@ -2361,7 +2361,164 @@ void swap(int &x, int &y) {
\end{frame}
\begin{frame}[fragile]
\frametitle{Konstanten I}
\begin{itemize}
\item Klassischer Weg: \cppinline{#define}
\item Ein Präprozessor-Makro: Vor dem Kompiliervorgang wird der Text im Quellcode ersetzt
\begin{cpp}
#define SYMBOL //z.B. bei Headerguard
#define PI 3.1415 //Zahlenkonstante
#define MAX(a, b) (a>b?a:b) //Makro
\end{cpp}
\item Wenig sinnvoll, Makros lassen sich durch \inlinecpp{inline}-Funktionen ersetzen
\item Unklarer Typ
\end{itemize}
\end{frame}
\begin{frame}[fragile]
\frametitle{Konstanten II}
\begin{itemize}
\item Besserer Weg: \cppinline{const}
\item Schlüsselwort vor der Variablendeklaration
\begin{cpp}[fontsize=\footnotesize]
// constant_values1.cpp
int main() {
const int i = 5;
i = 10; // error: assignment of read-only variable ‘i’
i++; // error: increment of read-only variable ‘i’
}
\end{cpp}
\end{itemize}
\end{frame}
\begin{frame}[fragile]
\frametitle{Type conversions I}
Was tut folgender Code?
\begin{cpp}[fontsize=\footnotesize]
#include <iostream>
int main(int argc, char **argv)
{
std::cout <<1./2. <<std::endl;
std::cout <<1/2 <<std::endl;
}
\end{cpp}
\end{frame}
\begin{frame}[fragile]
\frametitle{Type conversions II}
\begin{bash}
fspanier@thomson:~$ ./conv
0.5
0
fspanier@thomson:~$
\end{bash}
Aber wieso???
\begin{itemize}
\item Der Code macht Annahmen darüber, welchen Typ die Zahlenkonstanten haben
\item 2: \cppinline{int}, 2. \cppinline{float}
\item Für 1/2 wird also ein Ganzzahldivision durchgeführt, deren Ergebnis auf eine Ganzzahl abgerundet wird
\end{itemize}
\end{frame}
\begin{frame}[fragile]
\frametitle{Type conversions III}
\begin{itemize}
\item Konvertierung von einem Typ in einen anderen findet bei kompatiblen Typen automatisch statt
\item Trivial, wenn der neue Typ \glqq größer\grqq~ ist (\cppinline{int} $\rightarrow$ \cppinline{long}, \cppinline{float} $\rightarrow$ \cppinline{double})
\item Problematisch, wenn der Typ \glqq kleiner\grqq~ ist
\end{itemize}
\begin{cpp}[fontsize=\footnotesize]
#include <iostream>
int main(int argc, char **argv)
{
long a = 12345678901;
int b;
b = a;
std::cout <<a <<std::endl; //12345678901
std::cout <<b <<std::endl; //-539222987
}
\end{cpp}
\end{frame}
\begin{frame}[fragile]
\frametitle{Type conversions IV}
\begin{itemize}
\item C++ ist stark typisiert
\item Einige Konvertierungen erfordern eine explizite Angabe.
\end{itemize}
\begin{cpp}[fontsize=\footnotesize]
double x = 10.3;
int y;
y = int (x); // functional notation
y = (int) x; // c-like cast notation
\end{cpp}
\end{frame}
\begin{frame}[fragile]
\frametitle{Type conversions V}
\begin{itemize}
\item Die Umwandlung von Strings in Zahlen ist kein Typecast\dots
\end{itemize}
\begin{cpp}[fontsize=\footnotesize]
// string
char string[] = "938";
// char -> int
int i = atoi( string );
// char -> double
double d = atof( string );
\end{cpp}
\end{frame}
\begin{frame}[fragile]
\frametitle{Type conversions VI}
\begin{itemize}
\item Die Umwandlung von Zahlen in Strings ist\dots kompliziert
\item Viele Lösungen. Hier: Stringstream. Eine String wird wie \cppinline{cout} verwendet
\end{itemize}
\begin{cpp}[fontsize=\footnotesize]
// stringstream::str
#include <string> // std::string
#include <iostream> // std::cout
#include <sstream> // std::stringstream, std::stringbuf
int main () {
int i = 17;
std::string s;
std::stringstream ss;
ss << i;
ss >> s;
std::cout << s << std::endl;
return 0;
}
\end{cpp}
\end{frame}
\begin{frame}
\begin{center}
{\Large Fragen / Unklarheiten in C++ bis jetzt?}
\end{center}