diff --git a/20240711/ad-20240711.pdf b/20240711/ad-20240711.pdf index 62056f01cad5b8fad7345b1b9a5596d27da26ea0..5bd62b4b486f9cb9b237eb0a37790869f337975b 100644 Binary files a/20240711/ad-20240711.pdf and b/20240711/ad-20240711.pdf differ diff --git a/20240711/ad-20240711.tex b/20240711/ad-20240711.tex index b166d26123924b26d6a3599aca772b81962bf89a..b7693b633448baecf7adecae7b62ec6190479f6f 100644 --- a/20240711/ad-20240711.tex +++ b/20240711/ad-20240711.tex @@ -60,7 +60,7 @@ \item[\textbf{4}] \textbf{Kryptographie} \item[\textbf{5}] \textbf{Datenkompression} \item[\color{orange}\textbf{6}] \textbf{\color{orange}Einführung in C++, Datenorganisation} - \item[\color{orange}\textbf{7}] \textbf{\color{orange}Hardwarenahe Algorithmen} + \item[\color{medgreen}\textbf{7}] \textbf{\color{medgreen}Hardwarenahe Algorithmen} \end{itemize} \end{frame} @@ -423,22 +423,4 @@ \end{itemize} \end{frame} -\section{Hardwarenahe Algorithmen} - -\begin{frame} - \showsection - - \textbf{Aufgabe:} - Schreiben Sie die Sinusfunktion selbst. - - \smallskip - - \begin{itemize} - \item - Wir setzen nur die Grundrechenarten voraus. - \item - möglichst effizient - \end{itemize} -\end{frame} - \end{document} diff --git a/20240711/exceptions-00.cpp b/20240711/exceptions-00.cpp new file mode 100644 index 0000000000000000000000000000000000000000..6319e0e69bc10a3cd80e72d1663357fcdadde071 --- /dev/null +++ b/20240711/exceptions-00.cpp @@ -0,0 +1,19 @@ +#include <iostream> + +using namespace std; + +int main () +{ + int a = 42; + int b = 0; + if (b == 0) + { + cout << "Irgendwas ist schiefgelaufen: Ganzzahldivision durch Null" << endl; + } + else + { + int c = a / b; + cout << "c = " << c << endl; + } + return 0; +} diff --git a/20240711/exceptions-01.cpp b/20240711/exceptions-01.cpp new file mode 100644 index 0000000000000000000000000000000000000000..72db25aeeb34cf37d07a6f78eea2d55bf0db2851 --- /dev/null +++ b/20240711/exceptions-01.cpp @@ -0,0 +1,21 @@ +#include <iostream> + +using namespace std; + +int main () +{ + try + { + int a = 42; + int b = 0; + if (b == 0) + throw "Ganzzahldivision durch Null"; + int c = a / b; + cout << "c = " << c << endl; + } + catch (const char *msg) + { + cout << "Irgendwas ist schiefgelaufen: " << msg << endl; + } + return 0; +} diff --git a/20240711/exceptions-02.cpp b/20240711/exceptions-02.cpp new file mode 100644 index 0000000000000000000000000000000000000000..fd78e31adaa1b477a0dc6063f25b2fc96b23420e --- /dev/null +++ b/20240711/exceptions-02.cpp @@ -0,0 +1,43 @@ +#include <iostream> + +using namespace std; + +void check_answer (int answer) +{ + if (answer != 42) + { + try + { + if (answer == 10) + throw answer; + else if (answer == 137) + throw "alpha"; + } + catch (int e) + { + cout << "Yeah!" << endl; + throw; + } + throw "bullshit"; + } +} + +int main () +{ + int answer; + cout << "What's your answer? "; + cin >> answer; + try + { + if (answer == 23) + throw answer; + else + check_answer (answer); + } + catch (const char *e) + { + cout << "caught string exception: " << e << endl; + } + cout << "And what was the question?" << endl; + return 0; +} diff --git a/20240711/exceptions-04.cpp b/20240711/exceptions-04.cpp new file mode 100644 index 0000000000000000000000000000000000000000..a20e5690c7d5b42283766cf9b454b528ea4594a8 --- /dev/null +++ b/20240711/exceptions-04.cpp @@ -0,0 +1,28 @@ +#include <iostream> + +using namespace std; + +class philosophy_exception: public exception +{ + int answer; +public: + philosophy_exception (int a) { answer = a; } +}; + +int main () +{ + int answer; + cout << "What's your answer? "; + cin >> answer; + try + { + if (answer != 42) + throw philosophy_exception (answer); + cout << "And what was the question?" << endl; + } + catch (exception &e) + { + cout << "caught exception" << endl; + } + return 0; +} diff --git a/20240711/exceptions-05.cpp b/20240711/exceptions-05.cpp new file mode 100644 index 0000000000000000000000000000000000000000..f61ca5a43495ca4131a6e28bcd2cbedf2119b29f --- /dev/null +++ b/20240711/exceptions-05.cpp @@ -0,0 +1,33 @@ +#include <iostream> + +using namespace std; + +class philosophy_exception: public exception +{ + int answer; +public: + philosophy_exception (int a) { answer = a; } + int get_answer () { return answer; } +}; + +int main () +{ + int answer; + cout << "What's your answer? "; + cin >> answer; + try + { + if (answer != 42) + throw philosophy_exception (answer); + cout << "And what was the question?" << endl; + } + catch (philosophy_exception &e) + { + cout << "caught philosophy exception: " << e.get_answer () << endl; + } + catch (exception &e) + { + cout << "caught exception" << endl; + } + return 0; +} diff --git a/20240711/exceptions-06.cpp b/20240711/exceptions-06.cpp new file mode 100644 index 0000000000000000000000000000000000000000..326b7bca632c77958eaa6afbbab332efe76a86e5 --- /dev/null +++ b/20240711/exceptions-06.cpp @@ -0,0 +1,33 @@ +#include <iostream> + +using namespace std; + +class philosophy_exception: public exception +{ + int answer; +public: + philosophy_exception (int a) { answer = a; } + int get_answer () { return answer; } +}; + +int main () +{ + int answer; + cout << "What's your answer? "; + cin >> answer; + try + { + if (answer != 42) + throw philosophy_exception (answer); + cout << "And what was the question?" << endl; + } + catch (exception &e) + { + cout << "caught exception" << endl; + } + catch (philosophy_exception &e) + { + cout << "caught philosophy exception: " << e.get_answer () << endl; + } + return 0; +} diff --git a/20240711/exceptions-07.cpp b/20240711/exceptions-07.cpp new file mode 100644 index 0000000000000000000000000000000000000000..6a8cf005e189ce42ce3df8df0ce3c53ab3adaa34 --- /dev/null +++ b/20240711/exceptions-07.cpp @@ -0,0 +1,38 @@ +#include <iostream> +#include <sstream> + +using namespace std; + +class philosophy_exception: public exception +{ + int answer; +public: + philosophy_exception (int a) { answer = a; } + virtual const char *what (); +}; + +const char *philosophy_exception::what () +{ + ostringstream buffer; + buffer << "philosophy exception #" << answer; + return buffer.str ().c_str (); +} + +int main () +{ + int answer; + cout << "What's your answer? "; + cin >> answer; + try + { + if (answer == 42) + cout << "And what was the question?" << endl; + else + throw philosophy_exception (answer); + } + catch (exception &e) + { + cout << "caught exception: " << e.what () << endl; + } + return 0; +} diff --git a/20240711/exceptions-08.cpp b/20240711/exceptions-08.cpp new file mode 100644 index 0000000000000000000000000000000000000000..bf3e05ebdd4e42f25c35533198ad4663f4164455 --- /dev/null +++ b/20240711/exceptions-08.cpp @@ -0,0 +1,38 @@ +#include <iostream> +#include <sstream> + +using namespace std; + +class philosophy_exception: public exception +{ + int answer; +public: + philosophy_exception (int a) { answer = a; } + virtual const char *what () const noexcept; +}; + +const char *philosophy_exception::what () const noexcept +{ + ostringstream buffer; + buffer << "philosophy exception #" << answer; + return buffer.str ().c_str (); +} + +int main () +{ + int answer; + cout << "What's your answer? "; + cin >> answer; + try + { + if (answer == 42) + cout << "And what was the question?" << endl; + else + throw philosophy_exception (answer); + } + catch (exception &e) + { + cout << "caught exception: " << e.what () << endl; + } + return 0; +} diff --git a/20240711/exceptions-09.cpp b/20240711/exceptions-09.cpp new file mode 100644 index 0000000000000000000000000000000000000000..0d0eca904e2257f7604a7e87a8337c533f8a1f19 --- /dev/null +++ b/20240711/exceptions-09.cpp @@ -0,0 +1,39 @@ +#include <iostream> +#include <sstream> + +using namespace std; + +class philosophy_exception: public exception +{ + int answer; +public: + philosophy_exception (int a) { answer = a; } + virtual const char *what () const noexcept; +}; + +const char *philosophy_exception::what () const noexcept +{ + return "Marvin"; +// ostringstream buffer; +// buffer << "philosophy exception #" << answer; +// return buffer.str ().c_str (); +} + +int main () +{ + int answer; + cout << "What's your answer? "; + cin >> answer; + try + { + if (answer == 42) + cout << "And what was the question?" << endl; + else + throw philosophy_exception (answer); + } + catch (exception &e) + { + cout << "caught exception: " << e.what () << endl; + } + return 0; +} diff --git a/20240711/exceptions-10.cpp b/20240711/exceptions-10.cpp new file mode 100644 index 0000000000000000000000000000000000000000..35ddd03d74208975a496b879372a28ebda9af7a3 --- /dev/null +++ b/20240711/exceptions-10.cpp @@ -0,0 +1,42 @@ +#include <iostream> +#include <sstream> + +using namespace std; + +class philosophy_exception: public exception +{ + int answer; + static const char *buffer; +public: + philosophy_exception (int a) { answer = a; } + virtual const char *what () const noexcept; +}; + +const char *philosophy_exception::what () const noexcept +{ + return buffer; +// ostringstream buffer; +// buffer << "philosophy exception #" << answer; +// return buffer.str ().c_str (); +} + +const char *philosophy_exception::buffer = "Marvin"; + +int main () +{ + int answer; + cout << "What's your answer? "; + cin >> answer; + try + { + if (answer == 42) + cout << "And what was the question?" << endl; + else + throw philosophy_exception (answer); + } + catch (exception &e) + { + cout << "caught exception: " << e.what () << endl; + } + return 0; +} diff --git a/20240711/exceptions-11.cpp b/20240711/exceptions-11.cpp new file mode 100644 index 0000000000000000000000000000000000000000..6188c398a67c45d754ec0a014b7f93caebc2ce75 --- /dev/null +++ b/20240711/exceptions-11.cpp @@ -0,0 +1,43 @@ +#include <iostream> +#include <sstream> +#include <string.h> + +using namespace std; + +class philosophy_exception: public exception +{ + int answer; + static char buffer[42]; +public: + philosophy_exception (int a) { answer = a; } + virtual const char *what () const noexcept; +}; + +const char *philosophy_exception::what () const noexcept +{ + ostringstream tmp_buffer; + tmp_buffer << "philosophy exception #" << answer; + strcpy (buffer, tmp_buffer.str ().c_str ()); + return buffer; +} + +char philosophy_exception::buffer[42] = "Marvin"; + +int main () +{ + int answer; + cout << "What's your answer? "; + cin >> answer; + try + { + if (answer == 42) + cout << "And what was the question?" << endl; + else + throw philosophy_exception (answer); + } + catch (exception &e) + { + cout << "caught exception: " << e.what () << endl; + } + return 0; +} diff --git a/20240711/iterators-01.cpp b/20240711/iterators-01.cpp new file mode 100644 index 0000000000000000000000000000000000000000..3429606a4490f5ab06ba77371a6850b155706c0a --- /dev/null +++ b/20240711/iterators-01.cpp @@ -0,0 +1,9 @@ +#include <iostream> + +int main () +{ + int prime[5] = { 2, 3, 5, 7, 11 }; + for (int *p = prime; p != prime + 5; p++) + std::cout << *p << std::endl; + return 0; +} diff --git a/20240711/iterators-01.s b/20240711/iterators-01.s new file mode 100644 index 0000000000000000000000000000000000000000..fdacf1012806df5a7a61378b7f7d2d94aec2c129 --- /dev/null +++ b/20240711/iterators-01.s @@ -0,0 +1,107 @@ + .file "iterators-01.cpp" + .text + .globl main + .type main, @function +main: +.LFB1782: + .cfi_startproc + pushq %r13 + .cfi_def_cfa_offset 16 + .cfi_offset 13, -16 + pushq %r12 + .cfi_def_cfa_offset 24 + .cfi_offset 12, -24 + pushq %rbp + .cfi_def_cfa_offset 32 + .cfi_offset 6, -32 + pushq %rbx + .cfi_def_cfa_offset 40 + .cfi_offset 3, -40 + subq $40, %rsp + .cfi_def_cfa_offset 80 + movl $2, (%rsp) + movl $3, 4(%rsp) + movl $5, 8(%rsp) + movl $7, 12(%rsp) + movl $11, 16(%rsp) + movq %rsp, %rbp + leaq _ZSt4cout(%rip), %r12 + jmp .L5 +.L9: + call _ZSt16__throw_bad_castv@PLT +.L3: + movq %r13, %rdi + call _ZNKSt5ctypeIcE13_M_widen_initEv@PLT + movq 0(%r13), %rax + movl $10, %esi + movq %r13, %rdi + call *48(%rax) + movl %eax, %esi +.L4: + movsbl %sil, %esi + movq %rbx, %rdi + call _ZNSo3putEc@PLT + movq %rax, %rdi + call _ZNSo5flushEv@PLT + addq $4, %rbp + leaq 20(%rsp), %rax + cmpq %rax, %rbp + je .L8 +.L5: + movl 0(%rbp), %esi + movq %r12, %rdi + call _ZNSolsEi@PLT + movq %rax, %rbx + movq (%rax), %rax + movq -24(%rax), %rax + movq 240(%rbx,%rax), %r13 + testq %r13, %r13 + je .L9 + cmpb $0, 56(%r13) + je .L3 + movzbl 67(%r13), %esi + jmp .L4 +.L8: + movl $0, %eax + addq $40, %rsp + .cfi_def_cfa_offset 40 + popq %rbx + .cfi_def_cfa_offset 32 + popq %rbp + .cfi_def_cfa_offset 24 + popq %r12 + .cfi_def_cfa_offset 16 + popq %r13 + .cfi_def_cfa_offset 8 + ret + .cfi_endproc +.LFE1782: + .size main, .-main + .type _GLOBAL__sub_I_main, @function +_GLOBAL__sub_I_main: +.LFB2312: + .cfi_startproc + pushq %rbx + .cfi_def_cfa_offset 16 + .cfi_offset 3, -16 + leaq _ZStL8__ioinit(%rip), %rbx + movq %rbx, %rdi + call _ZNSt8ios_base4InitC1Ev@PLT + leaq __dso_handle(%rip), %rdx + movq %rbx, %rsi + movq _ZNSt8ios_base4InitD1Ev@GOTPCREL(%rip), %rdi + call __cxa_atexit@PLT + popq %rbx + .cfi_def_cfa_offset 8 + ret + .cfi_endproc +.LFE2312: + .size _GLOBAL__sub_I_main, .-_GLOBAL__sub_I_main + .section .init_array,"aw" + .align 8 + .quad _GLOBAL__sub_I_main + .local _ZStL8__ioinit + .comm _ZStL8__ioinit,1,1 + .hidden __dso_handle + .ident "GCC: (Debian 12.2.0-14) 12.2.0" + .section .note.GNU-stack,"",@progbits diff --git a/20240711/iterators-02.cpp b/20240711/iterators-02.cpp new file mode 100644 index 0000000000000000000000000000000000000000..13b3f45e3e612ddd4519d9168acc8b35ff86e1c7 --- /dev/null +++ b/20240711/iterators-02.cpp @@ -0,0 +1,9 @@ +#include <iostream> + +int main () +{ + int prime[5] = { 2, 3, 5, 7, 11 }; + for (int *p = prime; p <= prime + 4; p++) + std::cout << *p << std::endl; + return 0; +} diff --git a/20240711/iterators-03.cpp b/20240711/iterators-03.cpp new file mode 100644 index 0000000000000000000000000000000000000000..714302447a77591f24f6812da7f375d37f6e0a25 --- /dev/null +++ b/20240711/iterators-03.cpp @@ -0,0 +1,10 @@ +#include <iostream> +#include <array> + +int main () +{ + std::array <int, 5> prime = { { 2, 3, 5, 7, 11 } }; + for (std::array <int, 5>::iterator p = prime.begin (); p != prime.end (); p++) + std::cout << *p << std::endl; + return 0; +} diff --git a/20240711/iterators-03.s b/20240711/iterators-03.s new file mode 100644 index 0000000000000000000000000000000000000000..df4592ac22974d428002a43fd7013cbc27746278 --- /dev/null +++ b/20240711/iterators-03.s @@ -0,0 +1,107 @@ + .file "iterators-03.cpp" + .text + .globl main + .type main, @function +main: +.LFB1824: + .cfi_startproc + pushq %r13 + .cfi_def_cfa_offset 16 + .cfi_offset 13, -16 + pushq %r12 + .cfi_def_cfa_offset 24 + .cfi_offset 12, -24 + pushq %rbp + .cfi_def_cfa_offset 32 + .cfi_offset 6, -32 + pushq %rbx + .cfi_def_cfa_offset 40 + .cfi_offset 3, -40 + subq $40, %rsp + .cfi_def_cfa_offset 80 + movl $2, (%rsp) + movl $3, 4(%rsp) + movl $5, 8(%rsp) + movl $7, 12(%rsp) + movl $11, 16(%rsp) + movq %rsp, %rbp + leaq _ZSt4cout(%rip), %r12 + jmp .L5 +.L9: + call _ZSt16__throw_bad_castv@PLT +.L3: + movq %r13, %rdi + call _ZNKSt5ctypeIcE13_M_widen_initEv@PLT + movq 0(%r13), %rax + movl $10, %esi + movq %r13, %rdi + call *48(%rax) + movl %eax, %esi +.L4: + movsbl %sil, %esi + movq %rbx, %rdi + call _ZNSo3putEc@PLT + movq %rax, %rdi + call _ZNSo5flushEv@PLT + addq $4, %rbp + leaq 20(%rsp), %rax + cmpq %rax, %rbp + je .L8 +.L5: + movl 0(%rbp), %esi + movq %r12, %rdi + call _ZNSolsEi@PLT + movq %rax, %rbx + movq (%rax), %rax + movq -24(%rax), %rax + movq 240(%rbx,%rax), %r13 + testq %r13, %r13 + je .L9 + cmpb $0, 56(%r13) + je .L3 + movzbl 67(%r13), %esi + jmp .L4 +.L8: + movl $0, %eax + addq $40, %rsp + .cfi_def_cfa_offset 40 + popq %rbx + .cfi_def_cfa_offset 32 + popq %rbp + .cfi_def_cfa_offset 24 + popq %r12 + .cfi_def_cfa_offset 16 + popq %r13 + .cfi_def_cfa_offset 8 + ret + .cfi_endproc +.LFE1824: + .size main, .-main + .type _GLOBAL__sub_I_main, @function +_GLOBAL__sub_I_main: +.LFB2358: + .cfi_startproc + pushq %rbx + .cfi_def_cfa_offset 16 + .cfi_offset 3, -16 + leaq _ZStL8__ioinit(%rip), %rbx + movq %rbx, %rdi + call _ZNSt8ios_base4InitC1Ev@PLT + leaq __dso_handle(%rip), %rdx + movq %rbx, %rsi + movq _ZNSt8ios_base4InitD1Ev@GOTPCREL(%rip), %rdi + call __cxa_atexit@PLT + popq %rbx + .cfi_def_cfa_offset 8 + ret + .cfi_endproc +.LFE2358: + .size _GLOBAL__sub_I_main, .-_GLOBAL__sub_I_main + .section .init_array,"aw" + .align 8 + .quad _GLOBAL__sub_I_main + .local _ZStL8__ioinit + .comm _ZStL8__ioinit,1,1 + .hidden __dso_handle + .ident "GCC: (Debian 12.2.0-14) 12.2.0" + .section .note.GNU-stack,"",@progbits diff --git a/20240711/iterators-04.cpp b/20240711/iterators-04.cpp new file mode 100644 index 0000000000000000000000000000000000000000..3b04a800d57d6ba533671e319c751b1e5348d3ae --- /dev/null +++ b/20240711/iterators-04.cpp @@ -0,0 +1,10 @@ +#include <iostream> +#include <vector> + +int main () +{ + std::vector <int> prime = { { 2, 3, 5, 7, 11 } }; + for (std::vector <int>::iterator p = prime.begin (); p != prime.end (); p++) + std::cout << *p << std::endl; + return 0; +} diff --git a/20240711/iterators-05.cpp b/20240711/iterators-05.cpp new file mode 100644 index 0000000000000000000000000000000000000000..7c62ad835e980d3f2393757ad743d2faad262515 --- /dev/null +++ b/20240711/iterators-05.cpp @@ -0,0 +1,14 @@ +#include <iostream> +#include <vector> + +int main () +{ + std::vector <int> prime = { { 2, 3, 5, 7, 11 } }; + for (std::vector <int>::iterator p = prime.begin (); p != prime.end (); p++) + std::cout << *p << std::endl; + prime.push_back (13); + prime.push_back (17); + for (std::vector <int>::iterator p = prime.begin (); p != prime.end (); p++) + std::cout << *p << std::endl; + return 0; +} diff --git a/20240711/iterators-06.cpp b/20240711/iterators-06.cpp new file mode 100644 index 0000000000000000000000000000000000000000..8698385665db3b31275ec01525d6b84dd8ed3a57 --- /dev/null +++ b/20240711/iterators-06.cpp @@ -0,0 +1,14 @@ +#include <iostream> +#include <vector> + +int main () +{ + std::vector <int> prime = { { 2, 3, 5, 7, 11 } }; + for (auto p = prime.begin (); p != prime.end (); p++) + std::cout << *p << std::endl; + prime.push_back (13); + prime.push_back (17); + for (auto p = prime.begin (); p != prime.end (); p++) + std::cout << *p << std::endl; + return 0; +} diff --git a/20240711/iterators-07.cpp b/20240711/iterators-07.cpp new file mode 100644 index 0000000000000000000000000000000000000000..e7ca0b01d8fab62e914dfc8013d3b7241c09f02f --- /dev/null +++ b/20240711/iterators-07.cpp @@ -0,0 +1,14 @@ +#include <iostream> +#include <vector> + +int main () +{ + std::vector <int> prime = { { 2, 3, 5, 7, 11 } }; + for (auto p : prime) + std::cout << *p << std::endl; + prime.push_back (13); + prime.push_back (17); + for (auto p : prime) + std::cout << *p << std::endl; + return 0; +} diff --git a/20240711/iterators-08.cpp b/20240711/iterators-08.cpp new file mode 100644 index 0000000000000000000000000000000000000000..80328d1ede64acaf9ad67a5c402ee6c361b37fa1 --- /dev/null +++ b/20240711/iterators-08.cpp @@ -0,0 +1,14 @@ +#include <iostream> +#include <vector> + +int main () +{ + std::vector <int> prime = { { 2, 3, 5, 7, 11 } }; + for (auto &p : prime) + std::cout << p << std::endl; + prime.push_back (13); + prime.push_back (17); + for (auto &p : prime) + std::cout << p << std::endl; + return 0; +} diff --git a/20240711/iterators-09.cpp b/20240711/iterators-09.cpp new file mode 100644 index 0000000000000000000000000000000000000000..a1c2005a263464e99776974404086a75e3f46a8a --- /dev/null +++ b/20240711/iterators-09.cpp @@ -0,0 +1,14 @@ +#include <iostream> +#include <list> + +int main () +{ + std::list <int> prime = { { 2, 3, 5, 7, 11 } }; + for (auto &p : prime) + std::cout << p << std::endl; + prime.push_back (13); + prime.push_back (17); + for (auto &p : prime) + std::cout << p << std::endl; + return 0; +} diff --git a/20240711/r-value-references-01.cpp b/20240711/r-value-references-01.cpp new file mode 100644 index 0000000000000000000000000000000000000000..0be06b977c2dcf420c879f652ab0dae78f80467b --- /dev/null +++ b/20240711/r-value-references-01.cpp @@ -0,0 +1,46 @@ +#include <iostream> +#include <string> + +using namespace std; + +class TComplicated +{ + string *content; + +public: + TComplicated () + { + content = NULL; + } + + TComplicated (const char *content) + { + this->content = new string (content); + } + + ~TComplicated () + { + cout << "~TComplicated (): content = "; + print (); + if (content) + delete content; + content = NULL; + } + + void print () + { + if (content) + cout << *content << endl; + else + cout << "(NULL)" << endl; + } +}; + +int main () +{ + TComplicated c = "Hello, world!"; + c.print (); + TComplicated d; + d.print (); + return 0; +} diff --git a/20240711/r-value-references-02.cpp b/20240711/r-value-references-02.cpp new file mode 100644 index 0000000000000000000000000000000000000000..5477fbad2e6784fdeca06b39dec9eecfcd0a9a59 --- /dev/null +++ b/20240711/r-value-references-02.cpp @@ -0,0 +1,54 @@ +#include <iostream> +#include <string> + +using namespace std; + +class TComplicated +{ + string *content; + +public: + TComplicated () + { + content = NULL; + } + + TComplicated (const char *content) + { + this->content = new string (content); + } + + TComplicated (const TComplicated &src) + { + if (src.content) + this->content = new string (*src.content); + else + this->content = NULL; + } + + ~TComplicated () + { + cout << "~TComplicated (): content = "; + print (); + if (content) + delete content; + content = NULL; + } + + void print () + { + if (content) + cout << *content << endl; + else + cout << "(NULL)" << endl; + } +}; + +int main () +{ + TComplicated c = "Hello, world!"; + c.print (); + TComplicated d = c; + d.print (); + return 0; +} diff --git a/20240711/r-value-references-03.cpp b/20240711/r-value-references-03.cpp new file mode 100644 index 0000000000000000000000000000000000000000..35be424c2f9ec9fcbd3370ad18332995eff7cff9 --- /dev/null +++ b/20240711/r-value-references-03.cpp @@ -0,0 +1,57 @@ +#include <iostream> +#include <string> + +using namespace std; + +class TComplicated +{ + string *content; + +public: + TComplicated () + { + content = NULL; + } + + TComplicated (const char *content) + { + this->content = new string (content); + } + + TComplicated (const TComplicated &src) + { + if (src.content) + this->content = new string (*src.content); + else + this->content = NULL; + } + + ~TComplicated () + { + cout << "~TComplicated (): content = "; + print (); + if (content) + delete content; + content = NULL; + } + + void print () + { + if (content) + cout << *content << endl; + else + cout << "(NULL)" << endl; + } +}; + +void foo (TComplicated x) +{ + x.print (); +} + +int main () +{ + TComplicated c = "Hello, world!"; + foo (c); + return 0; +} diff --git a/20240711/r-value-references-04.cpp b/20240711/r-value-references-04.cpp new file mode 100644 index 0000000000000000000000000000000000000000..e7b04d87e8bb8cc9bf74a3e55ae2698fbba416de --- /dev/null +++ b/20240711/r-value-references-04.cpp @@ -0,0 +1,54 @@ +#include <iostream> +#include <string> + +using namespace std; + +class TComplicated +{ + string *content; + +public: + TComplicated () + { + content = NULL; + } + + TComplicated (const char *content) + { + this->content = new string (content); + } + + TComplicated (const TComplicated &src) + { + if (src.content) + this->content = new string (*src.content); + else + this->content = NULL; + } + + ~TComplicated () + { + cout << "~TComplicated (): content = "; + print (); + if (content) + delete content; + content = NULL; + } + + void print () + { + if (content) + cout << *content << endl; + else + cout << "(NULL)" << endl; + } +}; + +int main () +{ + TComplicated c = "Hello, world!"; + c.print (); + TComplicated d = move (c); + d.print (); + return 0; +} diff --git a/20240711/r-value-references-05.cpp b/20240711/r-value-references-05.cpp new file mode 100644 index 0000000000000000000000000000000000000000..cd82834382d06914a97e4f05b997476770d02acb --- /dev/null +++ b/20240711/r-value-references-05.cpp @@ -0,0 +1,60 @@ +#include <iostream> +#include <string> + +using namespace std; + +const bool something_really_complicated = true; + +class TComplicated +{ + string *content; + +public: + TComplicated () + { + content = NULL; + } + + TComplicated (const char *content) + { + this->content = new string (content); + } + + TComplicated (const TComplicated &src) + { + if (src.content) + this->content = new string (*src.content); + else + this->content = NULL; + } + + ~TComplicated () + { + cout << "~TComplicated (): content = "; + print (); + if (content) + delete content; + content = NULL; + } + + void print () + { + if (content) + cout << *content << endl; + else + cout << "(NULL)" << endl; + } +}; + +int main () +{ + TComplicated d; + if (something_really_complicated) + { + TComplicated c = "Hello, world!"; + c.print (); + d = c; + } + d.print (); + return 0; +} diff --git a/20240711/r-value-references-06.cpp b/20240711/r-value-references-06.cpp new file mode 100644 index 0000000000000000000000000000000000000000..f5653e3f064b813c83bec63d7ffa7344f15caa46 --- /dev/null +++ b/20240711/r-value-references-06.cpp @@ -0,0 +1,69 @@ +#include <iostream> +#include <string> + +using namespace std; + +const bool something_really_complicated = true; + +class TComplicated +{ + string *content; + +public: + TComplicated () + { + content = NULL; + } + + TComplicated (const char *content) + { + this->content = new string (content); + } + + TComplicated (const TComplicated &src) + { + if (src.content) + this->content = new string (*src.content); + else + this->content = NULL; + } + + ~TComplicated () + { + cout << "~TComplicated (): content = "; + print (); + if (content) + delete content; + content = NULL; + } + + TComplicated &operator = (const TComplicated &src) + { + if (src.content) + this->content = new string (*src.content); + else + this->content = NULL; + return *this; + } + + void print () + { + if (content) + cout << *content << endl; + else + cout << "(NULL)" << endl; + } +}; + +int main () +{ + TComplicated d; + if (something_really_complicated) + { + TComplicated c = "Hello, world!"; + c.print (); + d = c; + } + d.print (); + return 0; +} diff --git a/20240711/r-value-references-07.cpp b/20240711/r-value-references-07.cpp new file mode 100644 index 0000000000000000000000000000000000000000..690be43028b0216ae74b27beb65f050b7425c477 --- /dev/null +++ b/20240711/r-value-references-07.cpp @@ -0,0 +1,69 @@ +#include <iostream> +#include <string> + +using namespace std; + +const bool something_really_complicated = true; + +class TComplicated +{ + string *content; + +public: + TComplicated () + { + content = NULL; + } + + TComplicated (const char *content) + { + this->content = new string (content); + } + + TComplicated (const TComplicated &src) + { + if (src.content) + this->content = new string (*src.content); + else + this->content = NULL; + } + + ~TComplicated () + { + cout << "~TComplicated (): content = "; + print (); + if (content) + delete content; + content = NULL; + } + + TComplicated &operator = (const TComplicated &src) + { + if (src.content) + this->content = new string (*src.content); + else + this->content = NULL; + return *this; + } + + void print () + { + if (content) + cout << *content << endl; + else + cout << "(NULL)" << endl; + } +}; + +int main () +{ + TComplicated d; + if (something_really_complicated) + { + TComplicated c = "Hello, world!"; + c.print (); + d = move (c); + } + d.print (); + return 0; +} diff --git a/20240711/r-value-references-08.cpp b/20240711/r-value-references-08.cpp new file mode 100644 index 0000000000000000000000000000000000000000..7a6de9003cc849a4123b6f22bdeafcfec4306140 --- /dev/null +++ b/20240711/r-value-references-08.cpp @@ -0,0 +1,76 @@ +#include <iostream> +#include <string> + +using namespace std; + +const bool something_really_complicated = true; + +class TComplicated +{ + string *content; + +public: + TComplicated () + { + content = NULL; + } + + TComplicated (const char *content) + { + this->content = new string (content); + } + + TComplicated (const TComplicated &src) + { + if (src.content) + this->content = new string (*src.content); + else + this->content = NULL; + } + + ~TComplicated () + { + cout << "~TComplicated (): content = "; + print (); + if (content) + delete content; + content = NULL; + } + + TComplicated &operator = (const TComplicated &src) + { + if (src.content) + this->content = new string (*src.content); + else + this->content = NULL; + return *this; + } + + TComplicated &operator = (TComplicated &&src) + { + this->content = src.content; + src.content = NULL; + return *this; + } + + void print () + { + if (content) + cout << *content << endl; + else + cout << "(NULL)" << endl; + } +}; + +int main () +{ + TComplicated d; + if (something_really_complicated) + { + TComplicated c = "Hello, world!"; + c.print (); + d = move (c); + } + d.print (); + return 0; +} diff --git a/20240711/r-value-references-09.cpp b/20240711/r-value-references-09.cpp new file mode 100644 index 0000000000000000000000000000000000000000..fba9c1cab79269d879f19119aad0f03aa4262ac8 --- /dev/null +++ b/20240711/r-value-references-09.cpp @@ -0,0 +1,76 @@ +#include <iostream> +#include <string> + +using namespace std; + +const bool something_really_complicated = true; + +class TComplicated +{ + string *content; + +public: + TComplicated () + { + content = NULL; + } + + TComplicated (const char *content) + { + this->content = new string (content); + } + + TComplicated (const TComplicated &src) + { + if (src.content) + this->content = new string (*src.content); + else + this->content = NULL; + } + + ~TComplicated () + { + cout << "~TComplicated (): content = "; + print (); + if (content) + delete content; + content = NULL; + } + + TComplicated &operator = (const TComplicated &src) + { + if (src.content) + this->content = new string (*src.content); + else + this->content = NULL; + return *this; + } + + TComplicated &operator = (TComplicated &&src) + { + this->content = src.content; + src.content = NULL; + return *this; + } + + void print () + { + if (content) + cout << *content << endl; + else + cout << "(NULL)" << endl; + } +}; + +int main () +{ + TComplicated d; + if (something_really_complicated) + { + TComplicated c = "Hello, world!"; + c.print (); + d = c; + } + d.print (); + return 0; +} diff --git a/20240711/r-value-references-10.cpp b/20240711/r-value-references-10.cpp new file mode 100644 index 0000000000000000000000000000000000000000..bab07b5da43a4ce5e61a003bbeb138aa8aea59d1 --- /dev/null +++ b/20240711/r-value-references-10.cpp @@ -0,0 +1,76 @@ +#include <iostream> +#include <string> + +using namespace std; + +const bool something_really_complicated = true; + +class TComplicated +{ + string *content; + +public: + TComplicated () + { + content = NULL; + } + + TComplicated (const char *content) + { + this->content = new string (content); + } + + TComplicated (const TComplicated &src) + { + if (src.content) + this->content = new string (*src.content); + else + this->content = NULL; + } + + ~TComplicated () + { + cout << "~TComplicated (): content = "; + print (); + if (content) + delete content; + content = NULL; + } + + TComplicated &operator = (const TComplicated &src) + { + if (src.content) + this->content = new string (*src.content); + else + this->content = NULL; + return *this; + } + + TComplicated &operator = (TComplicated &&src) + { + this->content = src.content; + src.content = NULL; + return *this; + } + + void print () + { + if (content) + cout << *content << endl; + else + cout << "(NULL)" << endl; + } +}; + +int main () +{ + TComplicated d; + if (something_really_complicated) + { + TComplicated c = "Hello, world!"; + c.print (); + d = (TComplicated &&) c; + } + d.print (); + return 0; +} diff --git a/20240711/r-value-references-11.cpp b/20240711/r-value-references-11.cpp new file mode 100644 index 0000000000000000000000000000000000000000..253e7547b2486ed9c1bef096535bfc3eb747bfc4 --- /dev/null +++ b/20240711/r-value-references-11.cpp @@ -0,0 +1,77 @@ +#include <iostream> +#include <string> + +using namespace std; + +const bool something_really_complicated = true; + +class TComplicated +{ + string *content; + +public: + TComplicated () + { + content = NULL; + } + + TComplicated (const char *content) + { + this->content = new string (content); + } + + TComplicated (const TComplicated &src) + { + if (src.content) + this->content = new string (*src.content); + else + this->content = NULL; + } + + ~TComplicated () + { + cout << "~TComplicated (): content = "; + print (); + if (content) + delete content; + content = NULL; + } + + TComplicated &operator = (const TComplicated &src) + { + if (src.content) + this->content = new string (*src.content); + else + this->content = NULL; + return *this; + } + + void move (TComplicated &src) + { + if (content) + delete content; + this->content = src.content; + src.content = NULL; + } + + void print () + { + if (content) + cout << *content << endl; + else + cout << "(NULL)" << endl; + } +}; + +int main () +{ + TComplicated d; + if (something_really_complicated) + { + TComplicated c = "Hello, world!"; + c.print (); + d.move (c); + } + d.print (); + return 0; +} diff --git a/20240711/r-value-references-12.cpp b/20240711/r-value-references-12.cpp new file mode 100644 index 0000000000000000000000000000000000000000..44524f706225e2d2aa7a0d33880809f7dcc7fe9f --- /dev/null +++ b/20240711/r-value-references-12.cpp @@ -0,0 +1,83 @@ +#include <iostream> +#include <string> + +using namespace std; + +const bool something_really_complicated = true; + +class TComplicated +{ + string *content; + +public: + TComplicated () + { + content = NULL; + } + + TComplicated (const char *content) + { + this->content = new string (content); + } + + TComplicated (const TComplicated &src) + { + if (src.content) + this->content = new string (*src.content); + else + this->content = NULL; + } + + ~TComplicated () + { + cout << "~TComplicated (): content = "; + print (); + if (content) + delete content; + content = NULL; + } + + TComplicated &operator = (const TComplicated &src) + { + if (src.content) + this->content = new string (*src.content); + else + this->content = NULL; + return *this; + } + + string *purge_content () + { + string *save_content = content; + content = NULL; + return save_content; + } + + void move (TComplicated &src) + { + if (content) + delete content; + this->content = src.purge_content (); + } + + void print () + { + if (content) + cout << *content << endl; + else + cout << "(NULL)" << endl; + } +}; + +int main () +{ + TComplicated d; + if (something_really_complicated) + { + TComplicated c = "Hello, world!"; + c.print (); + d.move (c); + } + d.print (); + return 0; +} diff --git a/20240711/smart-pointers-01.cpp b/20240711/smart-pointers-01.cpp new file mode 100644 index 0000000000000000000000000000000000000000..ab07005528a3039e9b43a44038d2070ae94d25fb --- /dev/null +++ b/20240711/smart-pointers-01.cpp @@ -0,0 +1,13 @@ +#include <iostream> +#include <string> + +using std::cout; +using std::endl; +using std::string; + +int main () +{ + string *hello = new string ("Hello, world!"); + cout << *hello << endl; + return 0; +} diff --git a/20240711/smart-pointers-02.cpp b/20240711/smart-pointers-02.cpp new file mode 100644 index 0000000000000000000000000000000000000000..7fedf98b8e0474bee64563c39884d302cfe8cbb7 --- /dev/null +++ b/20240711/smart-pointers-02.cpp @@ -0,0 +1,15 @@ +#include <iostream> +#include <string> + +using std::cout; +using std::endl; +using std::string; + +int main () +{ + string *hello = new string ("Hello, world!"); + cout << *hello << endl; + delete hello; + cout << *hello << endl; + return 0; +} diff --git a/20240711/smart-pointers-03.cpp b/20240711/smart-pointers-03.cpp new file mode 100644 index 0000000000000000000000000000000000000000..8c7313dcfe63fc28202ba6de4eac1a4c9297051e --- /dev/null +++ b/20240711/smart-pointers-03.cpp @@ -0,0 +1,18 @@ +#include <iostream> +#include <string> + +using std::cout; +using std::endl; +using std::string; + +int main () +{ + string *hello = new string ("Hello, world!"); + cout << *hello << endl; + delete hello; + if (hello) + cout << *hello << endl; + else + cout << "Variable wurde bereits freigegeben." << endl; + return 0; +} diff --git a/20240711/smart-pointers-04.cpp b/20240711/smart-pointers-04.cpp new file mode 100644 index 0000000000000000000000000000000000000000..4184ff5e3862b702efe3fe4a1dc197cdc02b839e --- /dev/null +++ b/20240711/smart-pointers-04.cpp @@ -0,0 +1,21 @@ +#include <stdio.h> +#include <iostream> +#include <string> + +using std::cout; +using std::endl; +using std::string; + +int main () +{ + string *hello = new string ("Hello, world!"); + printf ("hello = 0x%016zx\n", (size_t) hello); + cout << *hello << endl; + delete hello; + printf ("hello = 0x%016zx\n", (size_t) hello); + if (hello) + cout << *hello << endl; + else + cout << "Variable wurde bereits freigegeben." << endl; + return 0; +} diff --git a/20240711/smart-pointers-05.cpp b/20240711/smart-pointers-05.cpp new file mode 100644 index 0000000000000000000000000000000000000000..188c175b6903e5b79cb43c2d7fd070b36b73eda7 --- /dev/null +++ b/20240711/smart-pointers-05.cpp @@ -0,0 +1,22 @@ +#include <stdio.h> +#include <iostream> +#include <string> + +using std::cout; +using std::endl; +using std::string; + +int main () +{ + string *hello = new string ("Hello, world!"); + printf ("hello = 0x%016zx\n", (size_t) hello); + cout << *hello << endl; + delete hello; + hello = NULL; + printf ("hello = 0x%016zx\n", (size_t) hello); + if (hello) + cout << *hello << endl; + else + cout << "Variable wurde bereits freigegeben." << endl; + return 0; +} diff --git a/20240711/smart-pointers-06.cpp b/20240711/smart-pointers-06.cpp new file mode 100644 index 0000000000000000000000000000000000000000..e924ba3a32354ed5a106377701fa5a4d9d55379c --- /dev/null +++ b/20240711/smart-pointers-06.cpp @@ -0,0 +1,17 @@ +#include <stdio.h> +#include <iostream> +#include <string> + +using std::cout; +using std::endl; +using std::string; + +int main () +{ + string *hello; + if (hello) + cout << *hello << endl; + else + cout << "Kein gültiger Zeiger." << endl; + return 0; +} diff --git a/20240711/smart-pointers-07.cpp b/20240711/smart-pointers-07.cpp new file mode 100644 index 0000000000000000000000000000000000000000..f70a5b327c1e05dc5e64413c1f31122f4e76c3dc --- /dev/null +++ b/20240711/smart-pointers-07.cpp @@ -0,0 +1,18 @@ +#include <iostream> +#include <string> +#include <memory> + +using std::cout; +using std::endl; +using std::string; +using std::shared_ptr; + +int main () +{ + shared_ptr <string> hello = new string ("Hello, world!"); + if (hello) + cout << *hello << endl; + else + cout << "Kein gültiger Zeiger." << endl; + return 0; +} diff --git a/20240711/smart-pointers-08.cpp b/20240711/smart-pointers-08.cpp new file mode 100644 index 0000000000000000000000000000000000000000..46bd15c76946227dadb6d267b3985f30bdbb4b92 --- /dev/null +++ b/20240711/smart-pointers-08.cpp @@ -0,0 +1,18 @@ +#include <iostream> +#include <string> +#include <memory> + +using std::cout; +using std::endl; +using std::string; +using std::shared_ptr; + +int main () +{ + shared_ptr <string> hello (new string ("Hello, world!")); + if (hello) + cout << *hello << endl; + else + cout << "Kein gültiger Zeiger." << endl; + return 0; +} diff --git a/20240711/smart-pointers-09.cpp b/20240711/smart-pointers-09.cpp new file mode 100644 index 0000000000000000000000000000000000000000..83b34a215c329db45dd03138ad0f2bdf81ce2f1d --- /dev/null +++ b/20240711/smart-pointers-09.cpp @@ -0,0 +1,18 @@ +#include <iostream> +#include <string> +#include <memory> + +using std::cout; +using std::endl; +using std::string; +using std::shared_ptr; + +int main () +{ + shared_ptr <string> hello ("Hello, world!"); + if (hello) + cout << *hello << endl; + else + cout << "Kein gültiger Zeiger." << endl; + return 0; +} diff --git a/20240711/smart-pointers-10.cpp b/20240711/smart-pointers-10.cpp new file mode 100644 index 0000000000000000000000000000000000000000..33ef969fdaf5026bbaaff1bc7c474e3547536eb9 --- /dev/null +++ b/20240711/smart-pointers-10.cpp @@ -0,0 +1,25 @@ +#include <iostream> +#include <string> +#include <memory> + +using std::cout; +using std::endl; +using std::string; +using std::shared_ptr; + +int main () +{ + shared_ptr <string> hello2; + if (hello2) + cout << *hello2 << endl; + else + cout << "(String noch nicht vorhanden)" << endl; + if (1) + { + shared_ptr <string> hello (new string ("Hello, world!")); + cout << *hello << endl; + hello2 = hello; + } + cout << *hello2 << endl; + return 0; +} diff --git a/20240711/smart-pointers-11.cpp b/20240711/smart-pointers-11.cpp new file mode 100644 index 0000000000000000000000000000000000000000..803168363150d322ae248e70d15ffc955c54a332 --- /dev/null +++ b/20240711/smart-pointers-11.cpp @@ -0,0 +1,26 @@ +#include <iostream> +#include <string> +#include <memory> + +using std::cout; +using std::endl; +using std::string; +using std::shared_ptr; + +struct container +{ + string content; + container (const char *s) + { + content = s; + } + shared_ptr <container> next; +}; + +int main () +{ + shared_ptr <container> hello1 (new container ("Hello,")); + shared_ptr <container> hello2 (new container ("world!")); + cout << hello1->content << " " << hello2->content << endl; + return 0; +} diff --git a/20240711/smart-pointers-12.cpp b/20240711/smart-pointers-12.cpp new file mode 100644 index 0000000000000000000000000000000000000000..b80cff7570ae19f6d3b843480afaad4fbd1b7f63 --- /dev/null +++ b/20240711/smart-pointers-12.cpp @@ -0,0 +1,28 @@ +#include <iostream> +#include <string> +#include <memory> + +using std::cout; +using std::endl; +using std::string; +using std::shared_ptr; + +struct container +{ + string content; + container (const char *s) + { + content = s; + } + shared_ptr <container> next; +}; + +int main () +{ + shared_ptr <container> hello1 (new container ("Hello,")); + shared_ptr <container> hello2 (new container ("world!")); + hello1->next = hello2; + hello2->next = hello1; + cout << hello1->content << " " << hello2->content << endl; + return 0; +} diff --git a/20240711/smart-pointers-13.cpp b/20240711/smart-pointers-13.cpp new file mode 100644 index 0000000000000000000000000000000000000000..f044de4d01b6ee14cdec6fae8ffbfc105f9f7be1 --- /dev/null +++ b/20240711/smart-pointers-13.cpp @@ -0,0 +1,32 @@ +#include <iostream> +#include <string> +#include <memory> + +using std::cout; +using std::endl; +using std::string; +using std::shared_ptr; + +struct container +{ + string content; + container (const char *s) + { + content = s; + } + ~container () + { + cout << "Bye: " << content << endl; + } + shared_ptr <container> next; +}; + +int main () +{ + shared_ptr <container> hello1 (new container ("Hello,")); + shared_ptr <container> hello2 (new container ("world!")); +// hello1->next = hello2; +// hello2->next = hello1; + cout << hello1->content << " " << hello2->content << endl; + return 0; +} diff --git a/20240711/smart-pointers-14.cpp b/20240711/smart-pointers-14.cpp new file mode 100644 index 0000000000000000000000000000000000000000..8d11a2d3f7d5b3af210692aa7a82ac207d648430 --- /dev/null +++ b/20240711/smart-pointers-14.cpp @@ -0,0 +1,32 @@ +#include <iostream> +#include <string> +#include <memory> + +using std::cout; +using std::endl; +using std::string; +using std::shared_ptr; + +struct container +{ + string content; + container (const char *s) + { + content = s; + } + ~container () + { + cout << "Bye: " << content << endl; + } + shared_ptr <container> next; +}; + +int main () +{ + shared_ptr <container> hello1 (new container ("Hello,")); + shared_ptr <container> hello2 (new container ("world!")); + hello1->next = hello2; + hello2->next = hello1; + cout << hello1->content << " " << hello2->content << endl; + return 0; +} diff --git a/20240711/smart-pointers-15.cpp b/20240711/smart-pointers-15.cpp new file mode 100644 index 0000000000000000000000000000000000000000..8a91989ed071820ff6163078837acdeae53626f7 --- /dev/null +++ b/20240711/smart-pointers-15.cpp @@ -0,0 +1,33 @@ +#include <iostream> +#include <string> +#include <memory> + +using std::cout; +using std::endl; +using std::string; +using std::shared_ptr; +using std::weak_ptr; + +struct container +{ + string content; + container (const char *s) + { + content = s; + } + ~container () + { + cout << "Bye: " << content << endl; + } + weak_ptr <container> next; +}; + +int main () +{ + shared_ptr <container> hello1 (new container ("Hello,")); + shared_ptr <container> hello2 (new container ("world!")); + hello1->next = hello2; + hello2->next = hello1; + cout << hello1->content << " " << hello2->content << endl; + return 0; +} diff --git a/20240711/smart-pointers-16.cpp b/20240711/smart-pointers-16.cpp new file mode 100644 index 0000000000000000000000000000000000000000..dcf942e1ba0baf0b4784d4d790a224bfed24753d --- /dev/null +++ b/20240711/smart-pointers-16.cpp @@ -0,0 +1,18 @@ +#include <iostream> +#include <string> +#include <memory> + +using std::cout; +using std::endl; +using std::string; +using std::shared_ptr; +using std::weak_ptr; + +int main () +{ + weak_ptr <string> p; + shared_ptr <string> hello (new string ("Hello, world!")); + p = hello; + cout << *p << endl; + return 0; +} diff --git a/20240711/smart-pointers-17.cpp b/20240711/smart-pointers-17.cpp new file mode 100644 index 0000000000000000000000000000000000000000..f9d845c11f6d20f1ab81f2b032637142a8af1a0a --- /dev/null +++ b/20240711/smart-pointers-17.cpp @@ -0,0 +1,19 @@ +#include <iostream> +#include <string> +#include <memory> + +using std::cout; +using std::endl; +using std::string; +using std::shared_ptr; +using std::weak_ptr; + +int main () +{ + weak_ptr <string> p; + shared_ptr <string> hello (new string ("Hello, world!")); + p = hello; + shared_ptr <string> q = p.lock (); + cout << *q << endl; + return 0; +} diff --git a/20240711/smart-pointers-18.cpp b/20240711/smart-pointers-18.cpp new file mode 100644 index 0000000000000000000000000000000000000000..d9f3327c183db5f3fa7684aaf86f7ea4605f2020 --- /dev/null +++ b/20240711/smart-pointers-18.cpp @@ -0,0 +1,16 @@ +#include <iostream> +#include <string> + +using std::cout; +using std::endl; +using std::string; + +int main () +{ + string *p; + string *hello = new string ("Hello, world!"); + p = hello; + string *q = p; + cout << *q << endl; + return 0; +} diff --git a/20240711/smart-pointers-19.cpp b/20240711/smart-pointers-19.cpp new file mode 100644 index 0000000000000000000000000000000000000000..27c4e636d72163088293d3106bf2b28a502bfc19 --- /dev/null +++ b/20240711/smart-pointers-19.cpp @@ -0,0 +1,22 @@ +#include <iostream> +#include <string> +#include <memory> + +using std::cout; +using std::endl; +using std::string; +using std::shared_ptr; +using std::weak_ptr; + +int main () +{ + weak_ptr <string> p; + shared_ptr <string> hello (new string ("Hello, world!")); + p = hello; + shared_ptr <string> q = p.lock (); + if (q) + cout << *q << endl; + else + cout << "Zeiger wurde bereits freigegeben." << endl; + return 0; +} diff --git a/20240711/smart-pointers-20.cpp b/20240711/smart-pointers-20.cpp new file mode 100644 index 0000000000000000000000000000000000000000..50e95cf6374133061ec4534acb17b3c2a2c3a71e --- /dev/null +++ b/20240711/smart-pointers-20.cpp @@ -0,0 +1,25 @@ +#include <iostream> +#include <string> +#include <memory> + +using std::cout; +using std::endl; +using std::string; +using std::shared_ptr; +using std::weak_ptr; + +int main () +{ + weak_ptr <string> p; + if (1) + { + shared_ptr <string> hello (new string ("Hello, world!")); + p = hello; + } + shared_ptr <string> q = p.lock (); + if (q) + cout << *q << endl; + else + cout << "Zeiger wurde bereits freigegeben." << endl; + return 0; +} diff --git a/20240711/smart-pointers-21.cpp b/20240711/smart-pointers-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..5699eaedb0e8be001d28c5a2e8965c652653f059 --- /dev/null +++ b/20240711/smart-pointers-21.cpp @@ -0,0 +1,26 @@ +#include <iostream> +#include <string> +#include <memory> + +using std::cout; +using std::endl; +using std::string; +using std::shared_ptr; +using std::weak_ptr; + +int main () +{ + weak_ptr <string> p; + shared_ptr <string> q; + if (1) + { + shared_ptr <string> hello (new string ("Hello, world!")); + p = hello; + q = p.lock (); + } + if (q) + cout << *q << endl; + else + cout << "Zeiger wurde bereits freigegeben." << endl; + return 0; +} diff --git a/20240711/smart-pointers-22.cpp b/20240711/smart-pointers-22.cpp new file mode 100644 index 0000000000000000000000000000000000000000..c8688a6fbc492b0fd16b1bb69d4672adf0dc9881 --- /dev/null +++ b/20240711/smart-pointers-22.cpp @@ -0,0 +1,19 @@ +#include <iostream> +#include <string> + +using std::cout; +using std::endl; +using std::string; + +int main () +{ + string *hello = new string ("Hello, world!"); + string *storage = NULL; + + storage = hello; + hello = NULL; + hello = new string ("Hallo, Welt!"); + + cout << *storage << endl; + return 0; +} diff --git a/20240711/smart-pointers-23.cpp b/20240711/smart-pointers-23.cpp new file mode 100644 index 0000000000000000000000000000000000000000..a9f8fd8bdae9c742bad60b5012a78a6e7c3c1628 --- /dev/null +++ b/20240711/smart-pointers-23.cpp @@ -0,0 +1,24 @@ +#include <iostream> +#include <string> + +using std::cout; +using std::endl; +using std::string; + +int main () +{ + string *hello = new string ("Hello, world!"); + string *storage = NULL; + + storage = hello; + hello = NULL; + + cout << *storage << endl; + + if (hello) + delete hello; + if (storage) + delete storage; + + return 0; +} diff --git a/20240711/smart-pointers-24.cpp b/20240711/smart-pointers-24.cpp new file mode 100644 index 0000000000000000000000000000000000000000..a3000f8f99fef966c9c692e5d06b7f2cdda38edf --- /dev/null +++ b/20240711/smart-pointers-24.cpp @@ -0,0 +1,24 @@ +#include <iostream> +#include <string> + +using std::cout; +using std::endl; +using std::string; + +int main () +{ + string *hello = new string ("Hello, world!"); + string *storage = NULL; + + storage = hello; +// hello = NULL; + + cout << *storage << endl; + + if (hello) + delete hello; + if (storage) + delete storage; + + return 0; +} diff --git a/20240711/smart-pointers-25.cpp b/20240711/smart-pointers-25.cpp new file mode 100644 index 0000000000000000000000000000000000000000..5ade0eb09b9d181ca4b0df5d292617e1f150ba7e --- /dev/null +++ b/20240711/smart-pointers-25.cpp @@ -0,0 +1,30 @@ +#include <iostream> +#include <string> + +using std::cout; +using std::endl; +using std::string; + +int main () +{ + string *hello = new string ("Hello, world!"); + string *storage = NULL; + + storage = hello; + + // Jetzt kommt ein anderer Thread und macht: + + if (hello) + delete hello; + + // Jetzt sind wir wieder im ursprünglichen Thread. + + hello = NULL; + + cout << *storage << endl; + + if (storage) + delete storage; + + return 0; +} diff --git a/20240711/smart-pointers-26.cpp b/20240711/smart-pointers-26.cpp new file mode 100644 index 0000000000000000000000000000000000000000..db13d3667419c0c014843f6d6ef9a9b90be42d00 --- /dev/null +++ b/20240711/smart-pointers-26.cpp @@ -0,0 +1,32 @@ +#include <iostream> +#include <string> +#include <memory> + +using std::cout; +using std::endl; +using std::string; +using std::unique_ptr; + +int main () +{ + unique_ptr <string> hello (new string ("Hello, world!")); + unique_ptr <string> storage; + + storage = hello; + + // Jetzt kommt ein anderer Thread und macht: + + if (hello) + delete hello; + + // Jetzt sind wir wieder im ursprünglichen Thread. + + hello = NULL; + + cout << *storage << endl; + + if (storage) + delete storage; + + return 0; +} diff --git a/20240711/smart-pointers-27.cpp b/20240711/smart-pointers-27.cpp new file mode 100644 index 0000000000000000000000000000000000000000..85d4d7d22ec5da6bbb0d1939b6be4ae1c22a13d9 --- /dev/null +++ b/20240711/smart-pointers-27.cpp @@ -0,0 +1,30 @@ +#include <iostream> +#include <string> +#include <memory> + +using std::cout; +using std::endl; +using std::string; +using std::unique_ptr; + +int main () +{ + unique_ptr <string> hello (new string ("Hello, world!")); + unique_ptr <string> storage; + + storage = move (hello); // setzt gleichzeitig auf NULL + + // Jetzt kommt ein anderer Thread und macht: + + if (hello) + delete hello; + + // Jetzt sind wir wieder im ursprünglichen Thread. + + cout << *storage << endl; + + if (storage) + delete storage; + + return 0; +} diff --git a/20240711/smart-pointers-28.cpp b/20240711/smart-pointers-28.cpp new file mode 100644 index 0000000000000000000000000000000000000000..4abf8d4ad82af564cef0d90152f19c340a850525 --- /dev/null +++ b/20240711/smart-pointers-28.cpp @@ -0,0 +1,26 @@ +#include <iostream> +#include <string> +#include <memory> + +using std::cout; +using std::endl; +using std::string; +using std::unique_ptr; + +int main () +{ + unique_ptr <string> hello (new string ("Hello, world!")); + unique_ptr <string> storage; + + storage = move (hello); // setzt gleichzeitig auf NULL + + // Ein anderer Thread hat keine Chance mehr, sich zwischen die Zeiger-Zuweisung + // und das Auf-NULL-Setzen zu schieben. + + cout << *storage << endl; + + // if (storage) + // delete storage; --> geschieht automatisch + + return 0; +} diff --git a/20240711/templates-01.cpp b/20240711/templates-01.cpp new file mode 100644 index 0000000000000000000000000000000000000000..b6d013e09364e0d8cf5569215fe4f4d935c69d74 --- /dev/null +++ b/20240711/templates-01.cpp @@ -0,0 +1,23 @@ +#include <iostream> +#include <string> + +using std::cout; +using std::endl; +using std::string; + +void print (int x) +{ + cout << x << endl; +} + +void print (string x) +{ + cout << x << endl; +} + +int main () +{ + print (42); + print ("Hello, world!"); + return 0; +} diff --git a/20240711/templates-02.cpp b/20240711/templates-02.cpp new file mode 100644 index 0000000000000000000000000000000000000000..191e3000629fc94071fcecc3be94077e9c7db360 --- /dev/null +++ b/20240711/templates-02.cpp @@ -0,0 +1,19 @@ +#include <iostream> +#include <string> + +using std::cout; +using std::endl; +using std::string; + +template <typename t> +void print (t x) +{ + cout << x << endl; +} + +int main () +{ + print (42); + print ("Hello, world!"); + return 0; +} diff --git a/20240711/templates-03.cpp b/20240711/templates-03.cpp new file mode 100644 index 0000000000000000000000000000000000000000..6166f19ccac0771319f0c933de6f4d40b7160bbb --- /dev/null +++ b/20240711/templates-03.cpp @@ -0,0 +1,17 @@ +#include <iostream> + +using std::cout; +using std::endl; + +template <typename t> +void print (t x) +{ + cout << x << endl; +} + +int main () +{ + print (42); + print ("Hello, world!"); + return 0; +} diff --git a/20240711/templates-04.cpp b/20240711/templates-04.cpp new file mode 100644 index 0000000000000000000000000000000000000000..4fca6712426ce894917c93bacfaadccd1fdab88f --- /dev/null +++ b/20240711/templates-04.cpp @@ -0,0 +1,24 @@ +#include <iostream> + +using std::cout; +using std::endl; + +class bla +{ + int content; +}; + +template <typename t> +void print (t x) +{ + cout << x << endl; +} + +int main () +{ + print (42); + print ("Hello, world!"); + bla blubb; + print (blubb); + return 0; +} diff --git a/20240711/templates-07.cpp b/20240711/templates-07.cpp new file mode 100644 index 0000000000000000000000000000000000000000..4279b334c08faebaacd2759bbb48e89389525d1a --- /dev/null +++ b/20240711/templates-07.cpp @@ -0,0 +1,35 @@ +#include <iostream> + +using std::cout; +using std::endl; + +class bla +{ +public: + int content; + bla (int x): + content (x) + { + } +}; + +template <typename t> +void print (t x) +{ + cout << x << endl; +} + +template <> +void print (bla x) +{ + cout << x.content << endl; +} + +int main () +{ + print (42); + print ("Hello, world!"); + bla blubb (137); + print (blubb); + return 0; +} diff --git a/20240711/templates-08.cpp b/20240711/templates-08.cpp new file mode 100644 index 0000000000000000000000000000000000000000..a0c3a29492a197cad3a8b9df9d25f6dea735fa41 --- /dev/null +++ b/20240711/templates-08.cpp @@ -0,0 +1,41 @@ +#include <iostream> + +using std::cout; +using std::endl; + +class bla +{ +public: + int content; + bla (int x): + content (x) + { + } +}; + +template <typename t> +void print (t x) +{ + cout << x << endl; +} + +template <> +void print (int x) +{ + cout << "Die Antwort lautet: " << x << endl; +} + +template <> +void print (bla x) +{ + cout << x.content << endl; +} + +int main () +{ + print (42); + print ("Hello, world!"); + bla blubb (137); + print (blubb); + return 0; +} diff --git a/20240711/templates-09.cpp b/20240711/templates-09.cpp new file mode 100644 index 0000000000000000000000000000000000000000..2606961e8a1c3a85645a333e4867cc82edd50608 --- /dev/null +++ b/20240711/templates-09.cpp @@ -0,0 +1,41 @@ +#include <iostream> + +using std::cout; +using std::endl; + +class bla +{ +public: + int content; + bla (int x): + content (x) + { + } +}; + +template <typename t> +void print (t x) +{ + cout << x << endl; +} + +template <> +void print (int x) +{ + cout << "Die Antwort lautet: " << x << endl; +} + +template <> +void print (bla x) +{ + cout << x << endl; // Fehler wird erkannt, obwohl nicht instantiiert. +} // Dies ist nicht immer der Fall. + +int main () +{ + print (42); + print ("Hello, world!"); + bla blubb (137); +// print (blubb); + return 0; +} diff --git a/20240711/templates-10.cpp b/20240711/templates-10.cpp new file mode 100644 index 0000000000000000000000000000000000000000..88fc5a10f5491f7a033885d619d904c93cb98cea --- /dev/null +++ b/20240711/templates-10.cpp @@ -0,0 +1,54 @@ +#include <iostream> +#include <string> + +using namespace std; + +template <typename argtype> +class list +{ + struct node + { + argtype content; + node *next; + }; + node *root; +public: + list (); + void insert (argtype x); + void output (); +}; + +template <typename argtype> +list <argtype>::list () +{ + root = NULL; +} + +template <typename argtype> +void list <argtype>::insert (argtype x) +{ + node *n = new node; + n->content = x; + n->next = root; + root = n; +} + +template <typename argtype> +void list <argtype>::output () +{ + for (list *p = root; p; p = p->next) + cout << p->content << endl; +} + +int main () +{ + list <string> s; + s.insert ("one"); + s.insert ("two"); + s.insert ("three"); + list <int> i; + i.insert (1); + i.insert (2); + i.insert (3); + return 0; +} diff --git a/20240711/templates-11.cpp b/20240711/templates-11.cpp new file mode 100644 index 0000000000000000000000000000000000000000..57aec679a2d56a4f3c9e8e84e3460309ec7d2720 --- /dev/null +++ b/20240711/templates-11.cpp @@ -0,0 +1,56 @@ +#include <iostream> +#include <string> + +using namespace std; + +template <typename argtype> +class list +{ + struct node + { + argtype content; + node *next; + }; + node *root; +public: + list (); + void insert (argtype x); + void output (); +}; + +template <typename argtype> +list <argtype>::list () +{ + root = NULL; +} + +template <typename argtype> +void list <argtype>::insert (argtype x) +{ + node *n = new node; + n->content = x; + n->next = root; + root = n; +} + +template <typename argtype> +void list <argtype>::output () +{ + for (list *p = root; p; p = p->next) + cout << p->content << endl; +} + +int main () +{ + list <string> s; + s.insert ("one"); + s.insert ("two"); + s.insert ("three"); + s.output (); + list <int> i; + i.insert (1); + i.insert (2); + i.insert (3); + i.output (); + return 0; +} diff --git a/20240711/templates-12.cpp b/20240711/templates-12.cpp new file mode 100644 index 0000000000000000000000000000000000000000..e5d654753a19253f9d2d478071d3ba985d8ef1ea --- /dev/null +++ b/20240711/templates-12.cpp @@ -0,0 +1,56 @@ +#include <iostream> +#include <string> + +using namespace std; + +template <typename argtype> +class list +{ + struct node + { + argtype content; + node *next; + }; + node *root; +public: + list (); + void insert (argtype x); + void output (); +}; + +template <typename argtype> +list <argtype>::list () +{ + root = NULL; +} + +template <typename argtype> +void list <argtype>::insert (argtype x) +{ + node *n = new node; + n->content = x; + n->next = root; + root = n; +} + +template <typename argtype> +void list <argtype>::output () +{ + for (node *p = root; p; p = p->next) + cout << p->content << endl; +} + +int main () +{ + list <string> s; + s.insert ("one"); + s.insert ("two"); + s.insert ("three"); + s.output (); + list <int> i; + i.insert (1); + i.insert (2); + i.insert (3); + i.output (); + return 0; +} diff --git a/20240711/templates-13.cpp b/20240711/templates-13.cpp new file mode 100644 index 0000000000000000000000000000000000000000..3577e9f853a2d50197193b20ad40d734f314f283 --- /dev/null +++ b/20240711/templates-13.cpp @@ -0,0 +1,61 @@ +#include <iostream> +#include <string> + +using namespace std; + +template <typename argtype> +class list +{ + struct node + { + argtype content; + node *next; + }; + node *root; +public: + list (); + void insert (argtype x); + void output (); +}; + +template <typename argtype> +list <argtype>::list () +{ + root = NULL; +} + +template <typename argtype> +void list <argtype>::insert (argtype x) +{ + node *n = new node; + n->content = x; + n->next = root; + root = n; +} + +template <typename argtype> +void list <argtype>::output () +{ + for (node *p = root; p; p = p->next) + cout << p->content << endl; +} + +void blabla (int x) +{ + huddeldibuddel (wuppdich); +} + +int main () +{ + list <string> s; + s.insert ("one"); + s.insert ("two"); + s.insert ("three"); + s.output (); + list <int> i; + i.insert (1); + i.insert (2); + i.insert (3); + i.output (); + return 0; +} diff --git a/20240711/templates-14.cpp b/20240711/templates-14.cpp new file mode 100644 index 0000000000000000000000000000000000000000..b42e532f0a066ae1cd9991686967670c08c7e796 --- /dev/null +++ b/20240711/templates-14.cpp @@ -0,0 +1,54 @@ +#include <iostream> +#include <string> + +using namespace std; + +template <typename argtype> +class list +{ + struct node + { + argtype content; + node *next; + }; + node *root; +public: + list (); + void insert (argtype x); + void output (); +}; + +template <typename argtype> +list <argtype>::list () +{ + root = NULL; +} + +template <typename argtype> +void list <argtype>::insert (argtype x) +{ + node *n = new node; + n->content = x; + n->next = root; + root = n; +} + +template <typename argtype> +void list <argtype>::output () +{ + for (huddeldibuddel *p = root; p; p = p->next) + cout << p->content << endl; +} + +int main () +{ + list <string> s; + s.insert ("one"); + s.insert ("two"); + s.insert ("three"); + list <int> i; + i.insert (1); + i.insert (2); + i.insert (3); + return 0; +} diff --git a/20240711/templates-15.cpp b/20240711/templates-15.cpp new file mode 100644 index 0000000000000000000000000000000000000000..e8296e01cb5a66017ad9c4382e575bdd84989b43 --- /dev/null +++ b/20240711/templates-15.cpp @@ -0,0 +1,54 @@ +#include <iostream> +#include <string> + +using namespace std; + +template <typename argtype> +class list +{ + struct node + { + argtype content; + node *next; + }; + node *root; +public: + list (); + void insert (argtype x); + void output (); +}; + +template <typename argtype> +list <argtype>::list () +{ + root = NULL; +} + +template <typename argtype> +void list <argtype>::insert (argtype x) +{ + node *n = new node; + n->content = x; + n->next = root; + root = n; +} + +template <typename argtype> +void list <argtype>::output () +{ + for (char *p = root; p; p = p->next) + cout << p->content << endl; +} + +int main () +{ + list <string> s; + s.insert ("one"); + s.insert ("two"); + s.insert ("three"); + list <int> i; + i.insert (1); + i.insert (2); + i.insert (3); + return 0; +} diff --git a/20240711/templates-16.cpp b/20240711/templates-16.cpp new file mode 100644 index 0000000000000000000000000000000000000000..d023eeeb690ef2ba6032903f8564422a4a20a469 --- /dev/null +++ b/20240711/templates-16.cpp @@ -0,0 +1,64 @@ +#include <iostream> +#include <string> + +using namespace std; + +template <typename argtype> +class list +{ + struct node + { + argtype content; + node *next; + }; + node *root; +public: + list (); + void insert (argtype x); + void output (); +}; + +template <typename argtype> +list <argtype>::list () +{ + root = NULL; +} + +template <typename argtype> +void list <argtype>::insert (argtype x) +{ + node *n = new node; + n->content = x; + n->next = root; + root = n; +} + +template <typename argtype> +void list <argtype>::output () +{ + for (node *p = root; p; p = p->next) + cout << p->content << endl; +} + +template <> +void list <int>::output () +{ + for (node *p = root; p; p = p->next) + cout << p->content << " "; + cout << endl; +} + +int main () +{ + list <string> s; + s.insert ("one"); + s.insert ("two"); + s.insert ("three"); + s.output (); + list <int> i; + i.insert (1); + i.insert (2); + i.insert (3); + i.output (); + return 0; +} diff --git a/20240711/type-conversions-01.cpp b/20240711/type-conversions-01.cpp new file mode 100644 index 0000000000000000000000000000000000000000..f861606d9525fe4e28bad6edbec88c29a1da68f0 --- /dev/null +++ b/20240711/type-conversions-01.cpp @@ -0,0 +1,11 @@ +#include <stdio.h> +#include <stdint.h> +#include <inttypes.h> + +int main () +{ + const char *hello = "Hello, world!"; + uint64_t address = (uint64_t) hello; + printf ("%" PRIx64 "\n", address); + return 0; +} diff --git a/20240711/type-conversions-02.cpp b/20240711/type-conversions-02.cpp new file mode 100644 index 0000000000000000000000000000000000000000..be702cc1f9de8a45d7d030e28172636d4cd663d5 --- /dev/null +++ b/20240711/type-conversions-02.cpp @@ -0,0 +1,11 @@ +#include <stdio.h> +#include <stdint.h> +#include <inttypes.h> + +int main () +{ + const char *hello = "Hello, world!"; + uint64_t address = uint64_t (hello); + printf ("%" PRIx64 "\n", address); + return 0; +} diff --git a/20240711/type-conversions-03.cpp b/20240711/type-conversions-03.cpp new file mode 100644 index 0000000000000000000000000000000000000000..34572937e5194527db8c81746205cec0469ae129 --- /dev/null +++ b/20240711/type-conversions-03.cpp @@ -0,0 +1,24 @@ +#include <stdio.h> + +struct TBase +{ + virtual void print () + { + printf ("(Base)\n"); + } +}; + +struct THello: public TBase +{ + virtual void print () + { + printf ("Hello, world!\n"); + } +}; + +int main () +{ + TBase *p = new THello; + p->print (); + return 0; +} diff --git a/20240711/type-conversions-04.cpp b/20240711/type-conversions-04.cpp new file mode 100644 index 0000000000000000000000000000000000000000..e21b9c100d88449a17b5883a5d52da04c71a81bc --- /dev/null +++ b/20240711/type-conversions-04.cpp @@ -0,0 +1,24 @@ +#include <stdio.h> + +struct TBase +{ + void print () + { + printf ("(Base)\n"); + } +}; + +struct THello: public TBase +{ + void print () + { + printf ("Hello, world!\n"); + } +}; + +int main () +{ + TBase *p = new THello; + p->print (); + return 0; +} diff --git a/20240711/type-conversions-05.cpp b/20240711/type-conversions-05.cpp new file mode 100644 index 0000000000000000000000000000000000000000..ea77aecb3c95a10ae6adf322cda448fa0f5e89c4 --- /dev/null +++ b/20240711/type-conversions-05.cpp @@ -0,0 +1,25 @@ +#include <stdio.h> + +struct TBase +{ + virtual void print () + { + printf ("(Base)\n"); + } +}; + +struct THello: public TBase +{ + virtual void print () + { + printf ("Hello, world!\n"); + } +}; + +int main () +{ + TBase *p = new THello; + THello *q = p; + q->print (); + return 0; +} diff --git a/20240711/type-conversions-06.cpp b/20240711/type-conversions-06.cpp new file mode 100644 index 0000000000000000000000000000000000000000..5e969135dc33cb4f380d68867dec60b05d65bb4d --- /dev/null +++ b/20240711/type-conversions-06.cpp @@ -0,0 +1,25 @@ +#include <stdio.h> + +struct TBase +{ + virtual void print () + { + printf ("(Base)\n"); + } +}; + +struct THello: public TBase +{ + virtual void print () + { + printf ("Hello, world!\n"); + } +}; + +int main () +{ + TBase *p = new THello; + THello *q = dynamic_cast <THello *> (p); + q->print (); + return 0; +} diff --git a/20240711/type-conversions-07.cpp b/20240711/type-conversions-07.cpp new file mode 100644 index 0000000000000000000000000000000000000000..7454be7612da80fb38d8e96a6a871e8cb5ce72c6 --- /dev/null +++ b/20240711/type-conversions-07.cpp @@ -0,0 +1,25 @@ +#include <stdio.h> + +struct TBase +{ + virtual void print () + { + printf ("(Base)\n"); + } +}; + +struct THello: public TBase +{ + virtual void print () + { + printf ("Hello, world!\n"); + } +}; + +int main () +{ + TBase *p = new TBase; + THello *q = dynamic_cast <THello *> (p); + q->print (); + return 0; +} diff --git a/20240711/type-conversions-08.cpp b/20240711/type-conversions-08.cpp new file mode 100644 index 0000000000000000000000000000000000000000..c01ddccee9360bd0b7d24bd9b1c6a57828bf3934 --- /dev/null +++ b/20240711/type-conversions-08.cpp @@ -0,0 +1,28 @@ +#include <stdio.h> + +struct TBase +{ + virtual void print () + { + printf ("(Base)\n"); + } +}; + +struct THello: public TBase +{ + virtual void print () + { + printf ("Hello, world!\n"); + } +}; + +int main () +{ + TBase *p = new TBase; + THello *q = dynamic_cast <THello *> (p); + if (q) + q->print (); + else + printf ("Typumwandlung fehlgeschlagen.\n"); + return 0; +} diff --git a/20240711/type-conversions-09.cpp b/20240711/type-conversions-09.cpp new file mode 100644 index 0000000000000000000000000000000000000000..4aebad72e42a76c6ee2b41ac1924016890d186e7 --- /dev/null +++ b/20240711/type-conversions-09.cpp @@ -0,0 +1,28 @@ +#include <stdio.h> + +struct TBase +{ + virtual void print () + { + printf ("(Base)\n"); + } +}; + +struct THello: public TBase +{ + virtual void print () + { + printf ("Hello, world!\n"); + } +}; + +int main () +{ + TBase *p = new THello; + THello *q = dynamic_cast <THello *> (p); + if (q) + q->print (); + else + printf ("Typumwandlung fehlgeschlagen.\n"); + return 0; +} diff --git a/20240711/type-conversions-10.cpp b/20240711/type-conversions-10.cpp new file mode 100644 index 0000000000000000000000000000000000000000..416aa6e554911a1cb6862e53cbdf94df4cc7e8b6 --- /dev/null +++ b/20240711/type-conversions-10.cpp @@ -0,0 +1,28 @@ +#include <stdio.h> + +struct TBase +{ + virtual void print () + { + printf ("(Base)\n"); + } +}; + +struct THello: public TBase +{ + virtual void print () + { + printf ("Hello, world!\n"); + } +}; + +int main () +{ + TBase *p = new THello; + THello *q = static_cast <THello *> (p); + if (q) + q->print (); + else + printf ("Typumwandlung fehlgeschlagen.\n"); + return 0; +} diff --git a/20240711/type-conversions-11.cpp b/20240711/type-conversions-11.cpp new file mode 100644 index 0000000000000000000000000000000000000000..a92b6bb182e14c3968ff32c1e771b8ae084d60f0 --- /dev/null +++ b/20240711/type-conversions-11.cpp @@ -0,0 +1,28 @@ +#include <stdio.h> + +struct TBase +{ + virtual void print () + { + printf ("(Base)\n"); + } +}; + +struct THello: public TBase +{ + virtual void print () + { + printf ("Hello, world!\n"); + } +}; + +int main () +{ + TBase *p = new TBase; + THello *q = static_cast <THello *> (p); + if (q) + q->print (); + else + printf ("Typumwandlung fehlgeschlagen.\n"); + return 0; +} diff --git a/20240711/type-conversions-11a.cpp b/20240711/type-conversions-11a.cpp new file mode 100644 index 0000000000000000000000000000000000000000..2099f0690a5c4c9b7d4e16b6072e59a181f296a5 --- /dev/null +++ b/20240711/type-conversions-11a.cpp @@ -0,0 +1,28 @@ +#include <stdio.h> + +struct TBase +{ + void print () + { + printf ("(Base)\n"); + } +}; + +struct THello: public TBase +{ + void print () + { + printf ("Hello, world!\n"); + } +}; + +int main () +{ + TBase *p = new TBase; + THello *q = static_cast <THello *> (p); + if (q) + q->print (); + else + printf ("Typumwandlung fehlgeschlagen.\n"); + return 0; +} diff --git a/20240711/type-conversions-11b.cpp b/20240711/type-conversions-11b.cpp new file mode 100644 index 0000000000000000000000000000000000000000..04c2ed4f738e15ce9837317ca4bd573d08d68a83 --- /dev/null +++ b/20240711/type-conversions-11b.cpp @@ -0,0 +1,33 @@ +#include <stdio.h> + +struct TBase +{ + void print () + { + printf ("(Base)\n"); + } +}; + +struct THello: public TBase +{ + const char *content; + THello () + { + content = "Hello, world!\n"; + } + void print () + { + printf (content); + } +}; + +int main () +{ + TBase *p = new TBase; + THello *q = static_cast <THello *> (p); + if (q) + q->print (); + else + printf ("Typumwandlung fehlgeschlagen.\n"); + return 0; +} diff --git a/20240711/type-conversions-12.cpp b/20240711/type-conversions-12.cpp new file mode 100644 index 0000000000000000000000000000000000000000..bce43d1d16cd7c1026c415f2c05d04e8f918e426 --- /dev/null +++ b/20240711/type-conversions-12.cpp @@ -0,0 +1,29 @@ +#include <stdio.h> + +struct TBase +{ + virtual void print () + { + printf ("(Base)\n"); + } +}; + +struct THello: public TBase +{ + virtual void print () + { + printf ("Hello, world!\n"); + } +}; + +int main () +{ + int answer = 13; + int *p = &answer; + THello *q = static_cast <THello *> (p); + if (q) + q->print (); + else + printf ("Typumwandlung fehlgeschlagen.\n"); + return 0; +} diff --git a/20240711/type-conversions-12a.cpp b/20240711/type-conversions-12a.cpp new file mode 100644 index 0000000000000000000000000000000000000000..8c76b2b148142cae60976aaafe8fbd592543785a --- /dev/null +++ b/20240711/type-conversions-12a.cpp @@ -0,0 +1,29 @@ +#include <stdio.h> + +struct TBase +{ + virtual void print () + { + printf ("(Base)\n"); + } +}; + +struct THello: public TBase +{ + virtual void print () + { + printf ("Hello, world!\n"); + } +}; + +int main () +{ + int answer = 13; + int *p = &answer; + THello *q = dynamic_cast <THello *> (p); + if (q) + q->print (); + else + printf ("Typumwandlung fehlgeschlagen.\n"); + return 0; +} diff --git a/20240711/type-conversions-13.cpp b/20240711/type-conversions-13.cpp new file mode 100644 index 0000000000000000000000000000000000000000..50a04d324603d78b5c0b99dad7bb4b2be522398f --- /dev/null +++ b/20240711/type-conversions-13.cpp @@ -0,0 +1,29 @@ +#include <stdio.h> + +struct TBase +{ + virtual void print () + { + printf ("(Base)\n"); + } +}; + +struct THello: public TBase +{ + virtual void print () + { + printf ("Hello, world!\n"); + } +}; + +int main () +{ + int answer = 13; + int *p = &answer; + THello *q = (THello *) p; + if (q) + q->print (); + else + printf ("Typumwandlung fehlgeschlagen.\n"); + return 0; +} diff --git a/20240711/type-conversions-14.cpp b/20240711/type-conversions-14.cpp new file mode 100644 index 0000000000000000000000000000000000000000..a9ed970c9ef1cf65a7d89c12b4216929ac187bc5 --- /dev/null +++ b/20240711/type-conversions-14.cpp @@ -0,0 +1,31 @@ +#include <stdio.h> + +struct TBase +{ + virtual void print () + { + printf ("(Base)\n"); + } +}; + +struct THello: public TBase +{ + const char *content; + + THello (const char *s) + { + content = s; + } + + virtual void print () + { + printf ("%s\n", content); + } +}; + +int main () +{ + THello h = "Hello, world!"; + h.print (); + return 0; +} diff --git a/20240711/type-conversions-15.cpp b/20240711/type-conversions-15.cpp new file mode 100644 index 0000000000000000000000000000000000000000..17fa8b5119724a0c9da63eb0c6829064801ba106 --- /dev/null +++ b/20240711/type-conversions-15.cpp @@ -0,0 +1,37 @@ +#include <stdio.h> + +struct TBase +{ + virtual void print () + { + printf ("(Base)\n"); + } +}; + +struct THello: public TBase +{ + const char *content; + + THello (const char *s) + { + content = s; + } + + THello (const TBase &b) + { + content = "(Base)"; + } + + virtual void print () + { + printf ("%s\n", content); + } +}; + +int main () +{ + TBase b; + THello h = b; + h.print (); + return 0; +} diff --git a/20240711/type-conversions-16.cpp b/20240711/type-conversions-16.cpp new file mode 100644 index 0000000000000000000000000000000000000000..761cda5c89f4fd73fd14b6555b8aa9ebce7e2a3e --- /dev/null +++ b/20240711/type-conversions-16.cpp @@ -0,0 +1,37 @@ +#include <stdio.h> + +struct TBase +{ + virtual void print () + { + printf ("(Base)\n"); + } +}; + +struct THello: public TBase +{ + const char *content; + + THello (const char *s) + { + content = s; + } + + explicit THello (const TBase &b) + { + content = "(Base)"; + } + + virtual void print () + { + printf ("%s\n", content); + } +}; + +int main () +{ + TBase b; + THello h = b; + h.print (); + return 0; +} diff --git a/20240711/type-conversions-17.cpp b/20240711/type-conversions-17.cpp new file mode 100644 index 0000000000000000000000000000000000000000..7f51b7f6d45792449bceee57905b07ae1756aca1 --- /dev/null +++ b/20240711/type-conversions-17.cpp @@ -0,0 +1,37 @@ +#include <stdio.h> + +struct TBase +{ + virtual void print () + { + printf ("(Base)\n"); + } +}; + +struct THello: public TBase +{ + const char *content; + + THello (const char *s) + { + content = s; + } + + explicit THello (const TBase &b) + { + content = "(Base)"; + } + + virtual void print () + { + printf ("%s\n", content); + } +}; + +int main () +{ + TBase b; + THello h (b); + h.print (); + return 0; +} diff --git a/20240711/type-conversions-18.cpp b/20240711/type-conversions-18.cpp new file mode 100644 index 0000000000000000000000000000000000000000..bfab7be4086f699d4bbba83afbb72ddae0207d9e --- /dev/null +++ b/20240711/type-conversions-18.cpp @@ -0,0 +1,41 @@ +#include <stdio.h> + +struct TBase +{ + virtual void print () + { + printf ("(Base)\n"); + } +}; + +struct THello: public TBase +{ + const char *content; + + THello (const char *s) + { + content = s; + } + + THello (const TBase &b) + { + content = "(Base)"; + } + + virtual void print () + { + printf ("%s\n", content); + } +}; + +void print_it (const THello &h) +{ + printf ("%s\n", h.content); +} + +int main () +{ + TBase b; + print_it (b); + return 0; +} diff --git a/20240711/type-conversions-19.cpp b/20240711/type-conversions-19.cpp new file mode 100644 index 0000000000000000000000000000000000000000..32fe3fbff2115f30691f899950146559811092cb --- /dev/null +++ b/20240711/type-conversions-19.cpp @@ -0,0 +1,41 @@ +#include <stdio.h> + +struct TBase +{ + virtual void print () + { + printf ("(Base)\n"); + } +}; + +struct THello: public TBase +{ + const char *content; + + THello (const char *s) + { + content = s; + } + + explicit THello (const TBase &b) + { + content = "(Base)"; + } + + virtual void print () + { + printf ("%s\n", content); + } +}; + +void print_it (const THello &h) +{ + printf ("%s\n", h.content); +} + +int main () +{ + TBase b; + print_it (b); + return 0; +} diff --git a/20240711/type-conversions-20.cpp b/20240711/type-conversions-20.cpp new file mode 100644 index 0000000000000000000000000000000000000000..cc5055de92ed51dd7fdb7372ef80e1a0e02a5fab --- /dev/null +++ b/20240711/type-conversions-20.cpp @@ -0,0 +1,9 @@ +#include <stdio.h> + +int main () +{ + int answer = 1117; + int *p = &answer; + printf ("The address of the answer is: %zx\n", p); + return 0; +} diff --git a/20240711/type-conversions-21.cpp b/20240711/type-conversions-21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..48cbdb94ecea46c10ddf74d1f1fd93f69ffe3b9b --- /dev/null +++ b/20240711/type-conversions-21.cpp @@ -0,0 +1,9 @@ +#include <stdio.h> + +int main () +{ + int answer = 1117; + int *p = &answer; + printf ("The address of the answer is: %zx\n", (size_t) p); + return 0; +} diff --git a/20240711/type-conversions-22.cpp b/20240711/type-conversions-22.cpp new file mode 100644 index 0000000000000000000000000000000000000000..592c47f9135e4c8df84ecf4a8a0e8f05b38beccb --- /dev/null +++ b/20240711/type-conversions-22.cpp @@ -0,0 +1,9 @@ +#include <stdio.h> + +int main () +{ + int answer = 1117; + int *p = &answer; + printf ("The address of the answer is: %zx\n", reinterpret_cast <size_t> (p)); + return 0; +} diff --git a/20240711/type-conversions-23.cpp b/20240711/type-conversions-23.cpp new file mode 100644 index 0000000000000000000000000000000000000000..45d2aeb563a5ebdbb151d8a8506c35c67d6a0687 --- /dev/null +++ b/20240711/type-conversions-23.cpp @@ -0,0 +1,9 @@ +#include <stdio.h> + +int main () +{ + const char* hello = "Hello, world!"; + char *h = hello; + printf ("%s\n", h); + return 0; +} diff --git a/20240711/type-conversions-24.cpp b/20240711/type-conversions-24.cpp new file mode 100644 index 0000000000000000000000000000000000000000..889b4d5ab47ac8b867f36f2a45c9646b6c3f678f --- /dev/null +++ b/20240711/type-conversions-24.cpp @@ -0,0 +1,9 @@ +#include <stdio.h> + +int main () +{ + const char* hello = "Hello, world!"; + char *h = (char *) hello; + printf ("%s\n", h); + return 0; +} diff --git a/20240711/type-conversions-25.cpp b/20240711/type-conversions-25.cpp new file mode 100644 index 0000000000000000000000000000000000000000..fdb8227429c2e1a1fef71ad2cc853bd67e8670a7 --- /dev/null +++ b/20240711/type-conversions-25.cpp @@ -0,0 +1,9 @@ +#include <stdio.h> + +int main () +{ + const char* hello = "Hello, world!"; + char *h = const_cast <char *> (hello); + printf ("%s\n", h); + return 0; +}