Aufgabe fertig, alles abgeändert so wies soll

This commit is contained in:
Tobias Kachel 2026-06-18 21:42:57 +02:00
parent faf2f3a217
commit e8754a1ab4
3 changed files with 108 additions and 1 deletions

View File

@ -132,4 +132,5 @@ zzz 6
```
# Was waren meine Fehler?
Ich hab vergessen, das mit jeden C Objekt auch den B Konstruktor aufgerufen wird
Nach xxx 4 hatte ich gedacht, der Destuktor von C wird aufgerufen, aber eigentlich wird der Destuktor von A aufgerufen.
Ich hab die Reihenfolge, mit der die Objekte nach zzz 6 gelöscht werden vertauscht.

BIN
10_Ausgabe/code/abgeändert Executable file

Binary file not shown.

View File

@ -0,0 +1,106 @@
#include <iostream>
#include <string>
using namespace std;
class A
{
public:
virtual void a() {}
virtual ~A() = default;
};
class B : public A
{
public:
B(string s="")
{
id = ++idStat;
this->s = s;
info("B()");
}
virtual ~B()
{
info("~B()");
}
void a()
{
s += "1";
info("B::a()");
}
void b()
{
s += "2";
info("B::b()");
}
void b() const
{
info("B::b() const");
}
static int liefereIdStat()
{
return idStat;
}
protected:
string s;
void info(const string& text) const
{
cout << id << ": " << text << " " << s << endl;
}
private:
int id;
static int idStat;
};
int B::idStat = 0;
class C : public B
{
public:
C() : B("C")
{
info("C()");
}
virtual ~C()
{
info("~C()");
}
void a()
{
s += "3";
info("C::a()");
}
void b()
{
s += "4";
info("C::b()");
}
};
int main()
{
A a;
a.a();
cout << "vvv " << B::liefereIdStat() << endl;
B const b;
b.b();
cout << "www " << b.liefereIdStat() << endl;
C* c[2];
c[0] = new C;
c[1] = new C;
c[0]->a();
c[1]->b();
delete c[0];
delete c[1];
const C c2;
cout << "xxx " << C::liefereIdStat() << endl;
A* ac = new C;
ac->a();
delete ac;
cout << "yyy " << B::liefereIdStat() << endl;
B* bc = new C;
bc->a();
bc->b();
delete bc;
cout << "zzz " << B::liefereIdStat() << endl;
}