From 0bf2d8be87d407b5eb7159e12bc997ed4b266012 Mon Sep 17 00:00:00 2001 From: Alexander Steinmaurer <a@steinmaurer.cc> Date: Thu, 29 Apr 2021 16:02:24 +0200 Subject: [PATCH] Examples 7th Lecture --- 2021-oop1/vo/stream-07/01-unique-ptr-1.cpp | 25 ++++++++++++ 2021-oop1/vo/stream-07/02-unique-ptr-2.cpp | 24 ++++++++++++ 2021-oop1/vo/stream-07/03-unique-ptr-3.cpp | 23 +++++++++++ 2021-oop1/vo/stream-07/04-access-members.cpp | 38 +++++++++++++++++++ 2021-oop1/vo/stream-07/05-shared-ptr-1.cpp | 36 ++++++++++++++++++ 2021-oop1/vo/stream-07/05-shared-ptr-2.cpp | 37 ++++++++++++++++++ .../vo/stream-07/06-circular-dependency.cpp | 26 +++++++++++++ .../vo/stream-07/07-dangling-pointer.cpp | 29 ++++++++++++++ 2021-oop1/vo/stream-07/08-weak-ptr-1.cpp | 22 +++++++++++ 2021-oop1/vo/stream-07/09-weak-ptr-2.cpp | 38 +++++++++++++++++++ 2021-oop1/vo/stream-07/10-weak-ptr-3.cpp | 38 +++++++++++++++++++ 2021-oop1/vo/stream-07/11-circular-solved.cpp | 27 +++++++++++++ 2021-oop1/vo/stream-07/12-exceptions-1.cpp | 25 ++++++++++++ 2021-oop1/vo/stream-07/13-exceptions-2.cpp | 14 +++++++ 2021-oop1/vo/stream-07/14-exceptions-3.cpp | 14 +++++++ 2021-oop1/vo/stream-07/15-exceptions-4.cpp | 22 +++++++++++ 16 files changed, 438 insertions(+) create mode 100644 2021-oop1/vo/stream-07/01-unique-ptr-1.cpp create mode 100644 2021-oop1/vo/stream-07/02-unique-ptr-2.cpp create mode 100644 2021-oop1/vo/stream-07/03-unique-ptr-3.cpp create mode 100644 2021-oop1/vo/stream-07/04-access-members.cpp create mode 100644 2021-oop1/vo/stream-07/05-shared-ptr-1.cpp create mode 100644 2021-oop1/vo/stream-07/05-shared-ptr-2.cpp create mode 100644 2021-oop1/vo/stream-07/06-circular-dependency.cpp create mode 100644 2021-oop1/vo/stream-07/07-dangling-pointer.cpp create mode 100644 2021-oop1/vo/stream-07/08-weak-ptr-1.cpp create mode 100644 2021-oop1/vo/stream-07/09-weak-ptr-2.cpp create mode 100644 2021-oop1/vo/stream-07/10-weak-ptr-3.cpp create mode 100644 2021-oop1/vo/stream-07/11-circular-solved.cpp create mode 100644 2021-oop1/vo/stream-07/12-exceptions-1.cpp create mode 100644 2021-oop1/vo/stream-07/13-exceptions-2.cpp create mode 100644 2021-oop1/vo/stream-07/14-exceptions-3.cpp create mode 100644 2021-oop1/vo/stream-07/15-exceptions-4.cpp diff --git a/2021-oop1/vo/stream-07/01-unique-ptr-1.cpp b/2021-oop1/vo/stream-07/01-unique-ptr-1.cpp new file mode 100644 index 0000000..192fd28 --- /dev/null +++ b/2021-oop1/vo/stream-07/01-unique-ptr-1.cpp @@ -0,0 +1,25 @@ +#include <iostream> +#include <string> +#include <memory> + +struct SuperHero { + public: + SuperHero(std::string name) : name_(name) + { + std::cout << name_ << " wurde erzeugt" << std::endl; + }; + + ~SuperHero() + { + std::cout << name_ << " wurde zerstört" << std::endl; + }; + + private: + std::string name_; +}; + +int main() +{ + std::unique_ptr<SuperHero> s1(new SuperHero("Batman")); + return 0; +} \ No newline at end of file diff --git a/2021-oop1/vo/stream-07/02-unique-ptr-2.cpp b/2021-oop1/vo/stream-07/02-unique-ptr-2.cpp new file mode 100644 index 0000000..8d2936a --- /dev/null +++ b/2021-oop1/vo/stream-07/02-unique-ptr-2.cpp @@ -0,0 +1,24 @@ +#include <iostream> +#include <string> +#include <memory> + +struct SuperHero { + std::string name_; + SuperHero(std::string name) : name_(name) + { + std::cout << name_ << " wurde erzeugt" << std::endl; + }; + + ~SuperHero() + { + std::cout << name_ << " wurde zerstört" << std::endl; + }; +}; + +int main() +{ + { + std::unique_ptr<SuperHero> s1(new SuperHero("Batman")); + } //Scope von s1 endet => s1 wird zerstört. + return 0; +} \ No newline at end of file diff --git a/2021-oop1/vo/stream-07/03-unique-ptr-3.cpp b/2021-oop1/vo/stream-07/03-unique-ptr-3.cpp new file mode 100644 index 0000000..3b28f7f --- /dev/null +++ b/2021-oop1/vo/stream-07/03-unique-ptr-3.cpp @@ -0,0 +1,23 @@ +#include <iostream> +#include <string> +#include <memory> + +struct SuperHero { + std::string name_; + SuperHero(std::string name) : name_(name) + { + std::cout << name_ << " wurde erzeugt" << std::endl; + }; + + ~SuperHero() + { + std::cout << name_ << " wurde zerstört" << std::endl; + }; +}; + +int main() +{ + auto joker1 = std::make_shared<SuperHero>("Joker"); + std::weak_ptr<SuperHero> joker2(joker1); + std::cout << joker1.use_count() << std::endl; +} \ No newline at end of file diff --git a/2021-oop1/vo/stream-07/04-access-members.cpp b/2021-oop1/vo/stream-07/04-access-members.cpp new file mode 100644 index 0000000..d9c2a44 --- /dev/null +++ b/2021-oop1/vo/stream-07/04-access-members.cpp @@ -0,0 +1,38 @@ +#include <iostream> +#include <string> +#include <memory> + +struct SuperHero { + public: + SuperHero(std::string name, std::string phrase) : name_(name), + phrase_(phrase) + { + std::cout << name_ << " wurde erzeugt" << std::endl; + }; + + ~SuperHero() + { + std::cout << name_ << " wurde zerstört" << std::endl; + }; + + void SayPhrase() + { + std::cout << phrase_ << std::endl; + }; + + private: + std::string phrase_; + std::string name_; +}; + +int main() +{ + std::unique_ptr<SuperHero> s1(new SuperHero("Batman", "I am Batman!")); + std::unique_ptr<SuperHero> s2 = std::make_unique<SuperHero>("Iron Man", + "I love you 3000!"); + + s1->SayPhrase(); //I am Batman! + s2->SayPhrase(); //I love you 3000! + + return 0; +} \ No newline at end of file diff --git a/2021-oop1/vo/stream-07/05-shared-ptr-1.cpp b/2021-oop1/vo/stream-07/05-shared-ptr-1.cpp new file mode 100644 index 0000000..45a8ce6 --- /dev/null +++ b/2021-oop1/vo/stream-07/05-shared-ptr-1.cpp @@ -0,0 +1,36 @@ +#include <iostream> +#include <string> +#include <memory> + +struct SuperHero { + public: + SuperHero(std::string name) : name_(name) + { + std::cout << name_ << " wurde erzeugt" << std::endl; + }; + + ~SuperHero() + { + std::cout << name_ << " wurde zerstört" << std::endl; + }; + + void SayPhrase() + { + std::cout << phrase_ << std::endl; + }; + + private: + std::string phrase_; + std::string name_; +}; + +int main() +{ + std::shared_ptr<SuperHero> s1(new SuperHero("Batman")); //one pointer to object + std::shared_ptr<SuperHero> s2(s1); //two pointers to object + std::shared_ptr<SuperHero> s3 = s2; //three pointers to object + + std::cout << s1.use_count() << std::endl; //3 + + return 0; +} \ No newline at end of file diff --git a/2021-oop1/vo/stream-07/05-shared-ptr-2.cpp b/2021-oop1/vo/stream-07/05-shared-ptr-2.cpp new file mode 100644 index 0000000..26b95de --- /dev/null +++ b/2021-oop1/vo/stream-07/05-shared-ptr-2.cpp @@ -0,0 +1,37 @@ +#include <iostream> +#include <string> +#include <memory> + +struct SuperHero { + public: + SuperHero(std::string name) : name_(name) + { + std::cout << name_ << " wurde erzeugt" << std::endl; + }; + + ~SuperHero() + { + std::cout << name_ << " wurde zerstört" << std::endl; + }; + + void SayPhrase() + { + std::cout << phrase_ << std::endl; + }; + + private: + std::string phrase_; + std::string name_; +}; + +int main() +{ + std::shared_ptr<SuperHero> s1 = std::make_shared<SuperHero>("Batman"); + { + std::shared_ptr<SuperHero> s2(s1); + std::cout << s1.use_count() << std::endl; //2 + } + std::cout << s1.use_count() << std::endl; //1 + + return 0; +} \ No newline at end of file diff --git a/2021-oop1/vo/stream-07/06-circular-dependency.cpp b/2021-oop1/vo/stream-07/06-circular-dependency.cpp new file mode 100644 index 0000000..c5be5c4 --- /dev/null +++ b/2021-oop1/vo/stream-07/06-circular-dependency.cpp @@ -0,0 +1,26 @@ +#include <iostream> +#include <string> +#include <memory> + +struct SuperHero { + public: + SuperHero(std::string name) : name_(name) { }; + ~SuperHero() { }; + std::shared_ptr<SuperHero> enemie_; + + private: + std::string name_; +}; + +int main() +{ + std::shared_ptr<SuperHero> s1(new SuperHero("Batman")); + std::shared_ptr<SuperHero> s2 = std::make_shared<SuperHero>("Two-Face"); + + s1->enemie_ = s2; + s2->enemie_ = s1; + + return 0; +} + +// Check for valgrind :) \ No newline at end of file diff --git a/2021-oop1/vo/stream-07/07-dangling-pointer.cpp b/2021-oop1/vo/stream-07/07-dangling-pointer.cpp new file mode 100644 index 0000000..58dc9f5 --- /dev/null +++ b/2021-oop1/vo/stream-07/07-dangling-pointer.cpp @@ -0,0 +1,29 @@ +#include <iostream> +#include <string> +#include <memory> + +struct SuperHero { + public: + SuperHero(std::string name) : name_(name) { }; + ~SuperHero() { }; + std::string GetName() const + { + return name_; + } + + private: + std::string name_; +}; + +int main() +{ + auto joker1 = std::make_shared<SuperHero>("Joker"); + std::shared_ptr<SuperHero> joker2(joker1); + + joker1 = nullptr; + + std::cout << joker1->GetName() << std::endl; //Segmentation Fault + return 0; + + return 0; +} diff --git a/2021-oop1/vo/stream-07/08-weak-ptr-1.cpp b/2021-oop1/vo/stream-07/08-weak-ptr-1.cpp new file mode 100644 index 0000000..8977c47 --- /dev/null +++ b/2021-oop1/vo/stream-07/08-weak-ptr-1.cpp @@ -0,0 +1,22 @@ +#include <iostream> +#include <string> +#include <memory> + +struct SuperHero { + std::string name_; + SuperHero(std::string name) : name_(name) + { + std::cout << name_ << " wurde erzeugt" << std::endl; + }; + + ~SuperHero() + { + std::cout << name_ << " wurde zerstört" << std::endl; + }; +}; + +int main() +{ + std::unique_ptr<SuperHero> s1 = std::make_unique<SuperHero>("Batman"); + return 0; +} \ No newline at end of file diff --git a/2021-oop1/vo/stream-07/09-weak-ptr-2.cpp b/2021-oop1/vo/stream-07/09-weak-ptr-2.cpp new file mode 100644 index 0000000..7bad666 --- /dev/null +++ b/2021-oop1/vo/stream-07/09-weak-ptr-2.cpp @@ -0,0 +1,38 @@ +#include <iostream> +#include <string> +#include <memory> + +struct SuperHero { + std::string name_; + SuperHero(std::string name) : name_(name) + { + std::cout << name_ << " wurde erzeugt" << std::endl; + }; + + ~SuperHero() + { + std::cout << name_ << " wurde zerstört" << std::endl; + }; + + std::string GetName() const + { + return name_; + } + +}; + +int main() +{ + auto hero = std::make_shared<SuperHero>("Aquaman"); + std::weak_ptr<SuperHero> w_hero(hero); + + auto new_hero = w_hero.lock(); //Umwandlung in std::shared_ptr + + new_hero = nullptr; + hero = nullptr; + if(w_hero.expired()) + { + std::cout << "Kein Aquaman mehr :-("; + } + return 0; +} \ No newline at end of file diff --git a/2021-oop1/vo/stream-07/10-weak-ptr-3.cpp b/2021-oop1/vo/stream-07/10-weak-ptr-3.cpp new file mode 100644 index 0000000..a052e7d --- /dev/null +++ b/2021-oop1/vo/stream-07/10-weak-ptr-3.cpp @@ -0,0 +1,38 @@ +#include <iostream> +#include <string> +#include <memory> + +struct SuperHero { + std::string name_; + SuperHero(std::string name) : name_(name) + { + std::cout << name_ << " wurde erzeugt" << std::endl; + }; + + ~SuperHero() + { + std::cout << name_ << " wurde zerstört" << std::endl; + }; + + std::string GetName() const + { + return name_; + } + +}; + +int main() +{ + auto hero = std::make_shared<SuperHero>("Aquaman"); + std::weak_ptr<SuperHero> w_hero(hero); + //std::cout << w_hero->GetName(); --> Zugriff nicht möglich! + + std::cout << w_hero.use_count() << std::endl; //1 + + auto new_hero = w_hero.lock(); //Umwandlung in std::shared_ptr + std::cout << new_hero->GetName(); + + std::cout << w_hero.use_count() << std::endl; //2 + + return 0; +} \ No newline at end of file diff --git a/2021-oop1/vo/stream-07/11-circular-solved.cpp b/2021-oop1/vo/stream-07/11-circular-solved.cpp new file mode 100644 index 0000000..9c501d7 --- /dev/null +++ b/2021-oop1/vo/stream-07/11-circular-solved.cpp @@ -0,0 +1,27 @@ +#include <iostream> +#include <string> +#include <memory> + +struct SuperHero { + public: + SuperHero(std::string name) : name_(name) { }; + ~SuperHero() { }; + std::weak_ptr<SuperHero> enemie_; + + private: + std::string name_; +}; + + +int main() +{ + std::shared_ptr<SuperHero> s1(new SuperHero("Batman")); + std::shared_ptr<SuperHero> s2 = std::make_shared<SuperHero>("Two-Face"); + + s1->enemie_ = s2; + s2->enemie_ = s1; + + return 0; +} + +//Check with Valgrind :) \ No newline at end of file diff --git a/2021-oop1/vo/stream-07/12-exceptions-1.cpp b/2021-oop1/vo/stream-07/12-exceptions-1.cpp new file mode 100644 index 0000000..781ff32 --- /dev/null +++ b/2021-oop1/vo/stream-07/12-exceptions-1.cpp @@ -0,0 +1,25 @@ +#include <iostream> + +double division(int var1, int var2) +{ + if (var2 == 0) { + throw "Division by Zero."; + } + return (var1 / var2); +} + +int main() +{ + int a = 30; + int b = 0; + double d = 0; + + try { + d = division(a, b); + std::cout << d << std::endl; + } + catch (const char* error) { + std::cout << error << std::endl; + } + return 0; +} \ No newline at end of file diff --git a/2021-oop1/vo/stream-07/13-exceptions-2.cpp b/2021-oop1/vo/stream-07/13-exceptions-2.cpp new file mode 100644 index 0000000..e4822f3 --- /dev/null +++ b/2021-oop1/vo/stream-07/13-exceptions-2.cpp @@ -0,0 +1,14 @@ +#include <iostream> +#include <exception> + +int main() +{ + try { + int* myarray = new int[1000000000000]; + } catch (std::bad_alloc &ex) { + std::cout << "Wozu brauchst du so viel Speicher?! " << ex.what() << std::endl; + } + catch (std::exception &ex) { + std::cout << ex.what() << std::endl; + } +} diff --git a/2021-oop1/vo/stream-07/14-exceptions-3.cpp b/2021-oop1/vo/stream-07/14-exceptions-3.cpp new file mode 100644 index 0000000..ceb0382 --- /dev/null +++ b/2021-oop1/vo/stream-07/14-exceptions-3.cpp @@ -0,0 +1,14 @@ +#include <iostream> +#include <exception> + +int main() +{ + try { + std::vector<int> rules = {34, 35}; + rules.at(2); + } catch(std::out_of_range &ex){ + std::cout << "out_of_range " << ex.what() << std::endl; + } catch (std::exception &ex) { + std::cout << "exception " << ex.what() << std::endl; + } +} \ No newline at end of file diff --git a/2021-oop1/vo/stream-07/15-exceptions-4.cpp b/2021-oop1/vo/stream-07/15-exceptions-4.cpp new file mode 100644 index 0000000..03fe779 --- /dev/null +++ b/2021-oop1/vo/stream-07/15-exceptions-4.cpp @@ -0,0 +1,22 @@ +#include <iostream> +#include <exception> + +class MyException : public std::exception +{ + public: + const char* what () const throw () + { + return "Ich schmeiße mit Exceptions um mich!"; + } +}; + +int main() +{ + try { + throw MyException(); + } catch (MyException& e) { + std::cout << e.what() << std::endl; + } catch (std::exception& e) { + std::cout << e.what(); + } +} \ No newline at end of file -- GitLab