diff --git a/10_Ausgabe/code/Meine Reihenfolge.md b/10_Ausgabe/code/Meine Reihenfolge.md index 50c4fa5..9326438 100644 --- a/10_Ausgabe/code/Meine Reihenfolge.md +++ b/10_Ausgabe/code/Meine Reihenfolge.md @@ -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. diff --git a/10_Ausgabe/code/abgeändert b/10_Ausgabe/code/abgeändert new file mode 100755 index 0000000..cd1d7e3 Binary files /dev/null and b/10_Ausgabe/code/abgeändert differ diff --git a/10_Ausgabe/code/mainAbgändert.cpp b/10_Ausgabe/code/mainAbgändert.cpp new file mode 100644 index 0000000..38686ac --- /dev/null +++ b/10_Ausgabe/code/mainAbgändert.cpp @@ -0,0 +1,106 @@ +#include +#include +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; +} +