diff --git a/20210128/hp-20210128.pdf b/20210128/hp-20210128.pdf index fb8d32701478cbfa971c264403c555d07f5d54f9..2f7012a249447a82c6d97afb9afd8378019fdefa 100644 Binary files a/20210128/hp-20210128.pdf and b/20210128/hp-20210128.pdf differ diff --git a/20210128/hp-20210128.tex b/20210128/hp-20210128.tex index 87af238337904a716caf91085ab1c9a97f7afa13..f291a0036f102c6dd7f28748db0e62e580a0745c 100644 --- a/20210128/hp-20210128.tex +++ b/20210128/hp-20210128.tex @@ -909,7 +909,7 @@ \item Zeiger auf verschiedene Strukturen\\ mit einem gemeinsamen Anteil von Datenfeldern\\ - \textarrow\ "`verwandte"' \newterm{Objekte}, \newterm{Klassen} von Objekten + \textarrow\ "`verwandte"' \newterm{Objekte}, \newterm{Klassenhierarchie} von Objekten \item Struktur, die \emph{nur\/} den gemeinsamen Anteil enthält\\ \textarrow\ "`Vorfahr"', \newterm{Basisklasse}, \newterm{Vererbung} diff --git a/20210128/objects-1.c b/20210128/objects-1.c new file mode 100644 index 0000000000000000000000000000000000000000..1a628668fd7b8fb3a0d9886ac14e8e909bc23793 --- /dev/null +++ b/20210128/objects-1.c @@ -0,0 +1,28 @@ +#include <stdio.h> + +typedef struct +{ + int type; +} t_base; + +typedef struct +{ + int type; + int content; +} t_integer; + +typedef struct +{ + int type; + char *content; +} t_string; + +int main (void) +{ + t_integer i = { 1, 42 }; + t_string s = { 2, "Hello, world!" }; + + t_base *object[] = { &i, &s }; + + return 0; +} diff --git a/20210128/objects-2.c b/20210128/objects-2.c new file mode 100644 index 0000000000000000000000000000000000000000..a47cfb4276085399afb86795d04b1f6ae20c95bf --- /dev/null +++ b/20210128/objects-2.c @@ -0,0 +1,28 @@ +#include <stdio.h> + +typedef struct +{ + int type; +} t_base; + +typedef struct +{ + int type; + int content; +} t_integer; + +typedef struct +{ + int type; + char *content; +} t_string; + +int main (void) +{ + t_integer i = { 1, 42 }; + t_string s = { 2, "Hello, world!" }; + + t_base *object[] = { (t_base *) &i, (t_base *) &s }; + + return 0; +} diff --git a/20210128/objects-3.c b/20210128/objects-3.c new file mode 100644 index 0000000000000000000000000000000000000000..ff9224c0767ccad39f5b1396720ee73b6a0455fb --- /dev/null +++ b/20210128/objects-3.c @@ -0,0 +1,34 @@ +#include <stdio.h> + +typedef struct +{ + int type; +} t_base; + +typedef struct +{ + int type; + int content; +} t_integer; + +typedef struct +{ + int type; + char *content; +} t_string; + +int main (void) +{ + t_integer i = { 1, 42 }; + t_string s = { 2, "Hello, world!" }; + + t_base *object[] = { (t_base *) &i, (t_base *) &s }; + + for (int i = 0; i < 2; i++) + if (object[i]->type == 1) + printf ("Integer: %d\n", object[i]->content); + else if (object[i]->type == 2) + printf ("String: \"%s\"\n", object[i]->content); + + return 0; +} diff --git a/20210128/objects-3a.c b/20210128/objects-3a.c new file mode 100644 index 0000000000000000000000000000000000000000..a285a87d72694f5259a8a736ab448383e7004580 --- /dev/null +++ b/20210128/objects-3a.c @@ -0,0 +1,34 @@ +#include <stdio.h> + +typedef struct +{ + int type; +} t_base; + +typedef struct +{ + int type; + int content; +} t_integer; + +typedef struct +{ + int type; + char *content; +} t_string; + +int main (void) +{ + t_integer i = { 1, 42 }; + t_string s = { 2, "Hello, world!" }; + + t_base *object[] = { &i, &s }; + + for (int i = 0; i < 2; i++) + if (object[i]->type == 1) + printf ("Integer: %d\n", object[i]->content); + else if (object[i]->type == 2) + printf ("String: \"%s\"\n", object[i]->content); + + return 0; +} diff --git a/20210128/objects-4.c b/20210128/objects-4.c new file mode 100644 index 0000000000000000000000000000000000000000..ef7bffe80471d4b014258824421dce0557fc41dd --- /dev/null +++ b/20210128/objects-4.c @@ -0,0 +1,34 @@ +#include <stdio.h> + +typedef struct +{ + int type; +} t_base; + +typedef struct +{ + int type; + int content; +} t_integer; + +typedef struct +{ + int type; + char *content; +} t_string; + +int main (void) +{ + t_integer i = { 1, 42 }; + t_string s = { 2, "Hello, world!" }; + + t_base *object[] = { (t_base *) &i, (t_base *) &s }; + + for (int i = 0; i < 2; i++) + if (object[i]->type == 1) + printf ("Integer: %d\n", (t_integer *) object[i]->content); + else if (object[i]->type == 2) + printf ("String: \"%s\"\n", (t_string *) object[i]->content); + + return 0; +} diff --git a/20210128/objects-5.c b/20210128/objects-5.c new file mode 100644 index 0000000000000000000000000000000000000000..820181d87e2a04b81cd2e03aa7980d970cd6c1a6 --- /dev/null +++ b/20210128/objects-5.c @@ -0,0 +1,34 @@ +#include <stdio.h> + +typedef struct +{ + int type; +} t_base; + +typedef struct +{ + int type; + int content; +} t_integer; + +typedef struct +{ + int type; + char *content; +} t_string; + +int main (void) +{ + t_integer i = { 1, 42 }; + t_string s = { 2, "Hello, world!" }; + + t_base *object[] = { (t_base *) &i, (t_base *) &s }; + + for (int i = 0; i < 2; i++) + if (object[i]->type == 1) + printf ("Integer: %d\n", ((t_integer *) object[i])->content); + else if (object[i]->type == 2) + printf ("String: \"%s\"\n", ((t_string *) object[i])->content); + + return 0; +} diff --git a/20210128/objects-5a.c b/20210128/objects-5a.c new file mode 100644 index 0000000000000000000000000000000000000000..14f1cadd3a7d0954abdd844249d73656322c9e89 --- /dev/null +++ b/20210128/objects-5a.c @@ -0,0 +1,35 @@ +#include <stdio.h> + +typedef struct +{ + int nutzlos; + int type; +} t_base; + +typedef struct +{ + int content; + int type; +} t_integer; + +typedef struct +{ + char *content; + int type; +} t_string; + +int main (void) +{ + t_integer i = { 42, 1 }; + t_string s = { "Hello, world!", 2 }; + + t_base *object[] = { (t_base *) &i, (t_base *) &s }; + + for (int i = 0; i < 2; i++) + if (object[i]->type == 1) + printf ("Integer: %d\n", ((t_integer *) object[i])->content); + else if (object[i]->type == 2) + printf ("String: \"%s\"\n", ((t_string *) object[i])->content); + + return 0; +} diff --git a/20210128/objects-6.c b/20210128/objects-6.c new file mode 100644 index 0000000000000000000000000000000000000000..86847c9085083e2d5093026619550fb272b5f4ec --- /dev/null +++ b/20210128/objects-6.c @@ -0,0 +1,39 @@ +#include <stdio.h> + +typedef struct +{ + int type; +} t_base; + +typedef struct +{ + int type; + int content; +} t_integer; + +typedef struct +{ + int type; + char *content; +} t_string; + +void print_object (t_base *this) +{ + if (this->type == 1) + printf ("Integer: %d\n", ((t_integer *) this)->content); + else if (this->type == 2) + printf ("String: \"%s\"\n", ((t_string *) this)->content); +} + +int main (void) +{ + t_integer i = { 1, 42 }; + t_string s = { 2, "Hello, world!" }; + + t_base *object[] = { (t_base *) &i, (t_base *) &s }; + + for (int i = 0; i < 2; i++) + print_object (object[i]); + + return 0; +} diff --git a/20210128/objects-7.c b/20210128/objects-7.c new file mode 100644 index 0000000000000000000000000000000000000000..b9b24ad65f650aeda194e4570ae853e1a5ad8f11 --- /dev/null +++ b/20210128/objects-7.c @@ -0,0 +1,43 @@ +#include <stdio.h> + +#define T_BASE 0 +#define T_INTEGER 1 +#define T_STRING 2 + +typedef struct +{ + int type; +} t_base; + +typedef struct +{ + int type; + int content; +} t_integer; + +typedef struct +{ + int type; + char *content; +} t_string; + +void print_object (t_base *this) +{ + if (this->type == T_INTEGER) + printf ("Integer: %d\n", ((t_integer *) this)->content); + else if (this->type == T_STRING) + printf ("String: \"%s\"\n", ((t_string *) this)->content); +} + +int main (void) +{ + t_integer i = { T_INTEGER, 42 }; + t_string s = { T_STRING, "Hello, world!" }; + + t_base *object[] = { (t_base *) &i, (t_base *) &s, NULL }; + + for (int i = 0; object[i]; i++) + print_object (object[i]); + + return 0; +} diff --git a/20210128/objects-8.c b/20210128/objects-8.c new file mode 100644 index 0000000000000000000000000000000000000000..0c93e8f6c3c4b5e504c758dfa87b8510e98c0692 --- /dev/null +++ b/20210128/objects-8.c @@ -0,0 +1,59 @@ +#include <stdio.h> +#include <stdlib.h> + +#define T_BASE 0 +#define T_INTEGER 1 +#define T_STRING 2 + +typedef struct +{ + int type; +} t_base; + +typedef struct +{ + int type; + int content; +} t_integer; + +typedef struct +{ + int type; + char *content; +} t_string; + +void print_object (t_base *this) +{ + if (this->type == T_INTEGER) + printf ("Integer: %d\n", ((t_integer *) this)->content); + else if (this->type == T_STRING) + printf ("String: \"%s\"\n", ((t_string *) this)->content); +} + +t_integer *new_integer (int i) +{ + t_integer *p = malloc (sizeof (t_integer)); + p->type = T_INTEGER; + p->content = i; + return p; +} + +t_string *new_string (char *s) +{ + t_string *p = malloc (sizeof (t_string)); + p->type = T_STRING; + p->content = s; + return p; +} + +int main (void) +{ + t_base *object[] = { (t_base *) new_integer (42), + (t_base *) new_string ("Hello, world!"), + NULL }; + + for (int i = 0; object[i]; i++) + print_object (object[i]); + + return 0; +} diff --git a/20210128/objects-8a.c b/20210128/objects-8a.c new file mode 100644 index 0000000000000000000000000000000000000000..76c3cc56ab586e07d5342c4421e7e77be1ead15d --- /dev/null +++ b/20210128/objects-8a.c @@ -0,0 +1,61 @@ +#include <stdio.h> +#include <stdlib.h> + +#define T_BASE 0 +#define T_INTEGER 1 +#define T_STRING 2 + +typedef struct +{ + int type; +} t_base; + +typedef struct +{ + int type; + int content; +} t_integer; + +typedef struct +{ + int type; + char *content; +} t_string; + +char *content = "Ätsch."; + +void print_object (t_base *this) +{ + if (this->type == T_INTEGER) + printf ("Integer: %d\n", ((t_integer *) this)->content); + else if (this->type == T_STRING) + printf ("String: \"%s\"\n", ((t_string *) this)->content); +} + +t_integer *new_integer (int i) +{ + t_integer *p = malloc (sizeof (t_integer)); + p->type = T_INTEGER; + p->content = i; + return p; +} + +t_string *new_string (char *s) +{ + t_string *p = malloc (sizeof (t_string)); + p->type = T_STRING; + p->content = s; + return p; +} + +int main (void) +{ + t_base *object[] = { (t_base *) new_integer (42), + (t_base *) new_string ("Hello, world!"), + NULL }; + + for (int i = 0; object[i]; i++) + print_object (object[i]); + + return 0; +} diff --git a/20210128/objects-8b.c b/20210128/objects-8b.c new file mode 100644 index 0000000000000000000000000000000000000000..a9d7b5a296a8fb0c822ae0274773dbe6f1c372c7 --- /dev/null +++ b/20210128/objects-8b.c @@ -0,0 +1,59 @@ +#include <stdio.h> +#include <stdlib.h> + +#define T_BASE 0 +#define T_INTEGER 1 +#define T_STRING 2 + +typedef struct +{ + int type; +} t_base; + +typedef struct +{ + int type; + int content; +} t_integer; + +typedef struct +{ + int type; + char *content; +} t_string; + +void print_object (t_base *this) +{ + if (this->type == T_INTEGER) + printf ("Integer: %d\n", ((t_integer *) this)->content); + else if (this->type == T_STRING) + printf ("String: \"%s\"\n", ((t_string *) this)->content); +} + +t_base *new_integer (int i) +{ + t_integer *p = malloc (sizeof (t_integer)); + p->type = T_INTEGER; + p->content = i; + return (t_base *) p; +} + +t_base *new_string (char *s) +{ + t_string *p = malloc (sizeof (t_string)); + p->type = T_STRING; + p->content = s; + return (t_base *) p; +} + +int main (void) +{ + t_base *object[] = { new_integer (42), + new_string ("Hello, world!"), + NULL }; + + for (int i = 0; object[i]; i++) + print_object (object[i]); + + return 0; +} diff --git a/20210128/objects-9.c b/20210128/objects-9.c new file mode 100644 index 0000000000000000000000000000000000000000..41468b043cd69b1c19b2c4456eb937ff34dbe9f8 --- /dev/null +++ b/20210128/objects-9.c @@ -0,0 +1,66 @@ +#include <stdio.h> +#include <stdlib.h> + +#define T_BASE 0 +#define T_INTEGER 1 +#define T_STRING 2 + +typedef struct +{ + int type; +} t_base; + +typedef struct +{ + int type; + int content; +} t_integer; + +typedef struct +{ + int type; + char *content; +} t_string; + +typedef union +{ + t_base base; + t_integer integer; + t_string string; +} t_object; + +void print_object (t_object *this) +{ + if (this->base.type == T_INTEGER) + printf ("Integer: %d\n", this->integer.content); + else if (this->base.type == T_STRING) + printf ("String: \"%s\"\n", this->string.content); +} + +t_object *new_integer (int i) +{ + t_object *p = malloc (sizeof (t_integer)); + p->integer.type = T_INTEGER; + p->integer.content = i; + return p; +} + +t_object *new_string (char *s) +{ + t_object *p = malloc (sizeof (t_string)); + p->string.type = T_STRING; + p->string.content = s; + return p; +} + +int main (void) +{ + t_object *object[] = { new_integer (42), + new_string ("Hello, world!"), + NULL }; + + for (int i = 0; object[i]; i++) + print_object (object[i]); + + return 0; +} diff --git a/20210128/objects-9a.c b/20210128/objects-9a.c new file mode 100644 index 0000000000000000000000000000000000000000..46431c9dbd4366c8175a6e3284a271080f175822 --- /dev/null +++ b/20210128/objects-9a.c @@ -0,0 +1,68 @@ +#include <stdio.h> +#include <stdlib.h> + +#define T_BASE 0 +#define T_INTEGER 1 +#define T_STRING 2 + +typedef struct +{ + int type; +} t_base; + +typedef struct +{ + int type; + int content; +} t_integer; + +typedef struct +{ + int type; + char *content; +} t_string; + +typedef union +{ + t_base base; + t_integer integer; + t_string string; +} t_object; + +void print_object (t_object *this) +{ + if (this->base.type == T_INTEGER) + printf ("Integer: %d\n", this->integer.content); + else if (this->base.type == T_STRING) + printf ("String: \"%s\"\n", this->string.content); +} + +t_object *new_integer (int i) +{ + t_object *p = malloc (sizeof (t_integer)); + p->integer.type = T_INTEGER; + p->integer.content = i; + return p; +} + +t_object *new_string (char *s) +{ + t_object *p = malloc (sizeof (t_string)); + p->string.type = T_STRING; + p->string.content = s; + return p; +} + +int main (void) +{ + t_object *object[] = { new_integer (42), + new_string ("Hello, world!"), + NULL }; + + for (int i = 0; object[i]; i++) + print_object (object[i]); + + for (int i = 0; object[i]; i++) + free (object[i]); + return 0; +} diff --git a/20210128/unions-1.c b/20210128/unions-1.c new file mode 100644 index 0000000000000000000000000000000000000000..b75a6063b50193e13e9a2fd0ce140db6a8827499 --- /dev/null +++ b/20210128/unions-1.c @@ -0,0 +1,16 @@ +#include <stdio.h> +#include <stdint.h> + +typedef union +{ + int8_t i; + uint8_t u; +} num8_t; + +int main (void) +{ + num8_t test; + test.i = -1; + printf ("%d\n", test.u); + return 0; +} diff --git a/20210128/unions-2.c b/20210128/unions-2.c new file mode 100644 index 0000000000000000000000000000000000000000..8f3c5d8411b6f68e25a1e981e0a811d8015e3526 --- /dev/null +++ b/20210128/unions-2.c @@ -0,0 +1,15 @@ +#include <stdio.h> +#include <stdint.h> + +typedef union +{ + char s[8]; + uint64_t x; +} num_char_t; + +int main (void) +{ + num_char_t test = { "Hello" }; + printf ("%lx\n", test.x); + return 0; +} diff --git a/20210128/unions-3.c b/20210128/unions-3.c new file mode 100644 index 0000000000000000000000000000000000000000..2162c065714146572a88da29babe54246afe61c6 --- /dev/null +++ b/20210128/unions-3.c @@ -0,0 +1,15 @@ +#include <stdio.h> +#include <stdint.h> + +typedef union +{ + char s[8]; + uint64_t x; +} num_char_t; + +int main (void) +{ + num_char_t test = { "Hello" }; + printf ("%ld\n", test.x); + return 0; +} diff --git a/20210128/unions-4.c b/20210128/unions-4.c new file mode 100644 index 0000000000000000000000000000000000000000..5838bc1c2d6eef69ac36dad50e1ebc14d0b64f68 --- /dev/null +++ b/20210128/unions-4.c @@ -0,0 +1,15 @@ +#include <stdio.h> +#include <stdint.h> + +typedef union +{ + char s[8]; + uint64_t x; +} num_char_t; + +int main (void) +{ + num_char_t test = { x: 478560413000 }; + printf ("%s\n", test.s); + return 0; +}