diff --git a/20160623/es-20160623.txt b/20160623/es-20160623.txt new file mode 100644 index 0000000000000000000000000000000000000000..c8baae09985702169abf5730f0187b854e2bfd28 --- /dev/null +++ b/20160623/es-20160623.txt @@ -0,0 +1,22 @@ +Software-Entwicklung für eingebettete Systeme +============================================= + +Einführung in C++ +~~~~~~~~~~~~~~~~~ +:) Streams: überladener Operator "<<" +:) Objektorientierte Programmierung: + :) virtuelle Methoden + :) Konstruktoren + :) Vererbung und Polymorphie + :) Zugriffsrechte + :) Destruktoren +:) Referenzen +:) Templates +:) STL +:) auto-Typen +:) spezielle Zeiger +:) Exceptions + +Spezielle Zeiger +~~~~~~~~~~~~~~~~ +Problem: Zeiger auf Objektinstanz; der Zeiger hat begrenzte Gültigkeit diff --git a/20160623/exceptions-0.cpp b/20160623/exceptions-0.cpp new file mode 100644 index 0000000000000000000000000000000000000000..6319e0e69bc10a3cd80e72d1663357fcdadde071 --- /dev/null +++ b/20160623/exceptions-0.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/20160623/exceptions-1.cpp b/20160623/exceptions-1.cpp new file mode 100644 index 0000000000000000000000000000000000000000..72db25aeeb34cf37d07a6f78eea2d55bf0db2851 --- /dev/null +++ b/20160623/exceptions-1.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/20160623/lists-1.cpp b/20160623/lists-1.cpp new file mode 100644 index 0000000000000000000000000000000000000000..55a8f0557bbb9f905827e406feb92616ec8ec4b9 --- /dev/null +++ b/20160623/lists-1.cpp @@ -0,0 +1,25 @@ +#include <iostream> + +using namespace std; + +struct node +{ + int content; + node *next; +}; + +int main () +{ + node A, B, C, D; + A.content = 2; + A.next = &B; + B.content = 3; + B.next = &C; + C.content = 5; + C.next = &D; + D.content = 7; + D.next = NULL; + for (node *p = &A; p; p = p->next) + cout << p->content << endl; + return 0; +} diff --git a/20160623/lists-2.cpp b/20160623/lists-2.cpp new file mode 100644 index 0000000000000000000000000000000000000000..a994f5df09f887c0cb9cd90491a6d0e4c506c93b --- /dev/null +++ b/20160623/lists-2.cpp @@ -0,0 +1,31 @@ +#include <iostream> + +using namespace std; + +struct node +{ + int content; + node *next; +}; + +int main () +{ + node A, B, C, D; + A.content = 2; + A.next = &B; + B.content = 3; + B.next = &C; + C.content = 5; + C.next = &D; + D.content = 7; + D.next = NULL; + for (node *p = &A; p; p = p->next) + cout << p->content << endl; + node BO; + BO.content = 4; + BO.next = &C; + B.next = &BO; + for (node *p = &A; p; p = p->next) + cout << p->content << endl; + return 0; +} diff --git a/20160623/lists-3.cpp b/20160623/lists-3.cpp new file mode 100644 index 0000000000000000000000000000000000000000..b8a764d7775140f4698d21e8352f8ccdea0c1f8c --- /dev/null +++ b/20160623/lists-3.cpp @@ -0,0 +1,39 @@ +#include <iostream> + +using namespace std; + +class node +{ + public: + int content; + node *next; + void insertAfter (node *newNode); +}; + +void node::insertAfter (node *newNode) +{ + if (newNode) + newNode->next = next; + next = newNode; +} + +int main () +{ + node A, B, C, D; + A.content = 2; + A.next = &B; + B.content = 3; + B.next = &C; + C.content = 5; + C.next = &D; + D.content = 7; + D.next = NULL; + for (node *p = &A; p; p = p->next) + cout << p->content << endl; + node BO; + BO.content = 4; + B.insertAfter (&BO); + for (node *p = &A; p; p = p->next) + cout << p->content << endl; + return 0; +} diff --git a/20160623/lists-4.cpp b/20160623/lists-4.cpp new file mode 100644 index 0000000000000000000000000000000000000000..40081ddbbf84415bfaa37b112d513f8f5d4b8a9c --- /dev/null +++ b/20160623/lists-4.cpp @@ -0,0 +1,45 @@ +#include <iostream> + +using namespace std; + +template <typename contentType> class node +{ + public: + contentType content; + node *next; + void insertAfter (node *newNode) + { + if (newNode) + newNode->next = next; + next = newNode; + } +}; + +int main () +{ + node <int> A, B, C, D; + A.content = 2; + A.next = &B; + B.content = 3; + B.next = &C; + C.content = 5; + C.next = &D; + D.content = 7; + D.next = NULL; + for (node <int> *p = &A; p; p = p->next) + cout << p->content << endl; + node <int> BO; + BO.content = 4; + B.insertAfter (&BO); + for (node <int> *p = &A; p; p = p->next) + cout << p->content << endl; + node <string> E, F; + E.content = "Hello, "; + E.next = NULL; + F.content = "world!"; + F.next = NULL; + E.insertAfter (&F); + for (node <string> *p = &E; p; p = p->next) + cout << p->content << endl; + return 0; +} diff --git a/20160623/objects-25.cpp b/20160623/objects-25.cpp new file mode 100644 index 0000000000000000000000000000000000000000..a6b15cd7fa0113ef01b2b3f28640ca3a40e8b4e0 --- /dev/null +++ b/20160623/objects-25.cpp @@ -0,0 +1,26 @@ +#include <iostream> + +using namespace std; + +class Bewirker +{ + public: + Bewirker (); + ~Bewirker (); +}; + +Bewirker::Bewirker () +{ + cout << "Raumfahrzeug wird startbereit gemacht ... Moment ..." << endl; +} + +Bewirker::~Bewirker () +{ + cout << "Raumfahrzeug wird wieder transportfähig gemacht ... Moment ..." << endl; +} + +int main () +{ + Bewirker SaturnV; + return 0; +} diff --git a/20160623/pointers-1.cpp b/20160623/pointers-1.cpp new file mode 100644 index 0000000000000000000000000000000000000000..518b0f6585c4c4b9a6c33f4f238618b2c34537d0 --- /dev/null +++ b/20160623/pointers-1.cpp @@ -0,0 +1,18 @@ +#include <iostream> + +using namespace std; + +void some_function () +{ + int *p = new int[1]; + *p = 42; + /* ... */ + cout << *p << endl; + delete p; +} + +int main () +{ + some_function (); + return 0; +} diff --git a/20160623/pointers-1a.c b/20160623/pointers-1a.c new file mode 100644 index 0000000000000000000000000000000000000000..85deeb30e8bd2e09bac1433d0e16cc657bf6e33b --- /dev/null +++ b/20160623/pointers-1a.c @@ -0,0 +1,17 @@ +#include <stdio.h> +#include <stdlib.h> + +void some_function (void) +{ + int *p = malloc (sizeof (int)); + *p = 42; + /* ... */ + printf ("%d\n", *p); + free (p); +} + +int main (void) +{ + some_function (); + return 0; +} diff --git a/20160623/pointers-2.cpp b/20160623/pointers-2.cpp new file mode 100644 index 0000000000000000000000000000000000000000..453794af64293ae493d2a5b58075a15d758be5b8 --- /dev/null +++ b/20160623/pointers-2.cpp @@ -0,0 +1,18 @@ +#include <iostream> + +using namespace std; + +int *some_function () +{ + int *p = new int[1]; + *p = 42; + return p; +} + +int main () +{ + int *p = some_function (); + cout << *p << endl; + delete p; + return 0; +} diff --git a/20160623/pointers-3.cpp b/20160623/pointers-3.cpp new file mode 100644 index 0000000000000000000000000000000000000000..be594cfccf8969bd0ab5563c0f558639f561394f --- /dev/null +++ b/20160623/pointers-3.cpp @@ -0,0 +1,18 @@ +#include <iostream> +#include <memory> + +using namespace std; + +shared_ptr <int> some_function () +{ + shared_ptr <int> p (new int[1]); + *p = 42; + return p; +} + +int main () +{ + auto p = some_function (); + cout << *p << endl; + return 0; +} diff --git a/20160623/pointers-4.cpp b/20160623/pointers-4.cpp new file mode 100644 index 0000000000000000000000000000000000000000..6911c59dffa5ac5efb66b4959317c59f020cc665 --- /dev/null +++ b/20160623/pointers-4.cpp @@ -0,0 +1,20 @@ +#include <iostream> +#include <memory> + +using namespace std; + +shared_ptr <int> some_function () +{ + shared_ptr <int> p (new int[1]); + *p = 42; + return p; +} + +int main () +{ + auto p = some_function (); + auto q = p; + cout << *p << endl; + cout << *q / 2 + 2 << endl; + return 0; +} diff --git a/20160623/pointers-5.cpp b/20160623/pointers-5.cpp new file mode 100644 index 0000000000000000000000000000000000000000..ad18750ff23b236bc20015979b2380acaf02ff0d --- /dev/null +++ b/20160623/pointers-5.cpp @@ -0,0 +1,20 @@ +#include <iostream> +#include <memory> + +using namespace std; + +unique_ptr <int> some_function () +{ + unique_ptr <int> p (new int[1]); + *p = 42; + return p; +} + +int main () +{ + auto p = some_function (); + auto q = p; + cout << *p << endl; + cout << *q / 2 + 2 << endl; + return 0; +} diff --git a/20160623/pointers-6.cpp b/20160623/pointers-6.cpp new file mode 100644 index 0000000000000000000000000000000000000000..5c974409783daa138d87cf3f3f38bb1bf3cb9aed --- /dev/null +++ b/20160623/pointers-6.cpp @@ -0,0 +1,20 @@ +#include <iostream> +#include <memory> + +using namespace std; + +unique_ptr <int> some_function () +{ + unique_ptr <int> p (new int[1]); + *p = 42; + return p; +} + +int main () +{ + auto p = some_function (); + auto q = move (p); + cout << *p << endl; + cout << *q / 2 + 2 << endl; + return 0; +} diff --git a/20160623/pointers-7.cpp b/20160623/pointers-7.cpp new file mode 100644 index 0000000000000000000000000000000000000000..d5ccb0adad68ed168e037964328bc105b0aaa4c6 --- /dev/null +++ b/20160623/pointers-7.cpp @@ -0,0 +1,20 @@ +#include <iostream> +#include <memory> + +using namespace std; + +unique_ptr <int> some_function () +{ + unique_ptr <int> p (new int[1]); + *p = 42; + return p; +} + +int main () +{ + auto p = some_function (); + cout << *p << endl; + auto q = move (p); + cout << *q / 2 + 2 << endl; + return 0; +} diff --git a/20160623/stl-1.cpp b/20160623/stl-1.cpp new file mode 100644 index 0000000000000000000000000000000000000000..444e2287e891bf46a9df01fe6820529f567f57d0 --- /dev/null +++ b/20160623/stl-1.cpp @@ -0,0 +1,17 @@ +#include <iostream> +#include <vector> + +using namespace std; + +int main () +{ + vector <int> prime; + prime.push_back (2); + prime.push_back (3); + prime.push_back (5); + prime.push_back (7); + prime.push_back (11); + for (int i = 0; i < 5; i++) + cout << prime[i] << endl; + return 0; +} diff --git a/20160623/stl-2.cpp b/20160623/stl-2.cpp new file mode 100644 index 0000000000000000000000000000000000000000..4a866fe81fb4bf83a5e52d15cdec1ca8ea75ddc4 --- /dev/null +++ b/20160623/stl-2.cpp @@ -0,0 +1,17 @@ +#include <iostream> +#include <list> + +using namespace std; + +int main () +{ + list <int> prime; + prime.push_back (2); + prime.push_back (3); + prime.push_back (5); + prime.push_back (7); + prime.push_back (11); + for (list <int> ::iterator p = prime.begin (); p != prime.end (); p++) + cout << *p << endl; + return 0; +} diff --git a/20160623/stl-2a.cpp b/20160623/stl-2a.cpp new file mode 100644 index 0000000000000000000000000000000000000000..a5520797cb6e732a45a41a6be79731d122491891 --- /dev/null +++ b/20160623/stl-2a.cpp @@ -0,0 +1,11 @@ +#include <iostream> + +using namespace std; + +int main () +{ + int prime[] = { 2, 3, 5, 7, 11 }; + for (int *p = prime; p != prime + 5; p++) + cout << *p << endl; + return 0; +} diff --git a/20160623/stl-3.cpp b/20160623/stl-3.cpp new file mode 100644 index 0000000000000000000000000000000000000000..19ae7a3437562826f0fff816b0a3ebe23d69e974 --- /dev/null +++ b/20160623/stl-3.cpp @@ -0,0 +1,17 @@ +#include <iostream> +#include <vector> + +using namespace std; + +int main () +{ + vector <int> prime; + prime.push_back (2); + prime.push_back (3); + prime.push_back (5); + prime.push_back (7); + prime.push_back (11); + for (vector <int> ::iterator p = prime.begin (); p != prime.end (); p++) + cout << *p << endl; + return 0; +} diff --git a/20160623/stl-4.cpp b/20160623/stl-4.cpp new file mode 100644 index 0000000000000000000000000000000000000000..cdd2a98bce60b9154d222b5962724b7b158fe276 --- /dev/null +++ b/20160623/stl-4.cpp @@ -0,0 +1,17 @@ +#include <iostream> +#include <vector> + +using namespace std; + +int main () +{ + vector <int> prime; + prime.push_back (2); + prime.push_back (3); + prime.push_back (5); + prime.push_back (7); + prime.push_back (11); + for (auto p = prime.begin (); p != prime.end (); p++) + cout << *p << endl; + return 0; +} diff --git a/20160623/stl-5.cpp b/20160623/stl-5.cpp new file mode 100644 index 0000000000000000000000000000000000000000..a8bebd567b8bc80c73079474ed9df93c242854f6 --- /dev/null +++ b/20160623/stl-5.cpp @@ -0,0 +1,17 @@ +#include <iostream> +#include <vector> + +using namespace std; + +int main () +{ + vector <int> prime; + prime.push_back (2); + prime.push_back (3); + prime.push_back (5); + prime.push_back (7); + prime.push_back (11); + for (auto p: prime) + cout << p << endl; + return 0; +}