From 50100fea75b5d490f36d306174099218dbdb2759 Mon Sep 17 00:00:00 2001 From: Peter Gerwinski <peter.gerwinski@hs-bochum.de> Date: Wed, 19 Apr 2023 21:48:21 +0200 Subject: [PATCH] =?UTF-8?q?Erg=C3=A4nzung=20(13.4.2023)=20der=20Beispiele?= =?UTF-8?q?=20vom=206.4.2023?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- 20230406/destructors-03a.cpp | 45 +++++++++++++++++++++++++++++ 20230406/destructors-03b.cpp | 46 +++++++++++++++++++++++++++++ 20230406/destructors-03c.cpp | 55 +++++++++++++++++++++++++++++++++++ 20230406/destructors-03d.cpp | 55 +++++++++++++++++++++++++++++++++++ 20230406/destructors-03e.cpp | 56 ++++++++++++++++++++++++++++++++++++ 20230406/iterators-08a.cpp | 14 +++++++++ 20230406/iterators-09a.cpp | 14 +++++++++ 20230406/iterators-09b.cpp | 14 +++++++++ 8 files changed, 299 insertions(+) create mode 100644 20230406/destructors-03a.cpp create mode 100644 20230406/destructors-03b.cpp create mode 100644 20230406/destructors-03c.cpp create mode 100644 20230406/destructors-03d.cpp create mode 100644 20230406/destructors-03e.cpp create mode 100644 20230406/iterators-08a.cpp create mode 100644 20230406/iterators-09a.cpp create mode 100644 20230406/iterators-09b.cpp diff --git a/20230406/destructors-03a.cpp b/20230406/destructors-03a.cpp new file mode 100644 index 0000000..65975a0 --- /dev/null +++ b/20230406/destructors-03a.cpp @@ -0,0 +1,45 @@ +#include <stdio.h> +#include <string.h> +#include <stdlib.h> + +class TString +{ +private: + char *content; +public: + void print () + { + printf ("%s\n", content); + } + TString (const char *s = "Hello, world!") + { + printf ("Constructing TString object\n"); + content = (char *) malloc (strlen (s)); + strcpy (content, s); + } + virtual ~TString () + { + printf ("Destructing TString object\n"); + free (content); + content = NULL; + } +}; + +int bye () +{ + printf ("Bye!\n"); + return 0; +} + +void do_something () +{ + TString s = "Hallo, Welt!"; + s.print (); +} + +int main () +{ + for (int i = 0; i < 1000; i++) + do_something (); + return bye (); +} diff --git a/20230406/destructors-03b.cpp b/20230406/destructors-03b.cpp new file mode 100644 index 0000000..99aa19e --- /dev/null +++ b/20230406/destructors-03b.cpp @@ -0,0 +1,46 @@ +#include <stdio.h> +#include <string.h> +#include <stdlib.h> + +class TString +{ +private: + char *content; +public: + void print () + { + printf ("%s\n", content); + } + TString (const char *s = "Hello, world!") + { + printf ("Constructing TString object\n"); + content = (char *) malloc (strlen (s)); + strcpy (content, s); + } + virtual ~TString () + { + printf ("Destructing TString object\n"); + free (content); + content = NULL; + } +}; + +int bye () +{ + printf ("Bye!\n"); + return 0; +} + +void do_something () +{ + TString *s = new TString ("Hallo, Welt!"); + s->print (); + delete s; +} + +int main () +{ + for (int i = 0; i < 1000; i++) + do_something (); + return bye (); +} diff --git a/20230406/destructors-03c.cpp b/20230406/destructors-03c.cpp new file mode 100644 index 0000000..e7b836c --- /dev/null +++ b/20230406/destructors-03c.cpp @@ -0,0 +1,55 @@ +#include <stdio.h> +#include <string.h> +#include <stdlib.h> + +class TString +{ +private: + char *content; +public: + void print () + { + printf ("%s\n", content); + } + TString (const char *s = "Hello, world!") + { + printf ("Constructing TString object\n"); + content = (char *) malloc (strlen (s)); + strcpy (content, s); + } + virtual ~TString () + { + printf ("Destructing TString object\n"); + free (content); + content = NULL; + } +}; + +int bye () +{ + printf ("Bye!\n"); + return 0; +} + +int very_important_condition () +{ + return 1; +} + +void do_something () +{ + printf ("Hi!\n"); + if (very_important_condition ()) + { + TString s = "Hallo, Welt!"; + s.print (); + } + printf ("Bye?\n"); +} + +int main () +{ + for (int i = 0; i < 1; i++) + do_something (); + return bye (); +} diff --git a/20230406/destructors-03d.cpp b/20230406/destructors-03d.cpp new file mode 100644 index 0000000..1fdd5c3 --- /dev/null +++ b/20230406/destructors-03d.cpp @@ -0,0 +1,55 @@ +#include <stdio.h> +#include <string.h> +#include <stdlib.h> + +class TString +{ +private: + char *content; +public: + void print () + { + printf ("%s\n", content); + } + TString (const char *s = "Hello, world!") + { + printf ("Constructing TString object\n"); + content = (char *) malloc (strlen (s)); + strcpy (content, s); + } + virtual ~TString () + { + printf ("Destructing TString object\n"); + free (content); + content = NULL; + } +}; + +int bye () +{ + printf ("Bye!\n"); + return 0; +} + +int very_important_condition () +{ + return 1; +} + +void do_something () +{ + printf ("Hi!\n"); + if (very_important_condition ()) + { + TString *s = new TString ("Hallo, Welt!"); + s->print (); + } + printf ("Bye?\n"); +} + +int main () +{ + for (int i = 0; i < 1; i++) + do_something (); + return bye (); +} diff --git a/20230406/destructors-03e.cpp b/20230406/destructors-03e.cpp new file mode 100644 index 0000000..ef5ec6e --- /dev/null +++ b/20230406/destructors-03e.cpp @@ -0,0 +1,56 @@ +#include <stdio.h> +#include <string.h> +#include <stdlib.h> + +class TString +{ +private: + char *content; +public: + void print () + { + printf ("%s\n", content); + } + TString (const char *s = "Hello, world!") + { + printf ("Constructing TString object\n"); + content = (char *) malloc (strlen (s)); + strcpy (content, s); + } + virtual ~TString () + { + printf ("Destructing TString object\n"); + free (content); + content = NULL; + } +}; + +int bye () +{ + printf ("Bye!\n"); + return 0; +} + +int very_important_condition () +{ + return 1; +} + +void do_something () +{ + printf ("Hi!\n"); + if (very_important_condition ()) + { + TString *s = new TString ("Hallo, Welt!"); + s->print (); + delete s; + } + printf ("Bye?\n"); +} + +int main () +{ + for (int i = 0; i < 1; i++) + do_something (); + return bye (); +} diff --git a/20230406/iterators-08a.cpp b/20230406/iterators-08a.cpp new file mode 100644 index 0000000..ccfc45e --- /dev/null +++ b/20230406/iterators-08a.cpp @@ -0,0 +1,14 @@ +#include <iostream> +#include <vector> + +int main () +{ + std::vector <int> prime = { { 2, 3, 5, 7, 11 } }; + for (auto *p : prime) + std::cout << *p << std::endl; + prime.push_back (13); + prime.push_back (17); + for (auto *p : prime) + std::cout << *p << std::endl; + return 0; +} diff --git a/20230406/iterators-09a.cpp b/20230406/iterators-09a.cpp new file mode 100644 index 0000000..81ec53d --- /dev/null +++ b/20230406/iterators-09a.cpp @@ -0,0 +1,14 @@ +#include <iostream> +#include <set> + +int main () +{ + std::set <int> prime = { { 2, 3, 5, 7, 11 } }; + for (auto &p : prime) + std::cout << p << std::endl; + prime.push_back (13); + prime.push_back (17); + for (auto &p : prime) + std::cout << p << std::endl; + return 0; +} diff --git a/20230406/iterators-09b.cpp b/20230406/iterators-09b.cpp new file mode 100644 index 0000000..b232c54 --- /dev/null +++ b/20230406/iterators-09b.cpp @@ -0,0 +1,14 @@ +#include <iostream> +#include <set> + +int main () +{ + std::set <int> prime = { { 2, 3, 5, 7, 11 } }; + for (auto &p : prime) + std::cout << p << std::endl; + prime.insert (prime.end (), 13); + prime.insert (prime.end (), 17); + for (auto &p : prime) + std::cout << p << std::endl; + return 0; +} -- GitLab