diff --git a/20250520/ad-20250520.pdf b/20250520/ad-20250520.pdf index 1b1eec7fbbe0d3179d8c9559f8451f576085d021..4ed0442e98c2b2bb6708570949cbceee1a796271 100644 Binary files a/20250520/ad-20250520.pdf and b/20250520/ad-20250520.pdf differ diff --git a/20250520/ad-20250520.tex b/20250520/ad-20250520.tex index 58c7ff6ef0f721d036f35cce2728da0112c92a5c..7ca66785f8be55b66225e1e933eadef0e46506b8 100644 --- a/20250520/ad-20250520.tex +++ b/20250520/ad-20250520.tex @@ -20,7 +20,7 @@ % Attribution-ShareAlike 3.0 Unported License along with this % document. If not, see <https://creativecommons.org/licenses/>. -% README: C++: Objekte, Strings, Templates, … +% README: C++: Objekte, Strings \documentclass[10pt,t]{beamer} @@ -69,6 +69,7 @@ \item[5.6] Objekte \color{red} \item[5.7] Strings + \color{black} \item[5.8] Templates \vspace{-\smallskipamount} \item[\textbf{\dots}] @@ -512,6 +513,8 @@ \end{itemize} \end{frame} +\iffalse + \subsection{Templates} \begin{frame}[fragile] @@ -721,4 +724,6 @@ \end{itemize} \end{frame} +\fi + \end{document} diff --git a/20250520/objects-15.cpp b/20250520/objects-15.cpp new file mode 100644 index 0000000000000000000000000000000000000000..9e607e7e25f962db23606faa14890751eb2e3da0 --- /dev/null +++ b/20250520/objects-15.cpp @@ -0,0 +1,55 @@ +#include <stdio.h> + +class TBase +{ + virtual void print () = 0; +}; + +class TInteger: TBase +{ + int content; + virtual void print (); + TInteger (int i); +}; + +class TString: TBase +{ + char *content; + virtual void print (); + TString (char *s); +}; + +void TInteger::print () +{ + printf ("Integer: %d\n", content); +} + +void TString::print () +{ + printf ("String: \"%s\"\n", content); +} + +TInteger::TInteger (int i) +{ + content = i; +} + +TString::TString (char *s) +{ + content = s; +} + +int main (void) +{ + TBase *object[] = { new TInteger (42), + new TString ("Hello, world!"), + NULL }; + + for (int i = 0; object[i]; i++) + object[i]->print (); + + for (int i = 0; object[i]; i++) + delete object[i]; + + return 0; +} diff --git a/20250520/objects-16.cpp b/20250520/objects-16.cpp new file mode 100644 index 0000000000000000000000000000000000000000..18ff2db1c07bf8a36ad1dd9116af25b3127a454f --- /dev/null +++ b/20250520/objects-16.cpp @@ -0,0 +1,58 @@ +#include <stdio.h> + +class TBase +{ +public: + virtual void print () = 0; +}; + +class TInteger: TBase +{ +public: + int content; + virtual void print (); + TInteger (int i); +}; + +class TString: TBase +{ +public: + char *content; + virtual void print (); + TString (char *s); +}; + +void TInteger::print () +{ + printf ("Integer: %d\n", content); +} + +void TString::print () +{ + printf ("String: \"%s\"\n", content); +} + +TInteger::TInteger (int i) +{ + content = i; +} + +TString::TString (char *s) +{ + content = s; +} + +int main (void) +{ + TBase *object[] = { new TInteger (42), + new TString ("Hello, world!"), + NULL }; + + for (int i = 0; object[i]; i++) + object[i]->print (); + + for (int i = 0; object[i]; i++) + delete object[i]; + + return 0; +} diff --git a/20250520/objects-17.cpp b/20250520/objects-17.cpp new file mode 100644 index 0000000000000000000000000000000000000000..6b4ddd7c020d6b0cba1b7dae6e9298ab1f45dc13 --- /dev/null +++ b/20250520/objects-17.cpp @@ -0,0 +1,58 @@ +#include <stdio.h> + +class TBase +{ +public: + virtual void print () = 0; +}; + +class TInteger: public TBase +{ +public: + int content; + virtual void print (); + TInteger (int i); +}; + +class TString: public TBase +{ +public: + char *content; + virtual void print (); + TString (char *s); +}; + +void TInteger::print () +{ + printf ("Integer: %d\n", content); +} + +void TString::print () +{ + printf ("String: \"%s\"\n", content); +} + +TInteger::TInteger (int i) +{ + content = i; +} + +TString::TString (char *s) +{ + content = s; +} + +int main (void) +{ + TBase *object[] = { new TInteger (42), + new TString ("Hello, world!"), + NULL }; + + for (int i = 0; object[i]; i++) + object[i]->print (); + + for (int i = 0; object[i]; i++) + delete object[i]; + + return 0; +} diff --git a/20250520/objects-18.cpp b/20250520/objects-18.cpp new file mode 100644 index 0000000000000000000000000000000000000000..4cd7f6663f248691b2b0453b6ac5fa70674d3693 --- /dev/null +++ b/20250520/objects-18.cpp @@ -0,0 +1,58 @@ +#include <stdio.h> + +class TBase +{ +public: + virtual void print () = 0; +}; + +class TInteger: public TBase +{ +public: + int content; + virtual void print (); + TInteger (int i); +}; + +class TString: public TBase +{ +public: + char *content; + virtual void print (); + TString (const char *s); +}; + +void TInteger::print () +{ + printf ("Integer: %d\n", content); +} + +void TString::print () +{ + printf ("String: \"%s\"\n", content); +} + +TInteger::TInteger (int i) +{ + content = i; +} + +TString::TString (char *s) +{ + content = s; +} + +int main (void) +{ + TBase *object[] = { new TInteger (42), + new TString ("Hello, world!"), + NULL }; + + for (int i = 0; object[i]; i++) + object[i]->print (); + + for (int i = 0; object[i]; i++) + delete object[i]; + + return 0; +} diff --git a/20250520/objects-19.cpp b/20250520/objects-19.cpp new file mode 100644 index 0000000000000000000000000000000000000000..67a54c00267572be4ea8696a664d592224a581b9 --- /dev/null +++ b/20250520/objects-19.cpp @@ -0,0 +1,58 @@ +#include <stdio.h> + +class TBase +{ +public: + virtual void print () = 0; +}; + +class TInteger: public TBase +{ +public: + int content; + virtual void print (); + TInteger (int i); +}; + +class TString: public TBase +{ +public: + char *content; + virtual void print (); + TString (const char *s); +}; + +void TInteger::print () +{ + printf ("Integer: %d\n", content); +} + +void TString::print () +{ + printf ("String: \"%s\"\n", content); +} + +TInteger::TInteger (int i) +{ + content = i; +} + +TString::TString (const char *s) +{ + content = s; +} + +int main (void) +{ + TBase *object[] = { new TInteger (42), + new TString ("Hello, world!"), + NULL }; + + for (int i = 0; object[i]; i++) + object[i]->print (); + + for (int i = 0; object[i]; i++) + delete object[i]; + + return 0; +} diff --git a/20250520/objects-20.cpp b/20250520/objects-20.cpp new file mode 100644 index 0000000000000000000000000000000000000000..9aa67e4574bebdc22e77e775be96252c55dc41f6 --- /dev/null +++ b/20250520/objects-20.cpp @@ -0,0 +1,58 @@ +#include <stdio.h> + +class TBase +{ +public: + virtual void print () = 0; +}; + +class TInteger: public TBase +{ +public: + int content; + virtual void print (); + TInteger (int i); +}; + +class TString: public TBase +{ +public: + const char *content; + virtual void print (); + TString (const char *s); +}; + +void TInteger::print () +{ + printf ("Integer: %d\n", content); +} + +void TString::print () +{ + printf ("String: \"%s\"\n", content); +} + +TInteger::TInteger (int i) +{ + content = i; +} + +TString::TString (const char *s) +{ + content = s; +} + +int main (void) +{ + TBase *object[] = { new TInteger (42), + new TString ("Hello, world!"), + NULL }; + + for (int i = 0; object[i]; i++) + object[i]->print (); + + for (int i = 0; object[i]; i++) + delete object[i]; + + return 0; +} diff --git a/20250520/objects-21.cpp b/20250520/objects-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..29dd861a0b0d2686c73fd4f25490f78696cbe8d8 --- /dev/null +++ b/20250520/objects-21.cpp @@ -0,0 +1,63 @@ +#include <stdio.h> + +class TBase +{ +public: + virtual void print () = 0; + TBase (); +}; + +class TInteger: public TBase +{ +public: + int content; + virtual void print (); + TInteger (int i); +}; + +class TString: public TBase +{ +public: + const char *content; + virtual void print (); + TString (const char *s); +}; + +void TInteger::print () +{ + printf ("Integer: %d\n", content); +} + +void TString::print () +{ + printf ("String: \"%s\"\n", content); +} + +TBase::TBase () +{ +} + +TInteger::TInteger (int i) +{ + content = i; +} + +TString::TString (const char *s) +{ + content = s; +} + +int main (void) +{ + TBase *object[] = { new TInteger (42), + new TString ("Hello, world!"), + NULL }; + + for (int i = 0; object[i]; i++) + object[i]->print (); + + for (int i = 0; object[i]; i++) + delete object[i]; + + return 0; +} diff --git a/20250520/objects-22.cpp b/20250520/objects-22.cpp new file mode 100644 index 0000000000000000000000000000000000000000..f8e5039989876bf4a844872be8486f1bea1869a8 --- /dev/null +++ b/20250520/objects-22.cpp @@ -0,0 +1,63 @@ +#include <stdio.h> + +class TBase +{ +public: + virtual void print () = 0; + ~TBase (); +}; + +class TInteger: public TBase +{ +public: + int content; + virtual void print (); + TInteger (int i); +}; + +class TString: public TBase +{ +public: + const char *content; + virtual void print (); + TString (const char *s); +}; + +void TInteger::print () +{ + printf ("Integer: %d\n", content); +} + +void TString::print () +{ + printf ("String: \"%s\"\n", content); +} + +TBase::~TBase () +{ +} + +TInteger::TInteger (int i) +{ + content = i; +} + +TString::TString (const char *s) +{ + content = s; +} + +int main (void) +{ + TBase *object[] = { new TInteger (42), + new TString ("Hello, world!"), + NULL }; + + for (int i = 0; object[i]; i++) + object[i]->print (); + + for (int i = 0; object[i]; i++) + delete object[i]; + + return 0; +} diff --git a/20250520/objects-23.cpp b/20250520/objects-23.cpp new file mode 100644 index 0000000000000000000000000000000000000000..aa34c0aaafba3ebd69ca1fb9b6ae25a400f89025 --- /dev/null +++ b/20250520/objects-23.cpp @@ -0,0 +1,63 @@ +#include <stdio.h> + +class TBase +{ +public: + virtual void print () = 0; + virtual ~TBase (); +}; + +class TInteger: public TBase +{ +public: + int content; + virtual void print (); + TInteger (int i); +}; + +class TString: public TBase +{ +public: + const char *content; + virtual void print (); + TString (const char *s); +}; + +void TInteger::print () +{ + printf ("Integer: %d\n", content); +} + +void TString::print () +{ + printf ("String: \"%s\"\n", content); +} + +TBase::~TBase () +{ +} + +TInteger::TInteger (int i) +{ + content = i; +} + +TString::TString (const char *s) +{ + content = s; +} + +int main (void) +{ + TBase *object[] = { new TInteger (42), + new TString ("Hello, world!"), + NULL }; + + for (int i = 0; object[i]; i++) + object[i]->print (); + + for (int i = 0; object[i]; i++) + delete object[i]; + + return 0; +} diff --git a/20250520/objects-24.cpp b/20250520/objects-24.cpp new file mode 100644 index 0000000000000000000000000000000000000000..7beb29d47c754916dfda8216010d0f5e88546f32 --- /dev/null +++ b/20250520/objects-24.cpp @@ -0,0 +1,72 @@ +#include <stdio.h> +#include <string.h> +#include <stdlib.h> + +class TBase +{ +public: + virtual void print () = 0; + virtual ~TBase (); +}; + +class TInteger: public TBase +{ +public: + int content; + virtual void print (); + TInteger (int i); +}; + +class TString: public TBase +{ +public: + char *content; + virtual void print (); + TString (const char *s); +}; + +void TInteger::print () +{ + printf ("Integer: %d\n", content); +} + +void TString::print () +{ + printf ("String: \"%s\"\n", content); +} + +TBase::~TBase () +{ +} + +TInteger::TInteger (int i) +{ + content = i; +} + +TString::TString (const char *s) +{ + if (s) + { + int l = strlen (s); + content = malloc (l + 1); + strcpy (content, s); + } + else + content = NULL; +} + +int main (void) +{ + TBase *object[] = { new TInteger (42), + new TString ("Hello, world!"), + NULL }; + + for (int i = 0; object[i]; i++) + object[i]->print (); + + for (int i = 0; object[i]; i++) + delete object[i]; + + return 0; +} diff --git a/20250520/objects-25.cpp b/20250520/objects-25.cpp new file mode 100644 index 0000000000000000000000000000000000000000..91816cd3799d0fc6b99cf09d95ad9a11cc1adda2 --- /dev/null +++ b/20250520/objects-25.cpp @@ -0,0 +1,72 @@ +#include <stdio.h> +#include <string.h> +#include <stdlib.h> + +class TBase +{ +public: + virtual void print () = 0; + virtual ~TBase (); +}; + +class TInteger: public TBase +{ +public: + int content; + virtual void print (); + TInteger (int i); +}; + +class TString: public TBase +{ +public: + char *content; + virtual void print (); + TString (const char *s); +}; + +void TInteger::print () +{ + printf ("Integer: %d\n", content); +} + +void TString::print () +{ + printf ("String: \"%s\"\n", content); +} + +TBase::~TBase () +{ +} + +TInteger::TInteger (int i) +{ + content = i; +} + +TString::TString (const char *s) +{ + if (s) + { + int l = strlen (s); + content = (char *) malloc (l + 1); + strcpy (content, s); + } + else + content = NULL; +} + +int main (void) +{ + TBase *object[] = { new TInteger (42), + new TString ("Hello, world!"), + NULL }; + + for (int i = 0; object[i]; i++) + object[i]->print (); + + for (int i = 0; object[i]; i++) + delete object[i]; + + return 0; +} diff --git a/20250520/objects-26.cpp b/20250520/objects-26.cpp new file mode 100644 index 0000000000000000000000000000000000000000..7e1ad721e8a6afa50f06fba404a2bb357c267563 --- /dev/null +++ b/20250520/objects-26.cpp @@ -0,0 +1,79 @@ +#include <stdio.h> +#include <string.h> +#include <stdlib.h> + +class TBase +{ +public: + virtual void print () = 0; + virtual ~TBase (); +}; + +class TInteger: public TBase +{ +public: + int content; + virtual void print (); + TInteger (int i); +}; + +class TString: public TBase +{ +public: + char *content; + virtual void print (); + TString (const char *s); + virtual ~TString (); +}; + +void TInteger::print () +{ + printf ("Integer: %d\n", content); +} + +void TString::print () +{ + printf ("String: \"%s\"\n", content); +} + +TBase::~TBase () +{ +} + +TInteger::TInteger (int i) +{ + content = i; +} + +TString::TString (const char *s) +{ + if (s) + { + int l = strlen (s); + content = (char *) malloc (l + 1); + strcpy (content, s); + } + else + content = NULL; +} + +TString::~TString () +{ + if (content) + free (content); +} + +int main (void) +{ + TBase *object[] = { new TInteger (42), + new TString ("Hello, world!"), + NULL }; + + for (int i = 0; object[i]; i++) + object[i]->print (); + + for (int i = 0; object[i]; i++) + delete object[i]; + + return 0; +} diff --git a/20250520/objects-27.cpp b/20250520/objects-27.cpp new file mode 100644 index 0000000000000000000000000000000000000000..ed3e7a4349b472c7b84db162b86d290de4589b04 --- /dev/null +++ b/20250520/objects-27.cpp @@ -0,0 +1,79 @@ +#include <stdio.h> +#include <string.h> +#include <stdlib.h> + +class TBase +{ +public: + virtual void print () = 0; + ~TBase (); +}; + +class TInteger: public TBase +{ +public: + int content; + virtual void print (); + TInteger (int i); +}; + +class TString: public TBase +{ +public: + char *content; + virtual void print (); + TString (const char *s); + ~TString (); +}; + +void TInteger::print () +{ + printf ("Integer: %d\n", content); +} + +void TString::print () +{ + printf ("String: \"%s\"\n", content); +} + +TBase::~TBase () +{ +} + +TInteger::TInteger (int i) +{ + content = i; +} + +TString::TString (const char *s) +{ + if (s) + { + int l = strlen (s); + content = (char *) malloc (l + 1); + strcpy (content, s); + } + else + content = NULL; +} + +TString::~TString () +{ + if (content) + free (content); +} + +int main (void) +{ + TBase *object[] = { new TInteger (42), + new TString ("Hello, world!"), + NULL }; + + for (int i = 0; object[i]; i++) + object[i]->print (); + + for (int i = 0; object[i]; i++) + delete object[i]; + + return 0; +} diff --git a/20250520/objects-28.cpp b/20250520/objects-28.cpp new file mode 100644 index 0000000000000000000000000000000000000000..3a9748ffc70f64892f2d64ed5efc05d2f68fc3f4 --- /dev/null +++ b/20250520/objects-28.cpp @@ -0,0 +1,81 @@ +#include <stdio.h> +#include <string.h> +#include <stdlib.h> + +class TBase +{ +public: + virtual void print () = 0; + ~TBase (); +}; + +class TInteger: public TBase +{ +public: + int content; + virtual void print (); + TInteger (int i); +}; + +class TString: public TBase +{ +public: + char *content; + virtual void print (); + TString (const char *s); + ~TString (); +}; + +void TInteger::print () +{ + printf ("Integer: %d\n", content); +} + +void TString::print () +{ + printf ("String: \"%s\"\n", content); +} + +TBase::~TBase () +{ + printf ("destroying TBase\n"); +} + +TInteger::TInteger (int i) +{ + content = i; +} + +TString::TString (const char *s) +{ + if (s) + { + int l = strlen (s); + content = (char *) malloc (l + 1); + strcpy (content, s); + } + else + content = NULL; +} + +TString::~TString () +{ + printf ("destroying TString\n"); + if (content) + free (content); +} + +int main (void) +{ + TBase *object[] = { new TInteger (42), + new TString ("Hello, world!"), + NULL }; + + for (int i = 0; object[i]; i++) + object[i]->print (); + + for (int i = 0; object[i]; i++) + delete object[i]; + + return 0; +} diff --git a/20250520/objects-29.cpp b/20250520/objects-29.cpp new file mode 100644 index 0000000000000000000000000000000000000000..fbb6de1d169e95d071d39b9d93b640cd08dca755 --- /dev/null +++ b/20250520/objects-29.cpp @@ -0,0 +1,81 @@ +#include <stdio.h> +#include <string.h> +#include <stdlib.h> + +class TBase +{ +public: + void print () = 0; + ~TBase (); +}; + +class TInteger: public TBase +{ +public: + int content; + void print (); + TInteger (int i); +}; + +class TString: public TBase +{ +public: + char *content; + void print (); + TString (const char *s); + ~TString (); +}; + +void TInteger::print () +{ + printf ("Integer: %d\n", content); +} + +void TString::print () +{ + printf ("String: \"%s\"\n", content); +} + +TBase::~TBase () +{ + printf ("destroying TBase\n"); +} + +TInteger::TInteger (int i) +{ + content = i; +} + +TString::TString (const char *s) +{ + if (s) + { + int l = strlen (s); + content = (char *) malloc (l + 1); + strcpy (content, s); + } + else + content = NULL; +} + +TString::~TString () +{ + printf ("destroying TString\n"); + if (content) + free (content); +} + +int main (void) +{ + TBase *object[] = { new TInteger (42), + new TString ("Hello, world!"), + NULL }; + + for (int i = 0; object[i]; i++) + object[i]->print (); + + for (int i = 0; object[i]; i++) + delete object[i]; + + return 0; +} diff --git a/20250520/objects-30.cpp b/20250520/objects-30.cpp new file mode 100644 index 0000000000000000000000000000000000000000..74fe8e734bb098e6fe89dfec276318d5ebcc99f2 --- /dev/null +++ b/20250520/objects-30.cpp @@ -0,0 +1,86 @@ +#include <stdio.h> +#include <string.h> +#include <stdlib.h> + +class TBase +{ +public: + void print (); + ~TBase (); +}; + +class TInteger: public TBase +{ +public: + int content; + void print (); + TInteger (int i); +}; + +class TString: public TBase +{ +public: + char *content; + void print (); + TString (const char *s); + ~TString (); +}; + +void TBase::print () +{ + printf ("Base\n"); +} + +TBase::~TBase () +{ + printf ("destroying TBase\n"); +} + +void TInteger::print () +{ + printf ("Integer: %d\n", content); +} + +void TString::print () +{ + printf ("String: \"%s\"\n", content); +} + +TInteger::TInteger (int i) +{ + content = i; +} + +TString::TString (const char *s) +{ + if (s) + { + int l = strlen (s); + content = (char *) malloc (l + 1); + strcpy (content, s); + } + else + content = NULL; +} + +TString::~TString () +{ + printf ("destroying TString\n"); + if (content) + free (content); +} + +int main (void) +{ + TBase *object[] = { new TInteger (42), + new TString ("Hello, world!"), + NULL }; + + for (int i = 0; object[i]; i++) + object[i]->print (); + + for (int i = 0; object[i]; i++) + delete object[i]; + + return 0; +} diff --git a/20250520/objects-31.cpp b/20250520/objects-31.cpp new file mode 100644 index 0000000000000000000000000000000000000000..3526c8eee8e18302be4e5ec5dca7277dce3e7512 --- /dev/null +++ b/20250520/objects-31.cpp @@ -0,0 +1,86 @@ +#include <stdio.h> +#include <string.h> +#include <stdlib.h> + +class TBase +{ +public: + virtual void print (); + ~TBase (); +}; + +class TInteger: public TBase +{ +public: + int content; + virtual void print (); + TInteger (int i); +}; + +class TString: public TBase +{ +public: + char *content; + virtual void print (); + TString (const char *s); + ~TString (); +}; + +void TBase::print () +{ + printf ("Base\n"); +} + +TBase::~TBase () +{ + printf ("destroying TBase\n"); +} + +void TInteger::print () +{ + printf ("Integer: %d\n", content); +} + +void TString::print () +{ + printf ("String: \"%s\"\n", content); +} + +TInteger::TInteger (int i) +{ + content = i; +} + +TString::TString (const char *s) +{ + if (s) + { + int l = strlen (s); + content = (char *) malloc (l + 1); + strcpy (content, s); + } + else + content = NULL; +} + +TString::~TString () +{ + printf ("destroying TString\n"); + if (content) + free (content); +} + +int main (void) +{ + TBase *object[] = { new TInteger (42), + new TString ("Hello, world!"), + NULL }; + + for (int i = 0; object[i]; i++) + object[i]->print (); + + for (int i = 0; object[i]; i++) + delete object[i]; + + return 0; +} diff --git a/20250520/objects-32.cpp b/20250520/objects-32.cpp new file mode 100644 index 0000000000000000000000000000000000000000..4c7cf54ba86ae1dbeb22dbddf5a747764b492911 --- /dev/null +++ b/20250520/objects-32.cpp @@ -0,0 +1,86 @@ +#include <stdio.h> +#include <string.h> +#include <stdlib.h> + +class TBase +{ +public: + virtual void print (); + virtual ~TBase (); +}; + +class TInteger: public TBase +{ +public: + int content; + virtual void print (); + TInteger (int i); +}; + +class TString: public TBase +{ +public: + char *content; + virtual void print (); + TString (const char *s); + virtual ~TString (); +}; + +void TBase::print () +{ + printf ("Base\n"); +} + +TBase::~TBase () +{ + printf ("destroying TBase\n"); +} + +void TInteger::print () +{ + printf ("Integer: %d\n", content); +} + +void TString::print () +{ + printf ("String: \"%s\"\n", content); +} + +TInteger::TInteger (int i) +{ + content = i; +} + +TString::TString (const char *s) +{ + if (s) + { + int l = strlen (s); + content = (char *) malloc (l + 1); + strcpy (content, s); + } + else + content = NULL; +} + +TString::~TString () +{ + printf ("destroying TString\n"); + if (content) + free (content); +} + +int main (void) +{ + TBase *object[] = { new TInteger (42), + new TString ("Hello, world!"), + NULL }; + + for (int i = 0; object[i]; i++) + object[i]->print (); + + for (int i = 0; object[i]; i++) + delete object[i]; + + return 0; +} diff --git a/20250520/objects-33.cpp b/20250520/objects-33.cpp new file mode 100644 index 0000000000000000000000000000000000000000..62ba31ceac13cd3677ab14adad44d540793ae2f9 --- /dev/null +++ b/20250520/objects-33.cpp @@ -0,0 +1,78 @@ +#include <stdio.h> +#include <string.h> +#include <stdlib.h> + +class TBase +{ +public: + virtual void print () + { + printf ("Base\n"); + } + + virtual ~TBase () + { + printf ("destroying TBase\n"); + } +}; + +class TInteger: public TBase +{ +public: + int content; + + virtual void print () + { + printf ("Integer: %d\n", content); + } + + TInteger (int i) + { + content = i; + } +}; + +class TString: public TBase +{ +public: + char *content; + + virtual void print () + { + printf ("String: \"%s\"\n", content); + } + + TString (const char *s) + { + if (s) + { + int l = strlen (s); + content = (char *) malloc (l + 1); + strcpy (content, s); + } + else + content = NULL; + } + + virtual ~TString () + { + printf ("destroying TString\n"); + if (content) + free (content); + } +}; + +int main (void) +{ + TBase *object[] = { new TInteger (42), + new TString ("Hello, world!"), + NULL }; + + for (int i = 0; object[i]; i++) + object[i]->print (); + + for (int i = 0; object[i]; i++) + delete object[i]; + + return 0; +} diff --git a/20250520/objects-34.cpp b/20250520/objects-34.cpp new file mode 100644 index 0000000000000000000000000000000000000000..d27665bdc509e22f34a149524aa4e838f47579ce --- /dev/null +++ b/20250520/objects-34.cpp @@ -0,0 +1,80 @@ +#include <stdio.h> +#include <string.h> +#include <stdlib.h> + +class TBase +{ +public: + virtual void print () + { + printf ("Base\n"); + } + + virtual ~TBase () + { + printf ("destroying TBase\n"); + } +}; + +class TInteger: public TBase +{ +private: + int content; + +public: + virtual void print () + { + printf ("Integer: %d\n", content); + } + + TInteger (int i) + { + content = i; + } +}; + +class TString: public TBase +{ +private: + char *content; + +public: + virtual void print () + { + printf ("String: \"%s\"\n", content); + } + + TString (const char *s) + { + if (s) + { + int l = strlen (s); + content = (char *) malloc (l + 1); + strcpy (content, s); + } + else + content = NULL; + } + + virtual ~TString () + { + printf ("destroying TString\n"); + if (content) + free (content); + } +}; + +int main (void) +{ + TBase *object[] = { new TInteger (42), + new TString ("Hello, world!"), + NULL }; + + for (int i = 0; object[i]; i++) + object[i]->print (); + + for (int i = 0; object[i]; i++) + delete object[i]; + + return 0; +} diff --git a/20250520/objects-35.cpp b/20250520/objects-35.cpp new file mode 100644 index 0000000000000000000000000000000000000000..f5070be8934876d022e3116b3dc96a2facf22002 --- /dev/null +++ b/20250520/objects-35.cpp @@ -0,0 +1,82 @@ +#include <stdio.h> +#include <string.h> +#include <stdlib.h> + +class TBase +{ +public: + virtual void print () + { + printf ("Base\n"); + } + + virtual ~TBase () + { + printf ("destroying TBase\n"); + } +}; + +class TInteger: public TBase +{ +private: + int content; + +public: + virtual void print () + { + printf ("Integer: %d\n", content); + } + + TInteger (int i) + { + content = i; + } +}; + +class TString: public TBase +{ +private: + char *content; + +public: + virtual void print () + { + printf ("String: \"%s\"\n", content); + } + + TString (const char *s) + { + if (s) + { + int l = strlen (s); + content = (char *) malloc (l + 1); + strcpy (content, s); + } + else + content = NULL; + } + + virtual ~TString () + { + printf ("destroying TString\n"); + if (content) + free (content); + } +}; + +int main (void) +{ + TBase *object[] = { new TInteger (42), + new TString ("Hello, world!"), + NULL }; + + object[0]->content = 23; + + for (int i = 0; object[i]; i++) + object[i]->print (); + + for (int i = 0; object[i]; i++) + delete object[i]; + + return 0; +} diff --git a/20250520/objects-36.cpp b/20250520/objects-36.cpp new file mode 100644 index 0000000000000000000000000000000000000000..3b6678e06a6ddfe4f5bde89e85ec3c3dbba6ca49 --- /dev/null +++ b/20250520/objects-36.cpp @@ -0,0 +1,82 @@ +#include <stdio.h> +#include <string.h> +#include <stdlib.h> + +class TBase +{ +public: + virtual void print () + { + printf ("Base\n"); + } + + virtual ~TBase () + { + printf ("destroying TBase\n"); + } +}; + +class TInteger: public TBase +{ +private: + int content; + +public: + virtual void print () + { + printf ("Integer: %d\n", content); + } + + TInteger (int i) + { + content = i; + } +}; + +class TString: public TBase +{ +private: + char *content; + +public: + virtual void print () + { + printf ("String: \"%s\"\n", content); + } + + TString (const char *s) + { + if (s) + { + int l = strlen (s); + content = (char *) malloc (l + 1); + strcpy (content, s); + } + else + content = NULL; + } + + virtual ~TString () + { + printf ("destroying TString\n"); + if (content) + free (content); + } +}; + +int main (void) +{ + TBase *object[] = { new TInteger (42), + new TString ("Hello, world!"), + NULL }; + + ((TInteger *) object[0])->content = 23; + + for (int i = 0; object[i]; i++) + object[i]->print (); + + for (int i = 0; object[i]; i++) + delete object[i]; + + return 0; +} diff --git a/20250520/objects-37.cpp b/20250520/objects-37.cpp new file mode 100644 index 0000000000000000000000000000000000000000..f40ca4b855c23d6eaca215ac62255594aed3f0dd --- /dev/null +++ b/20250520/objects-37.cpp @@ -0,0 +1,84 @@ +#include <stdio.h> +#include <string.h> +#include <stdlib.h> + +class TBase +{ +public: + virtual void print () + { + printf ("Base\n"); + } + + virtual ~TBase () + { + printf ("destroying TBase\n"); + } +}; + +class TInteger: public TBase +{ +private: + int content; + +public: + friend int main (); + + virtual void print () + { + printf ("Integer: %d\n", content); + } + + TInteger (int i) + { + content = i; + } +}; + +class TString: public TBase +{ +private: + char *content; + +public: + virtual void print () + { + printf ("String: \"%s\"\n", content); + } + + TString (const char *s) + { + if (s) + { + int l = strlen (s); + content = (char *) malloc (l + 1); + strcpy (content, s); + } + else + content = NULL; + } + + virtual ~TString () + { + printf ("destroying TString\n"); + if (content) + free (content); + } +}; + +int main (void) +{ + TBase *object[] = { new TInteger (42), + new TString ("Hello, world!"), + NULL }; + + ((TInteger *) object[0])->content = 23; + + for (int i = 0; object[i]; i++) + object[i]->print (); + + for (int i = 0; object[i]; i++) + delete object[i]; + + return 0; +} diff --git a/20250520/objects-38.cpp b/20250520/objects-38.cpp new file mode 100644 index 0000000000000000000000000000000000000000..24123f51ac722e4bf3311f9f45a7ed352663d7cd --- /dev/null +++ b/20250520/objects-38.cpp @@ -0,0 +1,90 @@ +#include <stdio.h> +#include <string.h> +#include <stdlib.h> + +class TBase +{ +public: + virtual void print () + { + printf ("Base\n"); + } + + virtual ~TBase () + { + printf ("destroying TBase\n"); + } +}; + +class TInteger: public TBase +{ +private: + int content; + +public: + friend int main (); + + virtual void print () + { + printf ("Integer: %d\n", content); + } + + TInteger (int i) + { + content = i; + } +}; + +class TString: public TBase +{ +private: + char *content; + +public: + virtual void print () + { + printf ("String: \"%s\"\n", content); + } + + static void hello () + { + printf ("Hello!\n"); + } + + TString (const char *s) + { + if (s) + { + int l = strlen (s); + content = (char *) malloc (l + 1); + strcpy (content, s); + } + else + content = NULL; + } + + virtual ~TString () + { + printf ("destroying TString\n"); + if (content) + free (content); + } +}; + +int main (void) +{ + TBase *object[] = { new TInteger (42), + new TString ("Hello, world!"), + NULL }; + + ((TInteger *) object[0])->content = 23; + ((TString *) object[1])->hello (); + + for (int i = 0; object[i]; i++) + object[i]->print (); + + for (int i = 0; object[i]; i++) + delete object[i]; + + return 0; +} diff --git a/20250520/objects-39.cpp b/20250520/objects-39.cpp new file mode 100644 index 0000000000000000000000000000000000000000..833847b613af7c814b0b3aed93c40069ce421691 --- /dev/null +++ b/20250520/objects-39.cpp @@ -0,0 +1,90 @@ +#include <stdio.h> +#include <string.h> +#include <stdlib.h> + +class TBase +{ +public: + virtual void print () + { + printf ("Base\n"); + } + + virtual ~TBase () + { + printf ("destroying TBase\n"); + } +}; + +class TInteger: public TBase +{ +private: + int content; + +public: + friend int main (); + + virtual void print () + { + printf ("Integer: %d\n", content); + } + + TInteger (int i) + { + content = i; + } +}; + +class TString: public TBase +{ +private: + char *content; + +public: + virtual void print () + { + printf ("String: \"%s\"\n", content); + } + + static void hello () + { + printf ("Hello: %s\n", content); + } + + TString (const char *s) + { + if (s) + { + int l = strlen (s); + content = (char *) malloc (l + 1); + strcpy (content, s); + } + else + content = NULL; + } + + virtual ~TString () + { + printf ("destroying TString\n"); + if (content) + free (content); + } +}; + +int main (void) +{ + TBase *object[] = { new TInteger (42), + new TString ("Hello, world!"), + NULL }; + + ((TInteger *) object[0])->content = 23; + ((TString *) object[1])->hello (); + + for (int i = 0; object[i]; i++) + object[i]->print (); + + for (int i = 0; object[i]; i++) + delete object[i]; + + return 0; +} diff --git a/20250520/objects-40.cpp b/20250520/objects-40.cpp new file mode 100644 index 0000000000000000000000000000000000000000..92ae6afeacd198c0a6c45e3006557f5833cd2b0c --- /dev/null +++ b/20250520/objects-40.cpp @@ -0,0 +1,91 @@ +#include <stdio.h> +#include <string.h> +#include <stdlib.h> + +class TBase +{ +public: + virtual void print () + { + printf ("Base\n"); + } + + virtual ~TBase () + { + printf ("destroying TBase\n"); + } +}; + +class TInteger: public TBase +{ +private: + int content; + +public: + friend int main (); + + virtual void print () + { + printf ("Integer: %d\n", content); + } + + TInteger (int i) + { + content = i; + } +}; + +class TString: public TBase +{ +private: + char *content; + +public: + virtual void print () + { + printf ("String: \"%s\"\n", content); + } + + static void hello () + { + printf ("Hello!\n"); + } + + TString (const char *s) + { + if (s) + { + int l = strlen (s); + content = (char *) malloc (l + 1); + strcpy (content, s); + } + else + content = NULL; + } + + virtual ~TString () + { + printf ("destroying TString\n"); + if (content) + free (content); + } +}; + +int main (void) +{ + TBase *object[] = { new TInteger (42), + new TString ("Hello, world!"), + NULL }; + + ((TInteger *) object[0])->content = 23; + + TString::hello (); + + for (int i = 0; object[i]; i++) + object[i]->print (); + + for (int i = 0; object[i]; i++) + delete object[i]; + + return 0; +} diff --git a/20250520/objects-41.cpp b/20250520/objects-41.cpp new file mode 100644 index 0000000000000000000000000000000000000000..cf9dd7a2bfca2829521ccadd1397ec0301cb2dad --- /dev/null +++ b/20250520/objects-41.cpp @@ -0,0 +1,92 @@ +#include <stdio.h> +#include <string.h> +#include <stdlib.h> + +class TBase +{ +public: + virtual void print () + { + printf ("Base\n"); + } + + virtual ~TBase () + { + printf ("destroying TBase\n"); + } +}; + +class TInteger: public TBase +{ +private: + int content; + +public: + friend int main (); + + virtual void print () + { + printf ("Integer: %d\n", content); + } + + TInteger (int i) + { + content = i; + } +}; + +class TString: public TBase +{ +private: + char *content; + static const char *hello_msg = "Hello!"; + +public: + virtual void print () + { + printf ("String: \"%s\"\n", content); + } + + static void hello () + { + printf ("%s\n", hello_msg); + } + + TString (const char *s) + { + if (s) + { + int l = strlen (s); + content = (char *) malloc (l + 1); + strcpy (content, s); + } + else + content = NULL; + } + + virtual ~TString () + { + printf ("destroying TString\n"); + if (content) + free (content); + } +}; + +int main (void) +{ + TBase *object[] = { new TInteger (42), + new TString ("Hello, world!"), + NULL }; + + ((TInteger *) object[0])->content = 23; + + TString::hello (); + + for (int i = 0; object[i]; i++) + object[i]->print (); + + for (int i = 0; object[i]; i++) + delete object[i]; + + return 0; +} diff --git a/20250520/objects-42.cpp b/20250520/objects-42.cpp new file mode 100644 index 0000000000000000000000000000000000000000..809f8c49f92f87dda95e0e6030583fbcbffc13aa --- /dev/null +++ b/20250520/objects-42.cpp @@ -0,0 +1,94 @@ +#include <stdio.h> +#include <string.h> +#include <stdlib.h> + +class TBase +{ +public: + virtual void print () + { + printf ("Base\n"); + } + + virtual ~TBase () + { + printf ("destroying TBase\n"); + } +}; + +class TInteger: public TBase +{ +private: + int content; + +public: + friend int main (); + + virtual void print () + { + printf ("Integer: %d\n", content); + } + + TInteger (int i) + { + content = i; + } +}; + +class TString: public TBase +{ +private: + char *content; + static const char *hello_msg; + +public: + virtual void print () + { + printf ("String: \"%s\"\n", content); + } + + static void hello () + { + printf ("%s\n", hello_msg); + } + + TString (const char *s) + { + if (s) + { + int l = strlen (s); + content = (char *) malloc (l + 1); + strcpy (content, s); + } + else + content = NULL; + } + + virtual ~TString () + { + printf ("destroying TString\n"); + if (content) + free (content); + } +}; + +static const char *TString::hello_msg = "Hello!"; + +int main (void) +{ + TBase *object[] = { new TInteger (42), + new TString ("Hello, world!"), + NULL }; + + ((TInteger *) object[0])->content = 23; + + TString::hello (); + + for (int i = 0; object[i]; i++) + object[i]->print (); + + for (int i = 0; object[i]; i++) + delete object[i]; + + return 0; +} diff --git a/20250520/objects-43.cpp b/20250520/objects-43.cpp new file mode 100644 index 0000000000000000000000000000000000000000..37d8cedf53b03a12adeeb3cd1aaf78afb5cb7139 --- /dev/null +++ b/20250520/objects-43.cpp @@ -0,0 +1,94 @@ +#include <stdio.h> +#include <string.h> +#include <stdlib.h> + +class TBase +{ +public: + virtual void print () + { + printf ("Base\n"); + } + + virtual ~TBase () + { + printf ("destroying TBase\n"); + } +}; + +class TInteger: public TBase +{ +private: + int content; + +public: + friend int main (); + + virtual void print () + { + printf ("Integer: %d\n", content); + } + + TInteger (int i) + { + content = i; + } +}; + +class TString: public TBase +{ +private: + char *content; + static const char *hello_msg; + +public: + virtual void print () + { + printf ("String: \"%s\"\n", content); + } + + static void hello () + { + printf ("%s\n", hello_msg); + } + + TString (const char *s) + { + if (s) + { + int l = strlen (s); + content = (char *) malloc (l + 1); + strcpy (content, s); + } + else + content = NULL; + } + + virtual ~TString () + { + printf ("destroying TString\n"); + if (content) + free (content); + } +}; + +const char *TString::hello_msg = "Hello!"; + +int main (void) +{ + TBase *object[] = { new TInteger (42), + new TString ("Hello, world!"), + NULL }; + + ((TInteger *) object[0])->content = 23; + + TString::hello (); + + for (int i = 0; object[i]; i++) + object[i]->print (); + + for (int i = 0; object[i]; i++) + delete object[i]; + + return 0; +} diff --git a/20250520/objects-44.cpp b/20250520/objects-44.cpp new file mode 100644 index 0000000000000000000000000000000000000000..c45af396d9e0ef5e0eb4312863ccc592d1e0964e --- /dev/null +++ b/20250520/objects-44.cpp @@ -0,0 +1,95 @@ +#include <stdio.h> +#include <string.h> +#include <stdlib.h> + +class TBase +{ +public: + virtual void print () + { + printf ("Base\n"); + } + + virtual ~TBase () + { + printf ("destroying TBase\n"); + } +}; + +class TInteger: public TBase +{ +private: + int content; + +public: + friend int main (); + + virtual void print () + { + printf ("Integer: %d\n", content); + } + + TInteger (int i) + { + content = i; + } +}; + +class TString: public TBase +{ +private: + char *content; + static const char *hello_msg; + +public: + virtual void print () + { + printf ("String: \"%s\"\n", content); + } + + static void hello () + { + printf ("%s\n", hello_msg); + } + + TString (const char *s) + { + if (s) + { + int l = strlen (s); + content = (char *) malloc (l + 1); + strcpy (content, s); + } + else + content = NULL; + } + + virtual ~TString () + { + printf ("destroying TString\n"); + if (content) + free (content); + } +}; + +const char *TString::hello_msg = "Hello!"; + +int main (void) +{ + TBase *object[] = { new TInteger (42), + new TString ("Hello, world!"), + NULL }; + + ((TInteger *) object[0])->content = 23; + + TString::hello (); + printf ("%s\n", TString::hello_msg); + + for (int i = 0; object[i]; i++) + object[i]->print (); + + for (int i = 0; object[i]; i++) + delete object[i]; + + return 0; +} diff --git a/20250520/objects-45.cpp b/20250520/objects-45.cpp new file mode 100644 index 0000000000000000000000000000000000000000..552c89a2f32b17f783eed3568cf2896e54e155d0 --- /dev/null +++ b/20250520/objects-45.cpp @@ -0,0 +1,92 @@ +#include <stdio.h> +#include <string.h> +#include <stdlib.h> + +class TBase +{ +public: + virtual void print () + { + printf ("Base\n"); + } + + virtual ~TBase () + { + printf ("destroying TBase\n"); + } +}; + +class TInteger: public TBase +{ +private: + int content; + +public: + friend int main (); + + virtual void print () + { + printf ("Integer: %d\n", content); + } + + TInteger (int i) + { + content = i; + } +}; + +class TString: public TBase +{ +private: + char *content; + static const char *hello_msg; + +public: + virtual void print () + { + printf ("String: \"%s\"\n", content); + } + + static void hello () + { + printf ("%s\n", hello_msg); + } + + TString (const char *s) + { + if (s) + { + int l = strlen (s); + content = (char *) malloc (l + 1); + strcpy (content, s); + } + else + content = NULL; + } + + virtual ~TString () + { + printf ("destroying TString\n"); + if (content) + free (content); + } +}; + +const char *TString::hello_msg = "Hello!"; + +int main (void) +{ + TBase *object[] = { new TInteger (42), + new TString ("Hello, world!"), + NULL }; + + object[0] = new TInteger (23); // Speicherleck! + + for (int i = 0; object[i]; i++) + object[i]->print (); + + for (int i = 0; object[i]; i++) + delete object[i]; + + return 0; +} diff --git a/20250520/objects-46.cpp b/20250520/objects-46.cpp new file mode 100644 index 0000000000000000000000000000000000000000..bf1004028f0c510aa8bf2aba4a666fe954c201bc --- /dev/null +++ b/20250520/objects-46.cpp @@ -0,0 +1,92 @@ +#include <stdio.h> +#include <string.h> +#include <stdlib.h> + +class TBase +{ +public: + virtual void print () + { + printf ("Base\n"); + } + + virtual ~TBase () + { + printf ("destroying TBase\n"); + } +}; + +class TInteger: public TBase +{ +private: + int content; + +public: + friend int main (); + + virtual void print () + { + printf ("Integer: %d\n", content); + } + + TInteger (int i) + { + content = i; + } +}; + +class TString: public TBase +{ +private: + char *content; + static const char *hello_msg; + +public: + virtual void print () + { + printf ("String: \"%s\"\n", content); + } + + static void hello () + { + printf ("%s\n", hello_msg); + } + + TString (const char *s) + { + if (s) + { + int l = strlen (s); + content = (char *) malloc (l + 1); + strcpy (content, s); + } + else + content = NULL; + } + + virtual ~TString () + { + printf ("destroying TString\n"); + if (content) + free (content); + } +}; + +const char *TString::hello_msg = "Hello!"; + +int main (void) +{ + TBase *object[] = { new TInteger (42), + new TString ("Hello, world!"), + NULL }; + + *((TInteger *) object[0]) = 23; + + for (int i = 0; object[i]; i++) + object[i]->print (); + + for (int i = 0; object[i]; i++) + delete object[i]; + + return 0; +} diff --git a/20250520/objects-47.cpp b/20250520/objects-47.cpp new file mode 100644 index 0000000000000000000000000000000000000000..fd8319e7c570b89dc81180ea923afd6d173130fe --- /dev/null +++ b/20250520/objects-47.cpp @@ -0,0 +1,90 @@ +#include <stdio.h> +#include <string.h> +#include <stdlib.h> + +class TBase +{ +public: + virtual void print () + { + printf ("Base\n"); + } + + virtual ~TBase () + { + printf ("destroying TBase\n"); + } +}; + +class TInteger: public TBase +{ +private: + int content; + +public: + friend int main (); + + virtual void print () + { + printf ("Integer: %d\n", content); + } + + TInteger (int i) + { + content = i; + } +}; + +class TString: public TBase +{ +private: + char *content; + static const char *hello_msg; + +public: + virtual void print () + { + printf ("String: \"%s\"\n", content); + } + + static void hello () + { + printf ("%s\n", hello_msg); + } + + TString (const char *s) + { + if (s) + { + int l = strlen (s); + content = (char *) malloc (l + 1); + strcpy (content, s); + } + else + content = NULL; + } + + virtual ~TString () + { + printf ("destroying TString\n"); + if (content) + free (content); + } +}; + +const char *TString::hello_msg = "Hello!"; + +int main (void) +{ + TInteger i = 42; + TString s = "Hello, world!"; + TBase *object[] = { &i, &s, NULL }; + + for (int i = 0; object[i]; i++) + object[i]->print (); + + for (int i = 0; object[i]; i++) + delete object[i]; + + return 0; +} diff --git a/20250520/objects-48.cpp b/20250520/objects-48.cpp new file mode 100644 index 0000000000000000000000000000000000000000..27dc6ce73e911a1e1b0c59c60f2ec3b08edfb926 --- /dev/null +++ b/20250520/objects-48.cpp @@ -0,0 +1,87 @@ +#include <stdio.h> +#include <string.h> +#include <stdlib.h> + +class TBase +{ +public: + virtual void print () + { + printf ("Base\n"); + } + + virtual ~TBase () + { + printf ("destroying TBase\n"); + } +}; + +class TInteger: public TBase +{ +private: + int content; + +public: + friend int main (); + + virtual void print () + { + printf ("Integer: %d\n", content); + } + + TInteger (int i) + { + content = i; + } +}; + +class TString: public TBase +{ +private: + char *content; + static const char *hello_msg; + +public: + virtual void print () + { + printf ("String: \"%s\"\n", content); + } + + static void hello () + { + printf ("%s\n", hello_msg); + } + + TString (const char *s) + { + if (s) + { + int l = strlen (s); + content = (char *) malloc (l + 1); + strcpy (content, s); + } + else + content = NULL; + } + + virtual ~TString () + { + printf ("destroying TString\n"); + if (content) + free (content); + } +}; + +const char *TString::hello_msg = "Hello!"; + +int main (void) +{ + TInteger i = 42; + TString s = "Hello, world!"; + TBase *object[] = { &i, &s, NULL }; + + for (int i = 0; object[i]; i++) + object[i]->print (); + + return 0; +} diff --git a/20250520/objects-49.cpp b/20250520/objects-49.cpp new file mode 100644 index 0000000000000000000000000000000000000000..1cda00386427369552cea54205501be163bc1cff --- /dev/null +++ b/20250520/objects-49.cpp @@ -0,0 +1,87 @@ +#include <stdio.h> +#include <string.h> +#include <stdlib.h> + +class TBase +{ +public: + virtual void print () + { + printf ("Base\n"); + } + + virtual ~TBase () + { + printf ("destroying TBase\n"); + } +}; + +class TInteger: public TBase +{ +private: + int content; + +public: + friend int main (); + + virtual void print () + { + printf ("Integer: %d\n", content); + } + + TInteger (int i) + { + content = i; + } +}; + +class TString: public TBase +{ +private: + char *content; + static const char *hello_msg; + +public: + virtual void print () + { + printf ("String: \"%s\"\n", content); + } + + static void hello () + { + printf ("%s\n", hello_msg); + } + + TString (const char *s) + { + if (s) + { + int l = strlen (s); + content = (char *) malloc (l + 1); + strcpy (content, s); + } + else + content = NULL; + } + + virtual ~TString () + { + printf ("destroying TString\n"); + if (content) + free (content); + } +}; + +const char *TString::hello_msg = "Hello!"; + +int main (void) +{ + TInteger i = 42; + TString s = "Hello, world!"; + TBase &object[] = { i, s }; + + for (int i = 0; i < 2; i++) + object[i].print (); + + return 0; +} diff --git a/20250520/references-01a.c b/20250520/references-01a.c new file mode 100644 index 0000000000000000000000000000000000000000..e9ae27ad141e8274cdefde90f334b37f99cde01d --- /dev/null +++ b/20250520/references-01a.c @@ -0,0 +1,14 @@ +#include <stdio.h> + +void calc_answer (int *answer) +{ + *answer = 42; +} + +int main (void) +{ + int answer; + calc_answer (answer); + printf ("The answer is: %d\n", answer); + return 0; +} diff --git a/20250520/references-01a.s b/20250520/references-01a.s new file mode 100644 index 0000000000000000000000000000000000000000..f410592f18013070f713b31646db01697af6d67a --- /dev/null +++ b/20250520/references-01a.s @@ -0,0 +1,38 @@ + .file "references-01a.c" + .text + .p2align 4 + .globl calc_answer + .type calc_answer, @function +calc_answer: +.LFB11: + .cfi_startproc + movl $42, (%rdi) + ret + .cfi_endproc +.LFE11: + .size calc_answer, .-calc_answer + .section .rodata.str1.1,"aMS",@progbits,1 +.LC0: + .string "The answer is: %d\n" + .section .text.startup,"ax",@progbits + .p2align 4 + .globl main + .type main, @function +main: +.LFB12: + .cfi_startproc + subq $8, %rsp + .cfi_def_cfa_offset 16 + xorl %esi, %esi + leaq .LC0(%rip), %rdi + xorl %eax, %eax + call printf@PLT + xorl %eax, %eax + addq $8, %rsp + .cfi_def_cfa_offset 8 + ret + .cfi_endproc +.LFE12: + .size main, .-main + .ident "GCC: (Debian 12.2.0-14) 12.2.0" + .section .note.GNU-stack,"",@progbits diff --git a/20250520/strings-01.cpp b/20250520/strings-01.cpp new file mode 100644 index 0000000000000000000000000000000000000000..d0624e4fecf5362f51a76f3b007187b6890adfa7 --- /dev/null +++ b/20250520/strings-01.cpp @@ -0,0 +1,9 @@ +#include <iostream> +#include <string> + +int main () +{ + std::string hello = "Hello, world!"; + std::cout << hello << std::endl; + return 0; +} diff --git a/20250520/strings-02.cpp b/20250520/strings-02.cpp new file mode 100644 index 0000000000000000000000000000000000000000..8f4c771bd3685fba9e8dcc16f517447af5aaa6ac --- /dev/null +++ b/20250520/strings-02.cpp @@ -0,0 +1,9 @@ +#include <stdio.h> +#include <string> + +int main () +{ + std::string hello = "Hello, world!"; + printf ("%s\n", hello); + return 0; +} diff --git a/20250520/strings-03.cpp b/20250520/strings-03.cpp new file mode 100644 index 0000000000000000000000000000000000000000..e1c7960a0ebdeb92b8ca014fa383868e6280316b --- /dev/null +++ b/20250520/strings-03.cpp @@ -0,0 +1,9 @@ +#include <stdio.h> +#include <string> + +int main () +{ + std::string hello = "Hello, world!"; + printf ("%s\n", hello.c_str ()); + return 0; +} diff --git a/20250520/strings-04.cpp b/20250520/strings-04.cpp new file mode 100644 index 0000000000000000000000000000000000000000..c7bb588c5d4ac4c92f91226386d880ef4dc0876b --- /dev/null +++ b/20250520/strings-04.cpp @@ -0,0 +1,11 @@ +#include <iostream> +#include <string> +#include <sstream> + +int main () +{ + std::ostringstream s; + s << "The answer is: " << 42 << std::endl; + std::cout << s.str (); + return 0; +} diff --git a/20250520/strings-05.cpp b/20250520/strings-05.cpp new file mode 100644 index 0000000000000000000000000000000000000000..896a0c0f99231ed438d9845174567456d48b599c --- /dev/null +++ b/20250520/strings-05.cpp @@ -0,0 +1,11 @@ +#include <stdio.h> +#include <string> + +int main () +{ + char buffer[42]; + sprintf (buffer, "The answer is: %d\n", 42); + std::string s = buffer; + printf ("%s", s.c_str ()); + return 0; +} diff --git a/README.md b/README.md index a656215cb40da0854c7da0d0bf251e0ddb240438..60d888565c2365a8838becb72ca35ee20ea1a68a 100644 --- a/README.md +++ b/README.md @@ -23,7 +23,7 @@ Vortragsfolien und Beispiele: * [29.04.2025: Quaternionen, Diffie-Hellman, ElGamal](https://gitlab.cvh-server.de/pgerwinski/ad/raw/2025ss/20250429/ad-20250429.pdf) [**(Beispiele)**](https://gitlab.cvh-server.de/pgerwinski/ad/tree/2025ss/20250429/) * [06.05.2025: Parität, CRC, Hadamard-Code, RAID](https://gitlab.cvh-server.de/pgerwinski/ad/raw/2025ss/20250506/ad-20250506.pdf) [**(Beispiele)**](https://gitlab.cvh-server.de/pgerwinski/ad/tree/2025ss/20250506/) * [13.05.2025: Einführung in C++](https://gitlab.cvh-server.de/pgerwinski/ad/raw/2025ss/20250513/ad-20250513.pdf) [**(Beispiele)**](https://gitlab.cvh-server.de/pgerwinski/ad/tree/2025ss/20250513/) - * [20.05.2025: C++: Objekte, Strings, Templates, …](https://gitlab.cvh-server.de/pgerwinski/ad/raw/2025ss/20250520/ad-20250520.pdf) [**(Beispiele)**](https://gitlab.cvh-server.de/pgerwinski/ad/tree/2025ss/20250520/) + * [20.05.2025: C++: Objekte, Strings](https://gitlab.cvh-server.de/pgerwinski/ad/raw/2025ss/20250520/ad-20250520.pdf) [**(Beispiele)**](https://gitlab.cvh-server.de/pgerwinski/ad/tree/2025ss/20250520/) * [alle in 1 Datei](https://gitlab.cvh-server.de/pgerwinski/ad/raw/2025ss/ad-slides-2025ss.pdf) Tafelbilder: diff --git a/ad-slides-2025ss.pdf b/ad-slides-2025ss.pdf index b6c7290eec7438bb0bcaf372bccd95cb31e8347c..058ebdeff9edb1cf00303475555107bbdf875d38 100644 Binary files a/ad-slides-2025ss.pdf and b/ad-slides-2025ss.pdf differ diff --git a/ad-slides-2025ss.tex b/ad-slides-2025ss.tex index d788ae5c6c21117250b7978224042dc79958f270..4bdf930f16ec278b6d99081f7ff0904fc35cc0c8 100644 --- a/ad-slides-2025ss.tex +++ b/ad-slides-2025ss.tex @@ -22,6 +22,6 @@ \includepdf[pages=-]{20250506/ad-20250506.pdf} \pdfbookmark[1]{13.05.2025: Einführung in C++}{20250513} \includepdf[pages=-]{20250513/ad-20250513.pdf} - \pdfbookmark[1]{20.05.2025: C++: Objekte}{20250520} + \pdfbookmark[1]{20.05.2025: C++: Objekte, Strings}{20250520} \includepdf[pages=-]{20250520/ad-20250520.pdf} \end{document}