finished all tasks

This commit is contained in:
2026-06-29 21:18:48 +02:00
parent 6303f5f224
commit a2680f18b4
21 changed files with 797 additions and 9 deletions
+66
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
*/
@@ -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
*/
+62
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
+34
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;
}
+94
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;
}
+69
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;
}
+59
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);
}
+18
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() << " "; }
}