Skip to content
Snippets Groups Projects
Commit f961a41d authored by Peter Gerwinski's avatar Peter Gerwinski
Browse files

Beispiele 3.6.2025

parent f0dea465
No related branches found
No related tags found
No related merge requests found
Showing
with 511 additions and 3 deletions
No preview for this file type
...@@ -20,7 +20,7 @@ ...@@ -20,7 +20,7 @@
% Attribution-ShareAlike 3.0 Unported License along with this % Attribution-ShareAlike 3.0 Unported License along with this
% document. If not, see <https://creativecommons.org/licenses/>. % document. If not, see <https://creativecommons.org/licenses/>.
% README: C++: Exceptions, Typ-Konversionen, intelligente Zeiger % README: C++: Exceptions, Typ-Konversionen
\documentclass[10pt,t]{beamer} \documentclass[10pt,t]{beamer}
...@@ -59,7 +59,6 @@ ...@@ -59,7 +59,6 @@
\begin{itemize} \begin{itemize}
\vspace{-\smallskipamount} \vspace{-\smallskipamount}
\item[\textbf{\dots}] \item[\textbf{\dots}]
\color{medgreen}
% \item[5.0] Was ist C? % \item[5.0] Was ist C?
% \item[5.1] Was ist C++? % \item[5.1] Was ist C++?
% \underconstruction % \underconstruction
...@@ -75,12 +74,15 @@ ...@@ -75,12 +74,15 @@
\color{red} \color{red}
\item[5.10] Exceptions \item[5.10] Exceptions
\item[5.11] Typ-Konversionen \item[5.11] Typ-Konversionen
\color{black}
\item[5.12] Intelligente Zeiger \item[5.12] Intelligente Zeiger
\end{itemize} \end{itemize}
\item[\textbf{6}] \textbf{Datenorganisation} \item[\textbf{6}] \textbf{Datenorganisation}
\vspace{-\smallskipamount} \vspace{-\smallskipamount}
\item[\textbf{\dots}] % \item[\textbf{\dots}]
\end{itemize} \end{itemize}
\medskip
Weitere mögliche Themen: Hardwarenahe Algorithmen, Quantencomputer, \dots
\vspace*{-1cm} \vspace*{-1cm}
\end{frame} \end{frame}
...@@ -296,6 +298,8 @@ ...@@ -296,6 +298,8 @@
\end{itemize} \end{itemize}
\end{frame} \end{frame}
\iffalse
\subsection{Intelligente Zeiger} \subsection{Intelligente Zeiger}
\begin{frame}[fragile] \begin{frame}[fragile]
...@@ -347,4 +351,6 @@ ...@@ -347,4 +351,6 @@
\end{itemize} \end{itemize}
\end{frame} \end{frame}
\fi
\end{document} \end{document}
#include <iostream>
using namespace std;
int main ()
{
int a = 42;
int b = 0;
if (b == 0)
{
cout << "Irgendwas ist schiefgelaufen: Ganzzahldivision durch Null" << endl;
}
else
{
int c = a / b;
cout << "c = " << c << endl;
}
return 0;
}
#include <iostream>
using namespace std;
int main ()
{
try
{
int a = 42;
int b = 0;
if (b == 0)
throw "Ganzzahldivision durch Null";
int c = a / b;
cout << "c = " << c << endl;
}
catch (const char *msg)
{
cout << "Irgendwas ist schiefgelaufen: " << msg << endl;
}
return 0;
}
#include <iostream>
using namespace std;
void check_answer (int answer)
{
if (answer != 42)
{
try
{
if (answer == 10)
throw answer;
else if (answer == 137)
throw "alpha";
}
catch (int e)
{
cout << "Yeah!" << endl;
throw;
}
throw "bullshit";
}
}
int main ()
{
int answer;
cout << "What's your answer? ";
cin >> answer;
try
{
if (answer == 23)
throw answer;
else
check_answer (answer);
}
catch (const char *e)
{
cout << "caught string exception: " << e << endl;
}
cout << "And what was the question?" << endl;
return 0;
}
#include <iostream>
using namespace std;
class philosophy_exception: public exception
{
int answer;
public:
philosophy_exception (int a) { answer = a; }
};
int main ()
{
int answer;
cout << "What's your answer? ";
cin >> answer;
try
{
if (answer != 42)
throw philosophy_exception (answer);
cout << "And what was the question?" << endl;
}
catch (exception &e)
{
cout << "caught exception" << endl;
}
return 0;
}
#include <iostream>
using namespace std;
class philosophy_exception: public exception
{
int answer;
public:
philosophy_exception (int a) { answer = a; }
int get_answer () { return answer; }
};
int main ()
{
int answer;
cout << "What's your answer? ";
cin >> answer;
try
{
if (answer != 42)
throw philosophy_exception (answer);
cout << "And what was the question?" << endl;
}
catch (philosophy_exception &e)
{
cout << "caught philosophy exception: " << e.get_answer () << endl;
}
catch (exception &e)
{
cout << "caught exception" << endl;
}
return 0;
}
#include <iostream>
using namespace std;
class philosophy_exception: public exception
{
int answer;
public:
philosophy_exception (int a) { answer = a; }
int get_answer () { return answer; }
};
int main ()
{
int answer;
cout << "What's your answer? ";
cin >> answer;
try
{
if (answer != 42)
throw philosophy_exception (answer);
cout << "And what was the question?" << endl;
}
catch (exception &e)
{
cout << "caught exception" << endl;
}
catch (philosophy_exception &e)
{
cout << "caught philosophy exception: " << e.get_answer () << endl;
}
return 0;
}
#include <iostream>
#include <sstream>
using namespace std;
class philosophy_exception: public exception
{
int answer;
public:
philosophy_exception (int a) { answer = a; }
virtual const char *what ();
};
const char *philosophy_exception::what ()
{
ostringstream buffer;
buffer << "philosophy exception #" << answer;
return buffer.str ().c_str ();
}
int main ()
{
int answer;
cout << "What's your answer? ";
cin >> answer;
try
{
if (answer == 42)
cout << "And what was the question?" << endl;
else
throw philosophy_exception (answer);
}
catch (exception &e)
{
cout << "caught exception: " << e.what () << endl;
}
return 0;
}
#include <iostream>
#include <sstream>
using namespace std;
class philosophy_exception: public exception
{
int answer;
public:
philosophy_exception (int a) { answer = a; }
virtual const char *what ();
};
const char *philosophy_exception::what ()
{
ostringstream buffer;
buffer << "philosophy exception #" << answer;
return buffer.str ().c_str ();
}
int main ()
{
int answer;
cout << "What's your answer? ";
cin >> answer;
try
{
if (answer == 42)
cout << "And what was the question?" << endl;
else
throw philosophy_exception (answer);
}
catch (philosophy_exception &e)
{
cout << "caught exception: " << e.what () << endl;
}
catch (exception &e)
{
cout << "caught exception: " << e.what () << endl;
}
return 0;
}
#include <iostream>
#include <sstream>
using namespace std;
class philosophy_exception: public exception
{
int answer;
public:
philosophy_exception (int a) { answer = a; }
virtual const char *what ();
};
const char *philosophy_exception::what ()
{
return "philosophy_exception";
}
int main ()
{
int answer;
cout << "What's your answer? ";
cin >> answer;
try
{
if (answer == 42)
cout << "And what was the question?" << endl;
else
throw philosophy_exception (answer);
}
catch (philosophy_exception &e)
{
cout << "caught exception: " << e.what () << endl;
}
catch (exception &e)
{
cout << "caught exception: " << e.what () << endl;
}
return 0;
}
#include <iostream>
#include <sstream>
#include <string.h>
using namespace std;
class philosophy_exception: public exception
{
int answer;
static char buffer[42];
public:
philosophy_exception (int a) { answer = a; }
virtual const char *what () const noexcept;
};
const char *philosophy_exception::what () const noexcept
{
ostringstream tmp_buffer;
tmp_buffer << "philosophy exception #" << answer;
strcpy (buffer, tmp_buffer.str ().c_str ());
return buffer;
}
char philosophy_exception::buffer[42] = "Marvin";
int main ()
{
int answer;
cout << "What's your answer? ";
cin >> answer;
try
{
if (answer == 42)
cout << "And what was the question?" << endl;
else
throw philosophy_exception (answer);
}
catch (exception &e)
{
cout << "caught exception: " << e.what () << endl;
}
return 0;
}
#include <iostream>
using namespace std;
int main ()
{
try
{
int a = 42;
int b = 0;
int c = a / b;
cout << "c = " << c << endl;
}
catch (const char *msg)
{
cout << "Irgendwas ist schiefgelaufen: " << msg << endl;
}
return 0;
}
#include <iostream>
using namespace std;
int main ()
{
try
{
int a = 42;
int b = 0;
int c = a / b;
cout << "c = " << c << endl;
}
catch (const char *msg)
{
cerr << "Irgendwas ist schiefgelaufen: " << msg << endl;
}
catch (const exception &e)
{
cerr << "Exception caught: " << e.what() << endl;
}
return 0;
}
#include <stdio.h>
#include <stdint.h>
#include <inttypes.h>
int main ()
{
const char *hello = "Hello, world!";
uint64_t address = (uint64_t) hello;
printf ("%" PRIx64 "\n", address);
return 0;
}
#include <stdio.h>
#include <stdint.h>
#include <inttypes.h>
int main ()
{
const char *hello = "Hello, world!";
uint64_t address = uint64_t (hello);
printf ("%" PRIx64 "\n", address);
return 0;
}
#include <stdio.h>
struct TBase
{
virtual void print ()
{
printf ("(Base)\n");
}
};
struct THello: public TBase
{
virtual void print ()
{
printf ("Hello, world!\n");
}
};
int main ()
{
TBase *p = new THello;
p->print ();
return 0;
}
#include <stdio.h>
struct TBase
{
void print ()
{
printf ("(Base)\n");
}
};
struct THello: public TBase
{
void print ()
{
printf ("Hello, world!\n");
}
};
int main ()
{
TBase *p = new THello;
p->print ();
return 0;
}
#include <stdio.h>
struct TBase
{
virtual void print ()
{
printf ("(Base)\n");
}
};
struct THello: public TBase
{
virtual void print ()
{
printf ("Hello, world!\n");
}
};
int main ()
{
TBase *p = new THello;
THello *q = p;
q->print ();
return 0;
}
#include <stdio.h>
struct TBase
{
virtual void print ()
{
printf ("(Base)\n");
}
};
struct THello: public TBase
{
virtual void print ()
{
printf ("Hello, world!\n");
}
};
int main ()
{
TBase *p = new THello;
THello *q = dynamic_cast <THello *> (p);
q->print ();
return 0;
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment