From 6b63727911ee07d88729c6a19f28a63670cb74e7 Mon Sep 17 00:00:00 2001 From: Peter Gerwinski <peter.gerwinski@hs-bochum.de> Date: Thu, 27 Jun 2024 18:14:23 +0200 Subject: [PATCH] Beispiele und Notizen 27.6.2024 --- 20240627/ad-20240627.txt | 70 ++++++++++++++++++++++++++++++ 20240627/constructors-01.cpp | 18 ++++++++ 20240627/constructors-02.cpp | 24 +++++++++++ 20240627/constructors-03.cpp | 30 +++++++++++++ 20240627/constructors-04.cpp | 31 ++++++++++++++ 20240627/constructors-05.cpp | 37 ++++++++++++++++ 20240627/constructors-06.cpp | 38 +++++++++++++++++ 20240627/constructors-07.cpp | 44 +++++++++++++++++++ 20240627/constructors-08.cpp | 41 ++++++++++++++++++ 20240627/constructors-09.cpp | 42 ++++++++++++++++++ 20240627/constructors-10.cpp | 42 ++++++++++++++++++ 20240627/constructors-11.cpp | 40 +++++++++++++++++ 20240627/namespaces-01.cpp | 7 +++ 20240627/namespaces-02.cpp | 9 ++++ 20240627/namespaces-03.cpp | 9 ++++ 20240627/namespaces-04.cpp | 14 ++++++ 20240627/namespaces-05.cpp | 14 ++++++ 20240627/namespaces-06.cpp | 15 +++++++ 20240627/namespaces-07.cpp | 16 +++++++ 20240627/namespaces-08.cpp | 15 +++++++ 20240627/namespaces-09.cpp | 17 ++++++++ 20240627/namespaces-10.cpp | 20 +++++++++ 20240627/objects-01.c | 28 ++++++++++++ 20240627/objects-02.c | 28 ++++++++++++ 20240627/objects-03.c | 34 +++++++++++++++ 20240627/objects-04.c | 34 +++++++++++++++ 20240627/objects-05.c | 34 +++++++++++++++ 20240627/objects-06.c | 39 +++++++++++++++++ 20240627/objects-07.c | 43 +++++++++++++++++++ 20240627/objects-08.c | 59 +++++++++++++++++++++++++ 20240627/objects-09.c | 69 ++++++++++++++++++++++++++++++ 20240627/objects-10.c | 76 +++++++++++++++++++++++++++++++++ 20240627/objects-11.c | 78 +++++++++++++++++++++++++++++++++ 20240627/objects-12.c | 69 ++++++++++++++++++++++++++++++ 20240627/objects-13.c | 75 ++++++++++++++++++++++++++++++++ 20240627/objects-14.cpp | 55 ++++++++++++++++++++++++ 20240627/objects-15.cpp | 57 +++++++++++++++++++++++++ 20240627/objects-16.cpp | 55 ++++++++++++++++++++++++ 20240627/objects-17.cpp | 55 ++++++++++++++++++++++++ 20240627/objects-18.cpp | 60 ++++++++++++++++++++++++++ 20240627/objects-19.cpp | 60 ++++++++++++++++++++++++++ 20240627/objects-20.cpp | 60 ++++++++++++++++++++++++++ 20240627/objects-21.cpp | 72 +++++++++++++++++++++++++++++++ 20240627/objects-22.cpp | 74 ++++++++++++++++++++++++++++++++ 20240627/objects-23.cpp | 74 ++++++++++++++++++++++++++++++++ 20240627/objects-24.cpp | 74 ++++++++++++++++++++++++++++++++ 20240627/objects-25.cpp | 79 ++++++++++++++++++++++++++++++++++ 20240627/objects-26.cpp | 82 +++++++++++++++++++++++++++++++++++ 20240627/objects-27.cpp | 83 ++++++++++++++++++++++++++++++++++++ 20240627/strings-01.cpp | 9 ++++ 20240627/strings-02.cpp | 11 +++++ 20240627/strings-03.cpp | 11 +++++ 20240627/strings-04.cpp | 11 +++++ 20240627/strings-05.cpp | 9 ++++ 20240627/strings-06.cpp | 12 ++++++ 20240627/strings-07.cpp | 12 ++++++ 56 files changed, 2274 insertions(+) create mode 100644 20240627/ad-20240627.txt create mode 100644 20240627/constructors-01.cpp create mode 100644 20240627/constructors-02.cpp create mode 100644 20240627/constructors-03.cpp create mode 100644 20240627/constructors-04.cpp create mode 100644 20240627/constructors-05.cpp create mode 100644 20240627/constructors-06.cpp create mode 100644 20240627/constructors-07.cpp create mode 100644 20240627/constructors-08.cpp create mode 100644 20240627/constructors-09.cpp create mode 100644 20240627/constructors-10.cpp create mode 100644 20240627/constructors-11.cpp create mode 100644 20240627/namespaces-01.cpp create mode 100644 20240627/namespaces-02.cpp create mode 100644 20240627/namespaces-03.cpp create mode 100644 20240627/namespaces-04.cpp create mode 100644 20240627/namespaces-05.cpp create mode 100644 20240627/namespaces-06.cpp create mode 100644 20240627/namespaces-07.cpp create mode 100644 20240627/namespaces-08.cpp create mode 100644 20240627/namespaces-09.cpp create mode 100644 20240627/namespaces-10.cpp create mode 100644 20240627/objects-01.c create mode 100644 20240627/objects-02.c create mode 100644 20240627/objects-03.c create mode 100644 20240627/objects-04.c create mode 100644 20240627/objects-05.c create mode 100644 20240627/objects-06.c create mode 100644 20240627/objects-07.c create mode 100644 20240627/objects-08.c create mode 100644 20240627/objects-09.c create mode 100644 20240627/objects-10.c create mode 100644 20240627/objects-11.c create mode 100644 20240627/objects-12.c create mode 100644 20240627/objects-13.c create mode 100644 20240627/objects-14.cpp create mode 100644 20240627/objects-15.cpp create mode 100644 20240627/objects-16.cpp create mode 100644 20240627/objects-17.cpp create mode 100644 20240627/objects-18.cpp create mode 100644 20240627/objects-19.cpp create mode 100644 20240627/objects-20.cpp create mode 100644 20240627/objects-21.cpp create mode 100644 20240627/objects-22.cpp create mode 100644 20240627/objects-23.cpp create mode 100644 20240627/objects-24.cpp create mode 100644 20240627/objects-25.cpp create mode 100644 20240627/objects-26.cpp create mode 100644 20240627/objects-27.cpp create mode 100644 20240627/strings-01.cpp create mode 100644 20240627/strings-02.cpp create mode 100644 20240627/strings-03.cpp create mode 100644 20240627/strings-04.cpp create mode 100644 20240627/strings-05.cpp create mode 100644 20240627/strings-06.cpp create mode 100644 20240627/strings-07.cpp diff --git a/20240627/ad-20240627.txt b/20240627/ad-20240627.txt new file mode 100644 index 0000000..be20d7f --- /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 0000000..7971c5a --- /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 0000000..fba4832 --- /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 0000000..47d4880 --- /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 0000000..8265819 --- /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 0000000..592603c --- /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 0000000..27262ce --- /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 0000000..a0451a0 --- /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 0000000..0b8d0a0 --- /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 0000000..33ca3f6 --- /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 0000000..399f7db --- /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 0000000..6275a03 --- /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 0000000..04f85e5 --- /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 0000000..f9a2dd5 --- /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 0000000..ba3d728 --- /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 0000000..9509bd3 --- /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 0000000..0cb37c8 --- /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 0000000..1d19c82 --- /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 0000000..436c80b --- /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 0000000..4ebeac0 --- /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 0000000..df8c957 --- /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 0000000..28ace28 --- /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 0000000..1a62866 --- /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 0000000..a47cfb4 --- /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 0000000..ff9224c --- /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 0000000..ef7bffe --- /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 0000000..820181d --- /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 0000000..86847c9 --- /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 0000000..b9b24ad --- /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 0000000..0c93e8f --- /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 0000000..3355ce3 --- /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 0000000..86787f3 --- /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 0000000..0a921d8 --- /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 0000000..3b0dddd --- /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 0000000..81ef279 --- /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 0000000..9032934 --- /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 0000000..ab8809f --- /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 0000000..d836ad1 --- /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 0000000..879b881 --- /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 0000000..22ae5ce --- /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 0000000..72fdeae --- /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 0000000..02d9036 --- /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 0000000..02702e4 --- /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 0000000..3a14624 --- /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 0000000..8c83388 --- /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 0000000..413ecd2 --- /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 0000000..d7a78fa --- /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 0000000..9555178 --- /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 0000000..091f298 --- /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 0000000..c2c9822 --- /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 0000000..380cba3 --- /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 0000000..aeae49b --- /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 0000000..2ebb315 --- /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 0000000..163ab7b --- /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 0000000..efc3c3c --- /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 0000000..5bd0023 --- /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; +} -- GitLab