finished all tasks

This commit is contained in:
Jonas Urban 2026-06-29 21:18:48 +02:00
parent 6303f5f224
commit a2680f18b4
21 changed files with 797 additions and 9 deletions

View File

@ -5,9 +5,11 @@ using namespace std;
class A class A
{ {
public: 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 class B : public A
{ {
public: public:
@ -21,12 +23,12 @@ public:
{ {
info("~B()"); info("~B()");
} }
void a() void a()
{ {
s += "1"; s += "1";
info("B::a()"); info("B::a()");
} }
virtual void b() void b() //virtual entfernt -> Aufruf dieser Funktion statt der Funktion in C für bc->b();
{ {
s += "2"; s += "2";
info("B::b()"); info("B::b()");
@ -80,7 +82,7 @@ int main()
A a; A a;
a.a(); a.a();
cout << "vvv " << B::liefereIdStat() << endl; cout << "vvv " << B::liefereIdStat() << endl;
B b; const B b; //const hinzugefügt -> Aufruf von: void b() const
b.b(); b.b();
cout << "www " << b.liefereIdStat() << endl; cout << "www " << b.liefereIdStat() << endl;
C* c[2]; C* c[2];

View File

@ -5,7 +5,7 @@ class Vektor{
public: public:
float x; float x;
float y; float y;
Vektor() : x(0), y(0) {} Vektor() : x(0), y(0) {}
Vektor(float x, float y) : x(x), y(y) {} Vektor(float x, float y) : x(x), y(y) {}
@ -15,7 +15,6 @@ class Vektor{
class Koerper{ class Koerper{
public: public:
float masse; float masse;
Vektor position; Vektor position;
Vektor geschwindigkeit; Vektor geschwindigkeit;

View File

@ -11,5 +11,5 @@ class Luftfahrzeug : public Verkehrsmittel{
Luftfahrzeug(int max_hoehe) : max_hoehe(max_hoehe), z(0) {} Luftfahrzeug(int max_hoehe) : max_hoehe(max_hoehe), z(0) {}
Luftfahrzeug(int max_hoehe, int z) : max_hoehe(max_hoehe), z(z) {} Luftfahrzeug(int max_hoehe, int z) : max_hoehe(max_hoehe), z(z) {}
std::string text(); string text();
}; };

22
7_Musiker/code/Klavier.h Normal file
View File

@ -0,0 +1,22 @@
#pragma once
#include <string>
#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;
}
};

52
7_Musiker/code/Musiker.h Normal file
View File

@ -0,0 +1,52 @@
#pragma once
#include <string>
#include <iostream>
#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() << ": " <<std::endl;
instrument->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; }
};

View File

@ -0,0 +1,28 @@
#pragma once
#include <string>
#include <iostream>
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; }
};

View File

@ -0,0 +1,38 @@
#pragma once
#include <string>
#include <iostream>
#include <vector>
#include "Musiker.h"
class Orchester
{
std::vector<Musiker const*> 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);
}
};

22
7_Musiker/code/Trompete.h Normal file
View File

@ -0,0 +1,22 @@
#pragma once
#include <string>
#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;
}
};

View File

@ -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;
}

View File

@ -0,0 +1,116 @@
#pragma once
#include <string>
#include <sstream>
#include <iostream>
#include <ostream>
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);

View File

@ -5,7 +5,7 @@
*/ */
#include "PrueffolgeReaktor.h" #include "PrueffolgeReaktor.h"
#include "Atomkern.h" #include "../Atomkern/Atomkern.h"
#include "Reaktor.h" #include "Reaktor.h"
using namespace std; using namespace std;

View File

@ -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);
}
};

View File

@ -0,0 +1,66 @@
#include <iostream>
#include <vector>
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<Produkt*> 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
*/

View File

@ -0,0 +1,50 @@
#include <iostream>
#include <string>
using namespace std;
template <typename T>
struct A {
const T x;
A(T x) : x(x) {}
};
template<typename T>
ostream& operator<<(ostream& os, const A<T>& a) {
os << a.x;
return os;
};
template<typename T, typename S>
struct B : public A<T> {
const S y;
const A<S> a;
B(const T& t, const S& s1, const S& s2) : A<T>(t), y(s1), a(s2) {}
};
template<typename T, typename S>
ostream& operator<<(ostream& os, const B<T,S>& b) {
os << b.x <<" "<< b.y<<" "<<b.a<<endl;
return os;
};
int main() {
A<int> a = 1;
cout << a << endl;
B<string,float> b = {"xyz", 2.6, 1.8};
cout << b << endl;
}
/*
Erwartete Ausgabe:
1
xyz 2.6 1.8
*/

View File

@ -0,0 +1,62 @@
#include <iostream>
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();
}

View File

View File

@ -0,0 +1,34 @@
#include <iostream>
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;
}

View File

@ -0,0 +1,94 @@
#include <iostream>
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;
}

View File

@ -0,0 +1,69 @@
#include <iostream>
#include <memory>
#include <stdexcept>
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<A> &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<A> 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<A> z1(new A{9});
cout << z1 << endl;
unique_ptr<A> z2(move(z1));
cout << z1 << endl;
cout << z2 << endl;
}

View File

@ -0,0 +1,59 @@
#include <iostream>
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);
}

View File

@ -0,0 +1,18 @@
#include <iostream>
#include <stdexcept>
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() << " "; }
}