Skip to content
Snippets Groups Projects
Commit 0bf2d8be authored by Alexander Steinmaurer's avatar Alexander Steinmaurer
Browse files

Examples 7th Lecture

parent 52329c26
No related branches found
No related tags found
No related merge requests found
Showing
with 438 additions and 0 deletions
#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
#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
#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
#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
#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
#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
#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
#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;
}
#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
#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
#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
#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
#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
#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;
}
}
#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
#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
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment