From a2680f18b457b3f497cc75bf7ccbc6dd8208c9eb Mon Sep 17 00:00:00 2001 From: Jonas Date: Mon, 29 Jun 2026 21:18:48 +0200 Subject: [PATCH] finished all tasks --- 10_Ausgabe/code/main.cpp | 12 +- 5_Flugkurve03/code/flugkurve03.h | 3 +- 6_Verkehrsmittel/Luftfahrzeug.h | 2 +- 7_Musiker/code/Klavier.h | 22 ++++ 7_Musiker/code/Musiker.h | 52 ++++++++ 7_Musiker/code/Musikinstrument.h | 28 +++++ 7_Musiker/code/Orchester.h | 38 ++++++ 7_Musiker/code/Trompete.h | 22 ++++ 8_9_Atomkern/code/Atomkern/Atomkern.cpp | 18 +++ 8_9_Atomkern/code/Atomkern/Atomkern.h | 116 ++++++++++++++++++ .../code/Reaktor/PrueffolgeReaktor.cpp | 2 +- 8_9_Atomkern/code/Reaktor/Reaktor.h | 39 ++++++ Klausurvorbereitung/3_1_Fabrik.cpp | 66 ++++++++++ .../3_2_Parametrierbare_Klassen.cpp | 50 ++++++++ Klausurvorbereitung/WS11_2.cpp | 62 ++++++++++ Klausurvorbereitung/WS19.cpp | 0 Klausurvorbereitung/WS19_2.1.cpp | 34 +++++ Klausurvorbereitung/WS19_2.2.cpp | 94 ++++++++++++++ Klausurvorbereitung/WS19_2.3.cpp | 69 +++++++++++ Klausurvorbereitung/WS24.cpp | 59 +++++++++ Klausurvorbereitung/test.cpp | 18 +++ 21 files changed, 797 insertions(+), 9 deletions(-) create mode 100644 7_Musiker/code/Klavier.h create mode 100644 7_Musiker/code/Musiker.h create mode 100644 7_Musiker/code/Musikinstrument.h create mode 100644 7_Musiker/code/Orchester.h create mode 100644 7_Musiker/code/Trompete.h create mode 100644 8_9_Atomkern/code/Atomkern/Atomkern.cpp create mode 100644 8_9_Atomkern/code/Atomkern/Atomkern.h create mode 100644 8_9_Atomkern/code/Reaktor/Reaktor.h create mode 100644 Klausurvorbereitung/3_1_Fabrik.cpp create mode 100644 Klausurvorbereitung/3_2_Parametrierbare_Klassen.cpp create mode 100644 Klausurvorbereitung/WS11_2.cpp create mode 100644 Klausurvorbereitung/WS19.cpp create mode 100644 Klausurvorbereitung/WS19_2.1.cpp create mode 100644 Klausurvorbereitung/WS19_2.2.cpp create mode 100644 Klausurvorbereitung/WS19_2.3.cpp create mode 100644 Klausurvorbereitung/WS24.cpp create mode 100644 Klausurvorbereitung/test.cpp diff --git a/10_Ausgabe/code/main.cpp b/10_Ausgabe/code/main.cpp index 025a43f..369092a 100644 --- a/10_Ausgabe/code/main.cpp +++ b/10_Ausgabe/code/main.cpp @@ -5,9 +5,11 @@ using namespace std; class A { public: - void a() {} -}; + virtual void a() {} //virtual hinzugefügt -> Aufruf der Funktion in C für ac->a(); + virtual ~A() {}//hinzugefügt -> Aufruf der Deskstruktoren con B und C + +}; class B : public A { public: @@ -21,12 +23,12 @@ public: { info("~B()"); } - void a() + void a() { s += "1"; info("B::a()"); } - virtual void b() + void b() //virtual entfernt -> Aufruf dieser Funktion statt der Funktion in C für bc->b(); { s += "2"; info("B::b()"); @@ -80,7 +82,7 @@ int main() A a; a.a(); cout << "vvv " << B::liefereIdStat() << endl; - B b; + const B b; //const hinzugefügt -> Aufruf von: void b() const b.b(); cout << "www " << b.liefereIdStat() << endl; C* c[2]; diff --git a/5_Flugkurve03/code/flugkurve03.h b/5_Flugkurve03/code/flugkurve03.h index 4d48154..eaf0999 100644 --- a/5_Flugkurve03/code/flugkurve03.h +++ b/5_Flugkurve03/code/flugkurve03.h @@ -5,7 +5,7 @@ class Vektor{ public: float x; float y; - + Vektor() : x(0), y(0) {} Vektor(float x, float y) : x(x), y(y) {} @@ -15,7 +15,6 @@ class Vektor{ class Koerper{ public: - float masse; Vektor position; Vektor geschwindigkeit; diff --git a/6_Verkehrsmittel/Luftfahrzeug.h b/6_Verkehrsmittel/Luftfahrzeug.h index 9ef8ccb..77870c9 100644 --- a/6_Verkehrsmittel/Luftfahrzeug.h +++ b/6_Verkehrsmittel/Luftfahrzeug.h @@ -11,5 +11,5 @@ class Luftfahrzeug : public Verkehrsmittel{ Luftfahrzeug(int max_hoehe) : max_hoehe(max_hoehe), z(0) {} Luftfahrzeug(int max_hoehe, int z) : max_hoehe(max_hoehe), z(z) {} - std::string text(); + string text(); }; \ No newline at end of file diff --git a/7_Musiker/code/Klavier.h b/7_Musiker/code/Klavier.h new file mode 100644 index 0000000..4abd73e --- /dev/null +++ b/7_Musiker/code/Klavier.h @@ -0,0 +1,22 @@ +#pragma once + +#include +#include "Musikinstrument.h" + +class Klavier : public Musikinstrument +{ +public: + Klavier() : Musikinstrument("Klavier") + { + std::cout << "Konstruktor : Klavier() : " << std::endl; + } + virtual ~Klavier() + { + std::cout << "Destruktor : ~Klavier() : " << std::endl; + } + + void spielen() const + { + std::cout << "Das Klavier klimpert" << std::endl; + } +}; \ No newline at end of file diff --git a/7_Musiker/code/Musiker.h b/7_Musiker/code/Musiker.h new file mode 100644 index 0000000..7b4640e --- /dev/null +++ b/7_Musiker/code/Musiker.h @@ -0,0 +1,52 @@ +#pragma once + +#include +#include +#include "Musikinstrument.h" + +class Musiker +{ + std::string const name; + Musikinstrument const* instrument; + +public: + Musiker(const std::string& n) : name(n), instrument(nullptr) + { + std::cout << "Konstruktor : Musiker(string name) : " << name << std::endl; + } + Musiker() : name("Name unbekannt"), instrument(nullptr) + { + std::cout << "Konstruktor : Musiker() : " << name << std::endl; + } + virtual ~Musiker() + { + std::cout << "Destruktor : ~Musiker() : " << name << std::endl; + delete instrument; + } + + void weiseInstrumentZu(Musikinstrument const* instrumentToAssign) + { + if(instrument != nullptr) + { + std::cout << "Der Musiker " << name << " hat bereits ein Instrument zugewiesen bekommen. Das alte Instrument wird durch das neue ersetzt" << std::endl; + delete instrument; + } + instrument = instrumentToAssign; + std::cout << "Der Musiker " << name << " erhält das Instrument " << instrument->liefereName() << std::endl; + } + + void spielen() const + { + if(instrument != nullptr) + { + std::cout << name << " spielt " << instrument->liefereName() << ": " <spielen(); + } + else + { + std::cout << "Der Musiker " << name << " hat kein Instrument zugewiesen bekommen und kann daher nicht spielen." << std::endl; + } + } + + std::string liefereName() const { return name; } +}; \ No newline at end of file diff --git a/7_Musiker/code/Musikinstrument.h b/7_Musiker/code/Musikinstrument.h new file mode 100644 index 0000000..1a3c359 --- /dev/null +++ b/7_Musiker/code/Musikinstrument.h @@ -0,0 +1,28 @@ +#pragma once + +#include +#include + +class Musikinstrument +{ + std::string const name; + +public: + Musikinstrument(const std::string& n) : name(n) + { + std::cout << "Konstruktor : Musikinstrument(string name) : " << name << std::endl; + } + Musikinstrument() : name("Irgendein Musikinstrument") + { + std::cout << "Konstruktor : Musikinstrument() : " << name << std::endl; + } + + virtual ~Musikinstrument() { std::cout << "Destruktor : ~Musikinstrument() : " << name << std::endl; } + + virtual void spielen() const + { + std::cout << "Irgendein Musikinstrument erklingt irgendwie." << std::endl; + } + + std::string liefereName() const { return name; } +}; \ No newline at end of file diff --git a/7_Musiker/code/Orchester.h b/7_Musiker/code/Orchester.h new file mode 100644 index 0000000..da8d082 --- /dev/null +++ b/7_Musiker/code/Orchester.h @@ -0,0 +1,38 @@ +#pragma once + +#include +#include +#include +#include "Musiker.h" + +class Orchester +{ + std::vector musiker; + +public: + Orchester() + { + std::cout << "Konstruktor : Orchester() : " << std::endl; + } + virtual ~Orchester() + { + std::cout << "Destruktor : ~Orchester() : " << std::endl; + for(Musiker const* m : musiker) + { + delete m; + } + } + + void spielen() const + { + for(Musiker const* m : musiker) + { + m->spielen(); + } + } + + void hinzufuegen(Musiker const* m) + { + musiker.push_back(m); + } +}; \ No newline at end of file diff --git a/7_Musiker/code/Trompete.h b/7_Musiker/code/Trompete.h new file mode 100644 index 0000000..4052520 --- /dev/null +++ b/7_Musiker/code/Trompete.h @@ -0,0 +1,22 @@ +#pragma once + +#include +#include "Musikinstrument.h" + +class Trompete : public Musikinstrument +{ +public: + Trompete() : Musikinstrument("Trompete") + { + std::cout << "Konstruktor : Trompete() : " << std::endl; + } + virtual ~Trompete() + { + std::cout << "Destruktor : ~Trompete() : " << std::endl; + } + + void spielen() const + { + std::cout << "Die Trompete trötet" << std::endl; + } +}; \ No newline at end of file diff --git a/8_9_Atomkern/code/Atomkern/Atomkern.cpp b/8_9_Atomkern/code/Atomkern/Atomkern.cpp new file mode 100644 index 0000000..c01a9ea --- /dev/null +++ b/8_9_Atomkern/code/Atomkern/Atomkern.cpp @@ -0,0 +1,18 @@ +#include "Atomkern.h" + +const Atomkern Atomkern::PROTON = Atomkern("p", 1, 1); +const Atomkern Atomkern::NEUTRON = Atomkern("n", 1, 0); + +std::ostream& operator << (std::ostream& os, const Atomkern& atomkern) +{ + os << atomkern._elementsymbol << "(" << atomkern._massenzahl << "," << atomkern._ordnungszahl << ")"; + return os; +} + +Atomkern operator * (int factor, const Atomkern& atomkern) +{ + Atomkern ergebnis; + ergebnis._ordnungszahl = atomkern._ordnungszahl * factor; + ergebnis._massenzahl = atomkern._massenzahl * factor; + return ergebnis; +} \ No newline at end of file diff --git a/8_9_Atomkern/code/Atomkern/Atomkern.h b/8_9_Atomkern/code/Atomkern/Atomkern.h new file mode 100644 index 0000000..2fce83a --- /dev/null +++ b/8_9_Atomkern/code/Atomkern/Atomkern.h @@ -0,0 +1,116 @@ +#pragma once +#include +#include +#include +#include + +class Atomkern +{ + public: + + static const Atomkern PROTON; + static const Atomkern NEUTRON; + + std::string _elementsymbol; + int _ordnungszahl; + int _massenzahl; + + + + Atomkern(std::string elementsymbol, int massenzahl, int ordnungszahl) + { + this->_elementsymbol = elementsymbol; + this->_ordnungszahl = ordnungszahl; + this->_massenzahl = massenzahl; + } + + Atomkern(int massenzahl, int ordnungszahl) + { + this->_elementsymbol = " "; + this->_ordnungszahl = ordnungszahl; + this->_massenzahl = massenzahl; + } + + + Atomkern() // Standardatomkern: Wasserstoff + { + this->_elementsymbol = "H"; + this->_ordnungszahl = 1; + this->_massenzahl = 1; + } + + ~Atomkern() {} + + std::string symbol() const + { + return this->_elementsymbol; + } + + int ordnungszahl() const + { + return this->_ordnungszahl; + } + + int massenzahl() const + { + return this->_massenzahl; + } + + Atomkern operator + (const Atomkern& atomkern_to_add) const + { + Atomkern ergebnis; + + ergebnis._ordnungszahl = this->_ordnungszahl + atomkern_to_add._ordnungszahl; + ergebnis._massenzahl = this->_massenzahl + atomkern_to_add._massenzahl; + + return ergebnis; + } + + void operator += (const Atomkern& atomkern_to_add) + { + this->_ordnungszahl += atomkern_to_add._ordnungszahl; + this->_massenzahl += atomkern_to_add._massenzahl; + } + + Atomkern operator - (const Atomkern& atomkern_to_subtract) const + { + Atomkern ergebnis; + + ergebnis._ordnungszahl = this->_ordnungszahl - atomkern_to_subtract._ordnungszahl; + ergebnis._massenzahl = this->_massenzahl - atomkern_to_subtract._massenzahl; + + return ergebnis; + } + + void operator -= (const Atomkern& atomkern_to_subtract) + { + this->_ordnungszahl -= atomkern_to_subtract._ordnungszahl; + this->_massenzahl -= atomkern_to_subtract._massenzahl; + } + + Atomkern operator * (const int factor) const + { + Atomkern ergebnis; + + ergebnis._ordnungszahl = this->_ordnungszahl * factor; + ergebnis._massenzahl = this->_massenzahl * factor; + + return ergebnis; + } + + + int operator == (const Atomkern& atomkern_to_compare) const + { + if (this->_ordnungszahl == atomkern_to_compare._ordnungszahl && this->_massenzahl == atomkern_to_compare._massenzahl) + { + return 1; + } + else + { + return 0; + } + } +}; + +std::ostream& operator<<(std::ostream& os, const Atomkern& atomkern); +Atomkern operator*(int factor, const Atomkern& atomkern); \ No newline at end of file diff --git a/8_9_Atomkern/code/Reaktor/PrueffolgeReaktor.cpp b/8_9_Atomkern/code/Reaktor/PrueffolgeReaktor.cpp index 21f4872..331709e 100644 --- a/8_9_Atomkern/code/Reaktor/PrueffolgeReaktor.cpp +++ b/8_9_Atomkern/code/Reaktor/PrueffolgeReaktor.cpp @@ -5,7 +5,7 @@ */ #include "PrueffolgeReaktor.h" -#include "Atomkern.h" +#include "../Atomkern/Atomkern.h" #include "Reaktor.h" using namespace std; diff --git a/8_9_Atomkern/code/Reaktor/Reaktor.h b/8_9_Atomkern/code/Reaktor/Reaktor.h new file mode 100644 index 0000000..b4368ae --- /dev/null +++ b/8_9_Atomkern/code/Reaktor/Reaktor.h @@ -0,0 +1,39 @@ +#pragma once +#include "../Atomkern/Atomkern.h" + +class Reaktor +{ + int _massenzahl_summe; + int _ordnungszahl_summe; + + public: + + Reaktor() : _massenzahl_summe(0), _ordnungszahl_summe(0) {} + ~Reaktor() {} + + Reaktor& operator < (const Atomkern& atomkern_to_add) + { + this->_massenzahl_summe += atomkern_to_add.massenzahl(); + this->_ordnungszahl_summe += atomkern_to_add.ordnungszahl(); + return *this; + } + + Reaktor& operator > (const Atomkern& atomkern_to_subtract) + { + this->_massenzahl_summe -= atomkern_to_subtract.massenzahl(); + this->_ordnungszahl_summe -= atomkern_to_subtract.ordnungszahl(); + return *this; + } + + void operator ~ () + { + this->_massenzahl_summe = 0; + this->_ordnungszahl_summe = 0; + } + + Atomkern operator * () const + { + return Atomkern(this->_massenzahl_summe, this->_ordnungszahl_summe); + } +}; + diff --git a/Klausurvorbereitung/3_1_Fabrik.cpp b/Klausurvorbereitung/3_1_Fabrik.cpp new file mode 100644 index 0000000..a508693 --- /dev/null +++ b/Klausurvorbereitung/3_1_Fabrik.cpp @@ -0,0 +1,66 @@ +#include +#include +using namespace std; + +struct Produkt { + virtual ~Produkt() {} + virtual string info() = 0; +}; + +struct Fabrik { + + virtual ~Fabrik() {} + + virtual Produkt* erzeuge(string const&) = 0; +}; + +class KonkretesProdukt : public Produkt { +private: + + string const _info; +public: + KonkretesProdukt(const string& i) : _info(i) {} + + virtual string info() {return _info;} +}; + + +class KonkreteFabrik : public Fabrik { +private: + vector produkte; +public: + ~KonkreteFabrik() { + + for(Produkt* p : produkte) { + + cout << "Lösche Produkt " << p->info() << endl; + + delete p; + } + } + Produkt* erzeuge(const string& s) { + + Produkt* newProduct = new KonkretesProdukt(s); + + produkte.push_back(newProduct); + + return newProduct; + } +}; + +int main() { + Fabrik* fabrik = new KonkreteFabrik(); + Produkt* p1 = fabrik->erzeuge("p1"); + cout << "Erzeugtes Produkt: " << p1->info() << endl; + Produkt* p2 = fabrik->erzeuge("p2"); + cout << "Erzeugtes Produkt: " << p2->info() << endl; + delete fabrik; +} + +/* +Erwartete Ausgabe: +Erzeugtes Produkt: p1 +Erzeugtes Produkt: p2 +Lösche Produkt p1 +Lösche Produkt p2 +*/ diff --git a/Klausurvorbereitung/3_2_Parametrierbare_Klassen.cpp b/Klausurvorbereitung/3_2_Parametrierbare_Klassen.cpp new file mode 100644 index 0000000..63f5345 --- /dev/null +++ b/Klausurvorbereitung/3_2_Parametrierbare_Klassen.cpp @@ -0,0 +1,50 @@ +#include +#include +using namespace std; + + +template +struct A { + + const T x; + + A(T x) : x(x) {} +}; + +template +ostream& operator<<(ostream& os, const A& a) { + + os << a.x; + return os; +}; + +template + +struct B : public A { + + const S y; + const A a; + + B(const T& t, const S& s1, const S& s2) : A(t), y(s1), a(s2) {} +}; + +template + +ostream& operator<<(ostream& os, const B& b) { + + os << b.x <<" "<< b.y<<" "< a = 1; + cout << a << endl; + B b = {"xyz", 2.6, 1.8}; + cout << b << endl; +} + +/* +Erwartete Ausgabe: +1 +xyz 2.6 1.8 +*/ diff --git a/Klausurvorbereitung/WS11_2.cpp b/Klausurvorbereitung/WS11_2.cpp new file mode 100644 index 0000000..011c583 --- /dev/null +++ b/Klausurvorbereitung/WS11_2.cpp @@ -0,0 +1,62 @@ +#include +using namespace std; + +class A +{ + public: + ~A() {} + + void const a() {} +}; + +class P +{ + public: + + int x; + int y; + + P() : x(0), y(0) {} + P(int x, int y) : x(x), y(y) {} + + +}; +ostream& operator << (ostream& os, P p) +{ + os << "(" << p.x << "," << p.y << ")"; + return os; +} +class B : public A +{ + P p; + public: + B(const P& p) : p(p) {} + + protected: + void const b() + { + cout << "B::p = " << p; + } +}; + +class C : public B +{ + public: + C(const P& p) : B(p) {} + + void const a() + { + B::b(); + } +}; + + + +int main() +{ + P p(19,20); + cout << "p=" << p << endl; + C c(p); + cout << "c.a() liefert: "; + c.a(); +} \ No newline at end of file diff --git a/Klausurvorbereitung/WS19.cpp b/Klausurvorbereitung/WS19.cpp new file mode 100644 index 0000000..e69de29 diff --git a/Klausurvorbereitung/WS19_2.1.cpp b/Klausurvorbereitung/WS19_2.1.cpp new file mode 100644 index 0000000..e7fa2ff --- /dev/null +++ b/Klausurvorbereitung/WS19_2.1.cpp @@ -0,0 +1,34 @@ +#include +using namespace std; +struct A +{ + void f() + { + cout << "A::f\n"; + g(); + h(); + } + virtual void g() { cout << "A::g\n"; } + void h() { cout << "A::h\n"; } +}; +struct B : A +{ + void g() { cout << "B::g\n"; } + virtual void h() { cout << "B::h\n"; } +}; +struct C : B +{ + virtual void g() { cout << "C::g\n"; } + virtual void h() { cout << "C::h\n"; } +}; +int main() +{ + B b; + C c; + B *z = new C; + b.f(); + z->g(); + z->h(); + c.f(); + delete z; +} \ No newline at end of file diff --git a/Klausurvorbereitung/WS19_2.2.cpp b/Klausurvorbereitung/WS19_2.2.cpp new file mode 100644 index 0000000..9dc2fd8 --- /dev/null +++ b/Klausurvorbereitung/WS19_2.2.cpp @@ -0,0 +1,94 @@ +#include +using namespace std; +struct A +{ + static int n; + const int id; + char *x; + A(int i) : id(++n) + { + cout << "A(int) " << id << "\n"; + x = new char(i); + } + A(const A &a) : id(++n) + { + cout << "A(const A&) " << id << "\n"; + x = new char(*(a.x)); + } + A(A &&a) : id(++n) + { + cout << "A(A&&) " << id << "\n"; + x = a.x; + a.x = nullptr; + } + virtual ~A() + { + cout << "~A() " << id << "\n"; + if (x != nullptr) + { + delete x; + } + } + A &operator=(const A &a) + { + cout << "operator=(constA&) " << id << " " << a.id << endl; + if (&a != this) + { + if (x == nullptr) + { + x = new char(*(a.x)); + } + else + { + *x = *(a.x); + } + } + return *this; + } + A &operator=(A &&a) + { + cout << "operator=(constA&&) " << id << " " << a.id << endl; + if (&a != this) + { + if (x != nullptr) + { + delete x; + } + x = a.x; + a.x = nullptr; + } + return *this; + } +}; + +int A::n = 0; + +ostream &operator<<(ostream &os, const A &a) +{ + os << a.id << " "; + if (a.x != nullptr) + { + os << *(a.x); + } + return os; +} + +int main() +{ + A a1 = {'X'}; + + cout << a1 << "\n" + << endl; + A a2 = {a1}; + cout << a2 << "\n" + << endl; + A a3 = {move(a1)}; + cout << a1 << endl; + cout << a3 << endl; + a1 = move(a3); + cout << a1 << endl; + cout << a3 << endl; + a3 = a1; + cout << a1 << endl; + cout << a3 << endl; +} \ No newline at end of file diff --git a/Klausurvorbereitung/WS19_2.3.cpp b/Klausurvorbereitung/WS19_2.3.cpp new file mode 100644 index 0000000..5e7f1fa --- /dev/null +++ b/Klausurvorbereitung/WS19_2.3.cpp @@ -0,0 +1,69 @@ +#include +#include +#include +using namespace std; +struct A +{ + int i; + A(int i) : i(i) { cout << "A()\n"; } + A(A &&a) : i(a.i) { cout << "A(A&&)\n"; } + ~A() { cout << "~A()\n"; } +}; + +ostream &operator<<(ostream &os, const unique_ptr &z) +{ + if (z == nullptr) + { + os << "nullptr"; + } + else + { + os << z->i; + } + return os; +} + +void e(int i) +{ + if (i == 0) + { + throw exception(); + } + else + { + throw invalid_argument(""); + } +} +void f1() +{ + A *z = new A(1); + e(0); + delete z; + cout << "Ende f1()\n"; +} +void f2() +{ + unique_ptr z(new A(2)); + e(1); + cout << "Ende f2()\n"; +} +int main() +{ + + try { f1();} + + catch(const exception& e) {cout << "S\n";} + catch(const invalid_argument& e) {cout << "T\n";} + + try {f2();} + + catch(const invalid_argument& e) {cout << "T\n";} + catch(const exception& e) {cout << "S\n";} + + unique_ptr z1(new A{9}); + cout << z1 << endl; + + unique_ptr z2(move(z1)); + cout << z1 << endl; + cout << z2 << endl; +} \ No newline at end of file diff --git a/Klausurvorbereitung/WS24.cpp b/Klausurvorbereitung/WS24.cpp new file mode 100644 index 0000000..e46ea71 --- /dev/null +++ b/Klausurvorbereitung/WS24.cpp @@ -0,0 +1,59 @@ +#include +using namespace std; +struct A +{ + int i; + A(int i) : i(i) {} + A(const A &a) : i(a.i) {} + A &operator=(int i) + { + this->i = i; + return *this; + } + A &operator-() + { + i = -i; + return *this; + } + A operator-() const + { + return A{-i}; + } + A &operator-(const A &a) + { + i -= a.i; + return *this; + } + A operator-(const A &a) const + { + return A{i - a.i}; + } +}; + +ostream &operator<<(ostream &os, const A &a) +{ + os << a.i; + return os; +} + +int main() +{ + A a1{1}; + const A a2{2}; + + A a3{-a1}; + cout << a1 << " " << a3 << endl; + + A a4{-a2}; + cout << a2 << " " << a4 << endl; + + a4 = 4; + a1 = -a4; + cout << a1 << " " << a4 << endl; + + A a5 = a1-a4; + cout << a1 << " " << a4 << " " << a5 << endl; + + const A a6{11}; + cout << -(a2-a6); +} \ No newline at end of file diff --git a/Klausurvorbereitung/test.cpp b/Klausurvorbereitung/test.cpp new file mode 100644 index 0000000..19e8a77 --- /dev/null +++ b/Klausurvorbereitung/test.cpp @@ -0,0 +1,18 @@ +#include +#include +using namespace std; + +void f() { throw invalid_argument("1"); } +void g() { throw runtime_error("2"); } +int main() { +try { f(); } +try { g(); } +catch(const runtime_error& e) { cout << "3 " << e.what() << " "; } +catch(const invalid_argument& e) { cout << "4 " << e.what() << " "; } +catch(const exception& e) { cout << "5 " << e.what() << " "; } + +//catch(const exception& e) { cout << "6 " << e.what() << " "; } +catch(const invalid_argument& e) { cout << "7 " << e.what() << " "; } +catch(const runtime_error& e) { cout << "8 " << e.what() << " "; } +catch(const exception& e) { cout << "6 " << e.what() << " "; } +} \ No newline at end of file