diff --git a/20240627/ad-20240627.txt b/20240627/ad-20240627.txt new file mode 100644 index 0000000000000000000000000000000000000000..be20d7f31b2c66029ea9dba4dec60b462986572c --- /dev/null +++ b/20240627/ad-20240627.txt @@ -0,0 +1,70 @@ +Aufgabe: Schreiben Sie die Sinusfunktion selbst. +~~~~~~~~ + - Wir setzen nur die Grundrechenarten voraus. + - möglichst effizient + +Lösungsansätze: + - Taylor-Reihe: sin x = x - x^3 / 3! + x^5 / 5! - x^7 / 7! + ... + - Kleinwinkelnäherung: Spezialfall der Taylor-Reihe + - Verallgemeinerung der Taylor-Reihe: mathematische Folge, die gegen die Lösung konvergiert + - Differentialgleichung lösen: f''(x) = -f(x) mit f(0) = 0, f'(0) = 1 + - möglichst effizient: Tabelle abspeichern, interpolieren + - Drehmatrizen --> CORDIC-Algorithmus + + +.----------------------------------------------------------------------------. +| Bis 17:45 Uhr bitte einen selbstgewählten Lösungsansatz implementieren, | +| so daß der berechnete Sinus mit dem Sinus der Laufzeitbibliothek möglichst | +| gut übereinstimmt. Dann vergleichen wir die Ergebnisse in Hinblick auf | +| Genauigkeit, Verbrauch an Rechenzeit, Verbrauch an Speicherplatz. | +| (Die verwendete Computersprache ist beliebig. Aber bitte erklären können.) | +`----------------------------------------------------------------------------' + + pi = 3.1415926535897932384626433... + 22/7 = 3.1428... +355/113 = 3.14159292... +(siehe auch: https://xkcd.com/2205/) + +sin (218 * 3.14159265358979323846 / 180) = -0.61566147532565827966 + sin (45 * 3.14159265358979323846 / 180) = 0.70710678118654752439 +sin (360 * 3.14159265358979323846 / 180) = 0.00000000000000000000 + sin (90 * 3.14159265358979323846 / 180) = 1.00000000000000000000 + sin (30 * 3.14159265358979323846 / 180) = 0.49999999999999999999 = 0.5 + +Taylor-Reihe bis 10: 5-6 Stellen Genauigkeit +längere Taylor-Reihe: 15-16 Stellen Genauigkeit +längere Taylor-Reihe rückwärts: Double-Genauigkeit :-) + + +Achtung: Wenn man stark unterschiedliche Gleitkommazahlen addiert, +verliert man Rechengenauigkeit! + +Beispiel: Rechenwerk mit 5 Dezimalstellen Genauigkeit + 1 = 1e0 + 1000 = 1e3 + 123.45 = 1.2345e2 + 123.45 + 0.0123 = 123.46 <-- Wir verlieren 2 Stellen Genauigkeit! + + 123.45 + 123.45 + 123.45 + ... + 123.45 + 123.45 (101 Summanden) + `-------------------v-----------------' + Summe so groß, daß der letzte Summand nur noch sehr ungenau zum Zuge kommt + (vereinfacht: 12345.0 + 123.45 = 12468.0, wir verlieren 2 Stellen) + + --> Beim Aufsummieren erst kleine Zahlen addieren, + so daß man insgesamt immer ungefähr gleich große Zahlen addiert + + +Drehmatrizen: (siehe auch: https://xkcd.com/184/) + + . . + | cos x -sin x | + | | dreht einen Vektor um den Winkel x + | sin x cos x | + ` ' + + ... kann ich hintereinanderschalten. + + Sammlung von Drehmatrizen: Drehung um pi/2, pi/4, pi/8, pi/16, ... + --> Winkel x liegt binär vor --> aus abgespeicherten Matrizen schnell eine zusammensetzen, + die um den Winkel x dreht + --> CORDIC-Algorithmus diff --git a/20240627/constructors-01.cpp b/20240627/constructors-01.cpp new file mode 100644 index 0000000000000000000000000000000000000000..7971c5a7f5c01a210327520866fb3ea815d3598d --- /dev/null +++ b/20240627/constructors-01.cpp @@ -0,0 +1,18 @@ +#include <stdio.h> + +struct Hello +{ + void hello (); +}; + +void Hello::hello () +{ + printf ("Hello, world!\n"); +} + +int main () +{ + Hello h; + h.hello (); + return 0; +} diff --git a/20240627/constructors-02.cpp b/20240627/constructors-02.cpp new file mode 100644 index 0000000000000000000000000000000000000000..fba4832e741f426b34f11b693fe518cb345d1712 --- /dev/null +++ b/20240627/constructors-02.cpp @@ -0,0 +1,24 @@ +#include <stdio.h> + +struct Hello +{ + void hello (); + Hello (); +}; + +void Hello::hello () +{ + printf ("Hello, world!\n"); +} + +Hello::Hello () +{ + printf ("constructor: Hello::Hello\n"); +} + +int main () +{ + Hello h; + h.hello (); + return 0; +} diff --git a/20240627/constructors-03.cpp b/20240627/constructors-03.cpp new file mode 100644 index 0000000000000000000000000000000000000000..47d48802872f353e61fa696d0a199b72988c89e3 --- /dev/null +++ b/20240627/constructors-03.cpp @@ -0,0 +1,30 @@ +#include <stdio.h> + +struct Hello +{ + void hello (); + Hello (); + virtual ~Hello (); +}; + +void Hello::hello () +{ + printf ("Hello, world!\n"); +} + +Hello::Hello () +{ + printf ("constructor: Hello::Hello\n"); +} + +Hello::~Hello () +{ + printf ("destructor: Hello::~Hello\n"); +} + +int main () +{ + Hello h; + h.hello (); + return 0; +} diff --git a/20240627/constructors-04.cpp b/20240627/constructors-04.cpp new file mode 100644 index 0000000000000000000000000000000000000000..826581957b3784e11f56aeb234af9dbb99073c5a --- /dev/null +++ b/20240627/constructors-04.cpp @@ -0,0 +1,31 @@ +#include <stdio.h> + +struct Hello +{ + void hello (); + Hello (); + virtual ~Hello (); +}; + +void Hello::hello () +{ + printf ("Hello, world!\n"); +} + +Hello::Hello () +{ + printf ("constructor: Hello::Hello\n"); +} + +Hello::~Hello () +{ + printf ("destructor: Hello::~Hello\n"); +} + +int main () +{ + Hello h1; + Hello h2 = h1; + h2.hello (); + return 0; +} diff --git a/20240627/constructors-05.cpp b/20240627/constructors-05.cpp new file mode 100644 index 0000000000000000000000000000000000000000..592603c565fb4b8fd0f6bd53c1241674dfba6664 --- /dev/null +++ b/20240627/constructors-05.cpp @@ -0,0 +1,37 @@ +#include <stdio.h> + +struct Hello +{ + void hello (); + Hello (); + Hello (const Hello &h); + virtual ~Hello (); +}; + +void Hello::hello () +{ + printf ("Hello, world!\n"); +} + +Hello::Hello () +{ + printf ("constructor: Hello::Hello ()\n"); +} + +Hello::Hello (const Hello &h) +{ + printf ("constructor: Hello::Hello (const Hello &h)\n"); +} + +Hello::~Hello () +{ + printf ("destructor: Hello::~Hello ()\n"); +} + +int main () +{ + Hello h1; + Hello h2 = h1; + h2.hello (); + return 0; +} diff --git a/20240627/constructors-06.cpp b/20240627/constructors-06.cpp new file mode 100644 index 0000000000000000000000000000000000000000..27262ce9ae15a1eb561604ac318f67be91f8c354 --- /dev/null +++ b/20240627/constructors-06.cpp @@ -0,0 +1,38 @@ +#include <stdio.h> + +struct Hello +{ + void hello (); + Hello (); + Hello (const Hello &h); + virtual ~Hello (); +}; + +void Hello::hello () +{ + printf ("Hello, world!\n"); +} + +Hello::Hello () +{ + printf ("constructor: Hello::Hello ()\n"); +} + +Hello::Hello (const Hello &h) +{ + printf ("constructor: Hello::Hello (const Hello &h)\n"); +} + +Hello::~Hello () +{ + printf ("destructor: Hello::~Hello ()\n"); +} + +int main () +{ + Hello h1; + Hello h2; + h2 = h1; + h2.hello (); + return 0; +} diff --git a/20240627/constructors-07.cpp b/20240627/constructors-07.cpp new file mode 100644 index 0000000000000000000000000000000000000000..a0451a0d950bc4439608e5043dad2a5733ccd12d --- /dev/null +++ b/20240627/constructors-07.cpp @@ -0,0 +1,44 @@ +#include <stdio.h> + +struct Hello +{ + void hello (); + Hello (); + Hello (const Hello &h); + virtual ~Hello (); + operator = (const Hello &h); +}; + +void Hello::hello () +{ + printf ("Hello, world!\n"); +} + +Hello::Hello () +{ + printf ("constructor: Hello::Hello ()\n"); +} + +Hello::Hello (const Hello &h) +{ + printf ("constructor: Hello::Hello (const Hello &h)\n"); +} + +operator Hello::= (const Hello &h) +{ + printf ("operator: Hello = Hello\n"); +} + +Hello::~Hello () +{ + printf ("destructor: Hello::~Hello ()\n"); +} + +int main () +{ + Hello h1; + Hello h2; + h2 = h1; + h2.hello (); + return 0; +} diff --git a/20240627/constructors-08.cpp b/20240627/constructors-08.cpp new file mode 100644 index 0000000000000000000000000000000000000000..0b8d0a07d7e26bb54f0bace965cce7e3f884aa13 --- /dev/null +++ b/20240627/constructors-08.cpp @@ -0,0 +1,41 @@ +#include <stdio.h> + +struct Hello +{ + + void hello () + { + printf ("Hello, world!\n"); + } + + Hello () + { + printf ("constructor: Hello::Hello ()\n"); + } + + Hello (const Hello &h) + { + printf ("constructor: Hello::Hello (const Hello &h)\n"); + } + + virtual ~Hello () + { + printf ("destructor: Hello::~Hello ()\n"); + } + + Hello &operator = (const Hello &h) + { + printf ("operator: Hello = Hello\n"); + return *this; + } + +}; + +int main () +{ + Hello h1; + Hello h2; + h2 = h1; + h2.hello (); + return 0; +} diff --git a/20240627/constructors-09.cpp b/20240627/constructors-09.cpp new file mode 100644 index 0000000000000000000000000000000000000000..33ca3f65281ec565c2445a3e2bf4d424519de377 --- /dev/null +++ b/20240627/constructors-09.cpp @@ -0,0 +1,42 @@ +#include <stdio.h> + +struct Hello +{ + + void hello () + { + printf ("Hello, world!\n"); + } + + Hello () + { + printf ("constructor: Hello::Hello ()\n"); + } + + Hello (const Hello &h) + { + printf ("constructor: Hello::Hello (const Hello &h)\n"); + } + + virtual ~Hello () + { + printf ("destructor: Hello::~Hello ()\n"); + } + + Hello &operator = (const Hello &h) + { + printf ("operator: Hello = Hello\n"); + return *this; + } + +}; + +int main () +{ + Hello *h1 = new Hello (); + Hello *h2 = new Hello (h1); + h2->hello (); + delete h1; + delete h2; + return 0; +} diff --git a/20240627/constructors-10.cpp b/20240627/constructors-10.cpp new file mode 100644 index 0000000000000000000000000000000000000000..399f7db150427881b90b92e7eee75d2cfd82446d --- /dev/null +++ b/20240627/constructors-10.cpp @@ -0,0 +1,42 @@ +#include <stdio.h> + +struct Hello +{ + + void hello () + { + printf ("Hello, world!\n"); + } + + Hello () + { + printf ("constructor: Hello::Hello ()\n"); + } + + Hello (const Hello &h) + { + printf ("constructor: Hello::Hello (const Hello &h)\n"); + } + + virtual ~Hello () + { + printf ("destructor: Hello::~Hello ()\n"); + } + + Hello &operator = (const Hello &h) + { + printf ("operator: Hello = Hello\n"); + return *this; + } + +}; + +int main () +{ + Hello *h1 = new Hello (); + Hello *h2 = new Hello (*h1); + h2->hello (); + delete h1; + delete h2; + return 0; +} diff --git a/20240627/constructors-11.cpp b/20240627/constructors-11.cpp new file mode 100644 index 0000000000000000000000000000000000000000..6275a034207f54f8aba1f75509271daa6fd9c729 --- /dev/null +++ b/20240627/constructors-11.cpp @@ -0,0 +1,40 @@ +#include <stdio.h> + +struct Hello +{ + + void hello () + { + printf ("Hello, world!\n"); + } + + Hello () + { + printf ("constructor: Hello::Hello ()\n"); + } + + Hello (const Hello &h) + { + printf ("constructor: Hello::Hello (const Hello &h)\n"); + } + + virtual ~Hello () + { + printf ("destructor: Hello::~Hello ()\n"); + } + + Hello &operator = (const Hello &h) + { + printf ("operator: Hello = Hello\n"); + return *this; + } + +}; + +int main () +{ + Hello *h1 = new Hello (); + Hello *h2 = new Hello (*h1); + h2->hello (); + return 0; +} diff --git a/20240627/namespaces-01.cpp b/20240627/namespaces-01.cpp new file mode 100644 index 0000000000000000000000000000000000000000..04f85e594a6a7ccbfde429021f61e9556f9581e9 --- /dev/null +++ b/20240627/namespaces-01.cpp @@ -0,0 +1,7 @@ +#include <iostream> + +int main () +{ + std::cout << "Hello, world!" << std::endl; + return 0; +} diff --git a/20240627/namespaces-02.cpp b/20240627/namespaces-02.cpp new file mode 100644 index 0000000000000000000000000000000000000000..f9a2dd57bca5acd537a3f7a66368fe0b4a86c1b9 --- /dev/null +++ b/20240627/namespaces-02.cpp @@ -0,0 +1,9 @@ +#include <iostream> + +using namespace std; + +int main () +{ + cout << "Hello, world!" << endl; + return 0; +} diff --git a/20240627/namespaces-03.cpp b/20240627/namespaces-03.cpp new file mode 100644 index 0000000000000000000000000000000000000000..ba3d7284fec443fb2bbe75a0b7beb0a9f3f8a5b0 --- /dev/null +++ b/20240627/namespaces-03.cpp @@ -0,0 +1,9 @@ +#include <iostream> + +using std::cout, std::endl; + +int main () +{ + cout << "Hello, world!" << endl; + return 0; +} diff --git a/20240627/namespaces-04.cpp b/20240627/namespaces-04.cpp new file mode 100644 index 0000000000000000000000000000000000000000..9509bd39ae03f72090979e394dc5640a349d109e --- /dev/null +++ b/20240627/namespaces-04.cpp @@ -0,0 +1,14 @@ +#include <iostream> + +using std::cout, std::endl; + +namespace greeting +{ + const char *hello = "Hello, world!"; +} + +int main () +{ + cout << hello << endl; + return 0; +} diff --git a/20240627/namespaces-05.cpp b/20240627/namespaces-05.cpp new file mode 100644 index 0000000000000000000000000000000000000000..0cb37c8e2fccefc9c1dde7a85d87427029e4bb60 --- /dev/null +++ b/20240627/namespaces-05.cpp @@ -0,0 +1,14 @@ +#include <iostream> + +using std::cout, std::endl; + +namespace greeting +{ + const char *hello = "Hello, world!"; +} + +int main () +{ + cout << greeting::hello << endl; + return 0; +} diff --git a/20240627/namespaces-06.cpp b/20240627/namespaces-06.cpp new file mode 100644 index 0000000000000000000000000000000000000000..1d19c82a5a60481b8325750ef7758cf479e340e1 --- /dev/null +++ b/20240627/namespaces-06.cpp @@ -0,0 +1,15 @@ +#include <iostream> + +using std::cout, std::endl; + +class greeting +{ +public: + const char *hello = "Hello, world!"; +}; + +int main () +{ + cout << greeting::hello << endl; + return 0; +} diff --git a/20240627/namespaces-07.cpp b/20240627/namespaces-07.cpp new file mode 100644 index 0000000000000000000000000000000000000000..436c80b15a25e8807d73e26b29f3f8ea856720fe --- /dev/null +++ b/20240627/namespaces-07.cpp @@ -0,0 +1,16 @@ +#include <iostream> + +using std::cout, std::endl; + +class greeting +{ +public: + const char *hello = "Hello, world!"; +}; + +int main () +{ + greeting Hello; + cout << Hello.hello << endl; + return 0; +} diff --git a/20240627/namespaces-08.cpp b/20240627/namespaces-08.cpp new file mode 100644 index 0000000000000000000000000000000000000000..4ebeac0bd8a3a607a8dfeadc8a31219b561f764b --- /dev/null +++ b/20240627/namespaces-08.cpp @@ -0,0 +1,15 @@ +#include <iostream> + +using std::cout, std::endl; + +class greeting +{ +public: + static const char *hello = "Hello, world!"; +}; + +int main () +{ + cout << greeting::hello << endl; + return 0; +} diff --git a/20240627/namespaces-09.cpp b/20240627/namespaces-09.cpp new file mode 100644 index 0000000000000000000000000000000000000000..df8c957d8be5ac3d69f906760829cde9e40eee2b --- /dev/null +++ b/20240627/namespaces-09.cpp @@ -0,0 +1,17 @@ +#include <iostream> + +using std::cout, std::endl; + +class greeting +{ +public: + static const char *hello; +}; + +const char *greeting::hello = "Hello, world!"; + +int main () +{ + cout << greeting::hello << endl; + return 0; +} diff --git a/20240627/namespaces-10.cpp b/20240627/namespaces-10.cpp new file mode 100644 index 0000000000000000000000000000000000000000..28ace2845160208141794c8a11d6a14493dd661f --- /dev/null +++ b/20240627/namespaces-10.cpp @@ -0,0 +1,20 @@ +#include <iostream> + +using std::cout, std::endl; + +class greeting +{ +public: + static void hello (); +}; + +void greeting::hello () +{ + cout << "Hello, world!" << endl; +} + +int main () +{ + greeting::hello (); + return 0; +} diff --git a/20240627/objects-01.c b/20240627/objects-01.c new file mode 100644 index 0000000000000000000000000000000000000000..1a628668fd7b8fb3a0d9886ac14e8e909bc23793 --- /dev/null +++ b/20240627/objects-01.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/20240627/objects-02.c b/20240627/objects-02.c new file mode 100644 index 0000000000000000000000000000000000000000..a47cfb4276085399afb86795d04b1f6ae20c95bf --- /dev/null +++ b/20240627/objects-02.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/20240627/objects-03.c b/20240627/objects-03.c new file mode 100644 index 0000000000000000000000000000000000000000..ff9224c0767ccad39f5b1396720ee73b6a0455fb --- /dev/null +++ b/20240627/objects-03.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/20240627/objects-04.c b/20240627/objects-04.c new file mode 100644 index 0000000000000000000000000000000000000000..ef7bffe80471d4b014258824421dce0557fc41dd --- /dev/null +++ b/20240627/objects-04.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/20240627/objects-05.c b/20240627/objects-05.c new file mode 100644 index 0000000000000000000000000000000000000000..820181d87e2a04b81cd2e03aa7980d970cd6c1a6 --- /dev/null +++ b/20240627/objects-05.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/20240627/objects-06.c b/20240627/objects-06.c new file mode 100644 index 0000000000000000000000000000000000000000..86847c9085083e2d5093026619550fb272b5f4ec --- /dev/null +++ b/20240627/objects-06.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/20240627/objects-07.c b/20240627/objects-07.c new file mode 100644 index 0000000000000000000000000000000000000000..b9b24ad65f650aeda194e4570ae853e1a5ad8f11 --- /dev/null +++ b/20240627/objects-07.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/20240627/objects-08.c b/20240627/objects-08.c new file mode 100644 index 0000000000000000000000000000000000000000..0c93e8f6c3c4b5e504c758dfa87b8510e98c0692 --- /dev/null +++ b/20240627/objects-08.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/20240627/objects-09.c b/20240627/objects-09.c new file mode 100644 index 0000000000000000000000000000000000000000..3355ce354278e9839db0952c6de0d7acca172157 --- /dev/null +++ b/20240627/objects-09.c @@ -0,0 +1,69 @@ +#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/20240627/objects-10.c b/20240627/objects-10.c new file mode 100644 index 0000000000000000000000000000000000000000..86787f3d0339bda54baefd062392d8792cd4b2be --- /dev/null +++ b/20240627/objects-10.c @@ -0,0 +1,76 @@ +#include <stdio.h> +#include <stdlib.h> + +#define T_BASE 0 +#define T_INTEGER 1 +#define T_STRING 2 + +typedef struct +{ + int type; + void (*print) (t_object *this); +} t_base; + +typedef struct +{ + int type; + void (*print) (t_object *this); + int content; +} t_integer; + +typedef struct +{ + int type; + void (*print) (t_object *this); + char *content; +} t_string; + +typedef union +{ + t_base base; + t_integer integer; + t_string string; +} t_object; + +void print_integer (t_object *this) +{ + printf ("Integer: %d\n", this->integer.content); +} + +void print_string (t_object *this) +{ + 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.print = print_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.print = print_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++) + object[i]->base.print (object[i]); + + for (int i = 0; object[i]; i++) + free (object[i]); + + return 0; +} diff --git a/20240627/objects-11.c b/20240627/objects-11.c new file mode 100644 index 0000000000000000000000000000000000000000..0a921d8b5c88198cdd07aeb409da6b2a57be33ac --- /dev/null +++ b/20240627/objects-11.c @@ -0,0 +1,78 @@ +#include <stdio.h> +#include <stdlib.h> + +#define T_BASE 0 +#define T_INTEGER 1 +#define T_STRING 2 + +union t_object; + +typedef struct +{ + int type; + void (*print) (union t_object *this); +} t_base; + +typedef struct +{ + int type; + void (*print) (union t_object *this); + int content; +} t_integer; + +typedef struct +{ + int type; + void (*print) (union t_object *this); + char *content; +} t_string; + +typedef union t_object +{ + t_base base; + t_integer integer; + t_string string; +} t_object; + +void print_integer (t_object *this) +{ + printf ("Integer: %d\n", this->integer.content); +} + +void print_string (t_object *this) +{ + 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.print = print_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.print = print_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++) + object[i]->base.print (object[i]); + + for (int i = 0; object[i]; i++) + free (object[i]); + + return 0; +} diff --git a/20240627/objects-12.c b/20240627/objects-12.c new file mode 100644 index 0000000000000000000000000000000000000000..3b0dddd0b7eda19d8b0545906dabd76bc9c8a378 --- /dev/null +++ b/20240627/objects-12.c @@ -0,0 +1,69 @@ +#include <stdio.h> +#include <stdlib.h> + +union t_object; + +typedef struct +{ + void (* print) (union t_object *this); +} t_base; + +typedef struct +{ + void (* print) (union t_object *this); + int content; +} t_integer; + +typedef struct +{ + void (* print) (union t_object *this); + char *content; +} t_string; + +typedef union t_object +{ + t_base base; + t_integer integer; + t_string string; +} t_object; + +void print_integer (t_object *this) +{ + printf ("Integer: %d\n", this->integer.content); +} + +void print_string (t_object *this) +{ + printf ("String: \"%s\"\n", this->string.content); +} + +t_object *new_integer (int i) +{ + t_object *p = malloc (sizeof (t_integer)); + p->integer.print = print_integer; + p->integer.content = i; + return p; +} + +t_object *new_string (char *s) +{ + t_object *p = malloc (sizeof (t_string)); + p->string.print = print_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++) + object[i]->base.print (object[i]); + + for (int i = 0; object[i]; i++) + free (object[i]); + + return 0; +} diff --git a/20240627/objects-13.c b/20240627/objects-13.c new file mode 100644 index 0000000000000000000000000000000000000000..81ef279b060e0b6290194fdeda8c3330cb716cdd --- /dev/null +++ b/20240627/objects-13.c @@ -0,0 +1,75 @@ +#include <stdio.h> +#include <stdlib.h> + +union t_object; +struct t_vmt; + +typedef struct +{ + struct t_vmt *vmt; +} t_base; + +typedef struct +{ + struct t_vmt *vmt; + int content; +} t_integer; + +typedef struct +{ + struct t_vmt *vmt; + char *content; +} t_string; + +typedef union t_object +{ + t_base base; + t_integer integer; + t_string string; +} t_object; + +typedef struct t_vmt +{ + void (* print) (union t_object *this); +} t_vmt; + +void print_integer (t_object *this) +{ + printf ("Integer: %d\n", this->integer.content); +} + +void print_string (t_object *this) +{ + printf ("String: \"%s\"\n", this->string.content); +} + +t_vmt vmt_integer = { print_integer }; +t_vmt vmt_string = { print_string }; + +t_object *new_integer (int i) +{ + t_object *p = malloc (sizeof (t_integer)); + p->integer.vmt = &vmt_integer; + p->integer.content = i; + return p; +} + +t_object *new_string (char *s) +{ + t_object *p = malloc (sizeof (t_string)); + p->integer.vmt = &vmt_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++) + object[i]->base.vmt->print (object[i]); + + return 0; +} diff --git a/20240627/objects-14.cpp b/20240627/objects-14.cpp new file mode 100644 index 0000000000000000000000000000000000000000..90329346afd7d1cfd1caad1d267781b25300b4ca --- /dev/null +++ b/20240627/objects-14.cpp @@ -0,0 +1,55 @@ +#include <stdio.h> + +struct TBase +{ + virtual void print () = 0; +}; + +struct TInteger: TBase +{ + int content; + virtual void print (); + TInteger (int i); +}; + +struct 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/20240627/objects-15.cpp b/20240627/objects-15.cpp new file mode 100644 index 0000000000000000000000000000000000000000..ab8809f4cbcaa761bee405e4627adc86fa19a8a5 --- /dev/null +++ b/20240627/objects-15.cpp @@ -0,0 +1,57 @@ +#include <stdio.h> + +struct TBase +{ + virtual void print () = 0; +}; + +struct TInteger: TBase +{ + int content; + virtual void print (); + TInteger (int i); +}; + +struct 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 ((char *) "Hello, world!"), + // objects-15.cpp:45:36: warning: ISO C++ forbids converting + // a string constant to ‘char*’ [-Wwrite-strings] + 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/20240627/objects-16.cpp b/20240627/objects-16.cpp new file mode 100644 index 0000000000000000000000000000000000000000..d836ad1f703a491ed0ef106b060d147f37d29bb2 --- /dev/null +++ b/20240627/objects-16.cpp @@ -0,0 +1,55 @@ +#include <stdio.h> + +struct TBase +{ + virtual void print () = 0; +}; + +struct TInteger: TBase +{ + int content; + virtual void print (); + TInteger (int i); +}; + +struct 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 (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/20240627/objects-17.cpp b/20240627/objects-17.cpp new file mode 100644 index 0000000000000000000000000000000000000000..879b8814dea5a91a8057bbe898eace65187ca1e0 --- /dev/null +++ b/20240627/objects-17.cpp @@ -0,0 +1,55 @@ +#include <stdio.h> + +struct TBase +{ + virtual void print () = 0; +}; + +struct TInteger: TBase +{ + int content; + virtual void print (); + TInteger (int i); +}; + +struct TString: TBase +{ + 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/20240627/objects-18.cpp b/20240627/objects-18.cpp new file mode 100644 index 0000000000000000000000000000000000000000..22ae5ce344f519a4c768e10de6c169d7fbd0b523 --- /dev/null +++ b/20240627/objects-18.cpp @@ -0,0 +1,60 @@ +#include <stdio.h> + +struct TBase +{ + virtual void print (); +}; + +struct TInteger: TBase +{ + int content; + virtual void print (); + TInteger (int i); +}; + +struct TString: TBase +{ + const char *content; + virtual void print (); + TString (const char *s); +}; + +void TBase::print () +{ + print ("Base\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) +{ + 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/20240627/objects-19.cpp b/20240627/objects-19.cpp new file mode 100644 index 0000000000000000000000000000000000000000..72fdeae081a0e27ab9946fb06e43d47d4d19d069 --- /dev/null +++ b/20240627/objects-19.cpp @@ -0,0 +1,60 @@ +#include <stdio.h> + +struct TBase +{ + virtual void print (); +}; + +struct TInteger: TBase +{ + int content; + virtual void print (); + TInteger (int i); +}; + +struct TString: TBase +{ + const char *content; + virtual void print (); + TString (const char *s); +}; + +void TBase::print () +{ + printf ("Base\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) +{ + 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/20240627/objects-20.cpp b/20240627/objects-20.cpp new file mode 100644 index 0000000000000000000000000000000000000000..02d903673c8f73d5be5bd9550a3e86447d7b37ad --- /dev/null +++ b/20240627/objects-20.cpp @@ -0,0 +1,60 @@ +#include <stdio.h> + +struct TBase +{ + void print (); +}; + +struct TInteger: TBase +{ + int content; + void print (); + TInteger (int i); +}; + +struct TString: TBase +{ + const char *content; + void print (); + TString (const char *s); +}; + +void TBase::print () +{ + printf ("Base\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) +{ + 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/20240627/objects-21.cpp b/20240627/objects-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..02702e4cbfdfa26cfcdfa755ee30f784b8aec698 --- /dev/null +++ b/20240627/objects-21.cpp @@ -0,0 +1,72 @@ +#include <stdio.h> + +struct TBase +{ + virtual void print (); + virtual ~Base (); +}; + +struct TInteger: TBase +{ + int content; + virtual void print (); + TInteger (int i); +}; + +struct TString: TBase +{ + char *content; + virtual void print (); + TString (const char *s); + virtual ~TString (); +}; + +void TBase::print () +{ + printf ("Base\n"); +} + +TBase::~TBase () +{ +} + +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 = malloc (strlen (s) + 1); + strcpy (content, s); +} + +TString::~TString () +{ + 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/20240627/objects-22.cpp b/20240627/objects-22.cpp new file mode 100644 index 0000000000000000000000000000000000000000..3a14624479d01ef041b17640268820cf4097e395 --- /dev/null +++ b/20240627/objects-22.cpp @@ -0,0 +1,74 @@ +#include <stdio.h> +#include <stdlib.h> +#include <string.h> + +struct TBase +{ + virtual void print (); + virtual ~TBase (); +}; + +struct TInteger: TBase +{ + int content; + virtual void print (); + TInteger (int i); +}; + +struct TString: TBase +{ + char *content; + virtual void print (); + TString (const char *s); + virtual ~TString (); +}; + +void TBase::print () +{ + printf ("Base\n"); +} + +TBase::~TBase () +{ +} + +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 = malloc (strlen (s) + 1); + strcpy (content, s); +} + +TString::~TString () +{ + 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/20240627/objects-23.cpp b/20240627/objects-23.cpp new file mode 100644 index 0000000000000000000000000000000000000000..8c8338886c187e3ad0cd3a990430773527ebad46 --- /dev/null +++ b/20240627/objects-23.cpp @@ -0,0 +1,74 @@ +#include <stdio.h> +#include <stdlib.h> +#include <string.h> + +struct TBase +{ + virtual void print (); + virtual ~TBase (); +}; + +struct TInteger: TBase +{ + int content; + virtual void print (); + TInteger (int i); +}; + +struct TString: TBase +{ + char *content; + virtual void print (); + TString (const char *s); + virtual ~TString (); +}; + +void TBase::print () +{ + printf ("Base\n"); +} + +TBase::~TBase () +{ +} + +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 = (char *) malloc (strlen (s) + 1); + strcpy (content, s); +} + +TString::~TString () +{ + 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/20240627/objects-24.cpp b/20240627/objects-24.cpp new file mode 100644 index 0000000000000000000000000000000000000000..413ecd227bf3772e0a62c8076931798dba5a1af5 --- /dev/null +++ b/20240627/objects-24.cpp @@ -0,0 +1,74 @@ +#include <stdio.h> +#include <stdlib.h> +#include <string.h> + +class TBase +{ + virtual void print (); + virtual ~TBase (); +}; + +class TInteger: TBase +{ + int content; + virtual void print (); + TInteger (int i); +}; + +class TString: TBase +{ + char *content; + virtual void print (); + TString (const char *s); + virtual ~TString (); +}; + +void TBase::print () +{ + printf ("Base\n"); +} + +TBase::~TBase () +{ +} + +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 = (char *) malloc (strlen (s) + 1); + strcpy (content, s); +} + +TString::~TString () +{ + 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/20240627/objects-25.cpp b/20240627/objects-25.cpp new file mode 100644 index 0000000000000000000000000000000000000000..d7a78fa6a1b463eaa02ccb91998a3852b1ec6580 --- /dev/null +++ b/20240627/objects-25.cpp @@ -0,0 +1,79 @@ +#include <stdio.h> +#include <stdlib.h> +#include <string.h> + +class TBase +{ +public: + virtual void print (); + virtual ~TBase (); +}; + +class TInteger: public TBase +{ +private: + int content; +public: + virtual void print (); + TInteger (int i); +}; + +class TString: public TBase +{ +private: + char *content; +public: + virtual void print (); + TString (const char *s); + virtual ~TString (); +}; + +void TBase::print () +{ + printf ("Base\n"); +} + +TBase::~TBase () +{ +} + +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 = (char *) malloc (strlen (s) + 1); + strcpy (content, s); +} + +TString::~TString () +{ + 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/20240627/objects-26.cpp b/20240627/objects-26.cpp new file mode 100644 index 0000000000000000000000000000000000000000..9555178e04d4e00f38cb6f63352f03aca253ed35 --- /dev/null +++ b/20240627/objects-26.cpp @@ -0,0 +1,82 @@ +#include <stdio.h> +#include <stdlib.h> +#include <string.h> + +class TBase +{ +public: + virtual void print (); + virtual ~TBase (); +}; + +class TInteger: public TBase +{ +private: + int content; +public: + virtual void print (); + TInteger (int i); +}; + +class TString: public TBase +{ +private: + char *content; +public: + virtual void print (); + TString (const char *s); + virtual ~TString (); +}; + +void TBase::print () +{ + printf ("Base\n"); +} + +TBase::~TBase () +{ +} + +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 = (char *) malloc (strlen (s) + 1); + strcpy (content, s); +} + +TString::~TString () +{ + free (content); +} + +int main (void) +{ + TInteger *answer = new TInteger (42); + TBase *object[] = { answer, + new TString ("Hello, world!"), + NULL }; + + for (int i = 0; object[i]; i++) + object[i]->print (); + + printf ("The answer is: %d.\n", answer->content); + + for (int i = 0; object[i]; i++) + delete object[i]; + + return 0; +} diff --git a/20240627/objects-27.cpp b/20240627/objects-27.cpp new file mode 100644 index 0000000000000000000000000000000000000000..091f2989a9feaa1b381f49ac5edbd07bb71d74ed --- /dev/null +++ b/20240627/objects-27.cpp @@ -0,0 +1,83 @@ +#include <stdio.h> +#include <stdlib.h> +#include <string.h> + +class TBase +{ +public: + virtual void print (); + virtual ~TBase (); +}; + +class TInteger: public TBase +{ +private: + int content; +public: + virtual void print (); + TInteger (int i); + friend int main (); +}; + +class TString: public TBase +{ +private: + char *content; +public: + virtual void print (); + TString (const char *s); + virtual ~TString (); +}; + +void TBase::print () +{ + printf ("Base\n"); +} + +TBase::~TBase () +{ +} + +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 = (char *) malloc (strlen (s) + 1); + strcpy (content, s); +} + +TString::~TString () +{ + free (content); +} + +int main (void) +{ + TInteger *answer = new TInteger (42); + TBase *object[] = { answer, + new TString ("Hello, world!"), + NULL }; + + for (int i = 0; object[i]; i++) + object[i]->print (); + + printf ("The answer is: %d.\n", answer->content); + + for (int i = 0; object[i]; i++) + delete object[i]; + + return 0; +} diff --git a/20240627/strings-01.cpp b/20240627/strings-01.cpp new file mode 100644 index 0000000000000000000000000000000000000000..c2c9822644e544549f06d680f4872c1642b3fbda --- /dev/null +++ b/20240627/strings-01.cpp @@ -0,0 +1,9 @@ +#include <iostream> +#include <string> + +int main () +{ + string hello = "Hello, world!"; + cout << hello << endl; + return 0; +} diff --git a/20240627/strings-02.cpp b/20240627/strings-02.cpp new file mode 100644 index 0000000000000000000000000000000000000000..380cba330d94a419b03739fa8aa3685275e64b93 --- /dev/null +++ b/20240627/strings-02.cpp @@ -0,0 +1,11 @@ +#include <iostream> +#include <string> + +using std::string, std::cout, std::endl; + +int main () +{ + string hello = "Hello, world!"; + cout << hello << endl; + return 0; +} diff --git a/20240627/strings-03.cpp b/20240627/strings-03.cpp new file mode 100644 index 0000000000000000000000000000000000000000..aeae49b6cb5fb8206f0c831226bb374f074b3317 --- /dev/null +++ b/20240627/strings-03.cpp @@ -0,0 +1,11 @@ +#include <stdio.h> +#include <string> + +using std::string; + +int main () +{ + string hello = "Hello, world!"; + printf ("%s\n", hello); + return 0; +} diff --git a/20240627/strings-04.cpp b/20240627/strings-04.cpp new file mode 100644 index 0000000000000000000000000000000000000000..2ebb3154c010121e16a89a9e3142f4c65e8e292b --- /dev/null +++ b/20240627/strings-04.cpp @@ -0,0 +1,11 @@ +#include <stdio.h> +#include <string> + +using std::string; + +int main () +{ + string hello = "Hello, world!"; + printf ("%s\n", hello.c_str ()); + return 0; +} diff --git a/20240627/strings-05.cpp b/20240627/strings-05.cpp new file mode 100644 index 0000000000000000000000000000000000000000..163ab7b2e778f9dc13ce7af6640c25cbc8a4d967 --- /dev/null +++ b/20240627/strings-05.cpp @@ -0,0 +1,9 @@ +#include <stdio.h> + +int main () +{ + char answer[100]; + snprintf (answer, 100, "The answer is %d.", 42); + printf ("%s\n", answer); + return 0; +} diff --git a/20240627/strings-06.cpp b/20240627/strings-06.cpp new file mode 100644 index 0000000000000000000000000000000000000000..efc3c3cb6ca3d5cf9a077c88099fefade8144abe --- /dev/null +++ b/20240627/strings-06.cpp @@ -0,0 +1,12 @@ +#include <sstream> +#include <iostream> + +using std::ostringstream, std::cout, std::endl; + +int main () +{ + ostringstream answer; + answer << "The answer is " << 42 << "."; + cout << answer << endl; + return 0; +} diff --git a/20240627/strings-07.cpp b/20240627/strings-07.cpp new file mode 100644 index 0000000000000000000000000000000000000000..5bd002357f7ea116c3ca9da6784ab16783275d81 --- /dev/null +++ b/20240627/strings-07.cpp @@ -0,0 +1,12 @@ +#include <sstream> +#include <iostream> + +using std::ostringstream, std::cout, std::endl; + +int main () +{ + ostringstream answer; + answer << "The answer is " << 42 << "."; + cout << answer.str () << endl; + return 0; +}