From d45e6116f5bdff5964256cd0e7c7523a2dcb08c3 Mon Sep 17 00:00:00 2001 From: Peter Gerwinski <peter.gerwinski@hs-bochum.de> Date: Sat, 8 Oct 2016 11:36:31 +0200 Subject: [PATCH] =?UTF-8?q?Einf=C3=BChrung=20in=20C++,=2023.6.2016?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- 20160623/es-20160623.txt | 22 +++++++++++++++++++ 20160623/exceptions-0.cpp | 19 +++++++++++++++++ 20160623/exceptions-1.cpp | 21 ++++++++++++++++++ 20160623/lists-1.cpp | 25 ++++++++++++++++++++++ 20160623/lists-2.cpp | 31 +++++++++++++++++++++++++++ 20160623/lists-3.cpp | 39 +++++++++++++++++++++++++++++++++ 20160623/lists-4.cpp | 45 +++++++++++++++++++++++++++++++++++++++ 20160623/objects-25.cpp | 26 ++++++++++++++++++++++ 20160623/pointers-1.cpp | 18 ++++++++++++++++ 20160623/pointers-1a.c | 17 +++++++++++++++ 20160623/pointers-2.cpp | 18 ++++++++++++++++ 20160623/pointers-3.cpp | 18 ++++++++++++++++ 20160623/pointers-4.cpp | 20 +++++++++++++++++ 20160623/pointers-5.cpp | 20 +++++++++++++++++ 20160623/pointers-6.cpp | 20 +++++++++++++++++ 20160623/pointers-7.cpp | 20 +++++++++++++++++ 20160623/stl-1.cpp | 17 +++++++++++++++ 20160623/stl-2.cpp | 17 +++++++++++++++ 20160623/stl-2a.cpp | 11 ++++++++++ 20160623/stl-3.cpp | 17 +++++++++++++++ 20160623/stl-4.cpp | 17 +++++++++++++++ 20160623/stl-5.cpp | 17 +++++++++++++++ 22 files changed, 475 insertions(+) create mode 100644 20160623/es-20160623.txt create mode 100644 20160623/exceptions-0.cpp create mode 100644 20160623/exceptions-1.cpp create mode 100644 20160623/lists-1.cpp create mode 100644 20160623/lists-2.cpp create mode 100644 20160623/lists-3.cpp create mode 100644 20160623/lists-4.cpp create mode 100644 20160623/objects-25.cpp create mode 100644 20160623/pointers-1.cpp create mode 100644 20160623/pointers-1a.c create mode 100644 20160623/pointers-2.cpp create mode 100644 20160623/pointers-3.cpp create mode 100644 20160623/pointers-4.cpp create mode 100644 20160623/pointers-5.cpp create mode 100644 20160623/pointers-6.cpp create mode 100644 20160623/pointers-7.cpp create mode 100644 20160623/stl-1.cpp create mode 100644 20160623/stl-2.cpp create mode 100644 20160623/stl-2a.cpp create mode 100644 20160623/stl-3.cpp create mode 100644 20160623/stl-4.cpp create mode 100644 20160623/stl-5.cpp diff --git a/20160623/es-20160623.txt b/20160623/es-20160623.txt new file mode 100644 index 0000000..c8baae0 --- /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 0000000..6319e0e --- /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 0000000..72db25a --- /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 0000000..55a8f05 --- /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 0000000..a994f5d --- /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 0000000..b8a764d --- /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 0000000..40081dd --- /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 0000000..a6b15cd --- /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 0000000..518b0f6 --- /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 0000000..85deeb3 --- /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 0000000..453794a --- /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 0000000..be594cf --- /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 0000000..6911c59 --- /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 0000000..ad18750 --- /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 0000000..5c97440 --- /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 0000000..d5ccb0a --- /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 0000000..444e228 --- /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 0000000..4a866fe --- /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 0000000..a552079 --- /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 0000000..19ae7a3 --- /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 0000000..cdd2a98 --- /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 0000000..a8bebd5 --- /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; +} -- GitLab