Created
January 29, 2018 21:56
-
-
Save jkomyno/f99299ac973cbaa6f153c4e91760fd11 to your computer and use it in GitHub Desktop.
P2 - 1° appello, 29.01.2018
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
using namespace std; | |
class A { | |
protected: | |
virtual void j() { cout << " A::j"; } | |
public: | |
virtual void g() const { cout << " A::g "; } | |
virtual void f() { cout << " A::f "; g(); j(); } | |
void m() { cout << " A::m "; g(); j(); } | |
virtual void k() { cout << " A::k "; j(); m(); } | |
virtual A* n() { cout << " A::n "; return this; } | |
}; | |
class B : public A { | |
public: | |
virtual void g() const override { cout << " B::g "; } | |
virtual void m() { cout << " B::m "; g(); j(); } | |
void k() { cout << " B::k "; A::n(); } | |
A* n() override { cout << " B::n "; return this; } | |
}; | |
class C : public A { | |
private: | |
void j() { cout << " C::j "; } | |
public: | |
virtual void g() { cout << " C::g "; } | |
void m() { cout << " C::m "; g(); j(); } | |
void k() const { cout << " C::k "; k(); } | |
}; | |
class D : public B { | |
protected: | |
void j() { cout << " D::j "; } | |
public: | |
B* n() final { cout << " D::n "; return this; } | |
void m() { cout << " D::m"; g(); j(); } | |
}; | |
int main() { | |
A* p1 = new D(); | |
A* p2 = new B(); | |
A* p3 = new C(); | |
B* p4 = new D(); | |
const A* p5 = new C(); | |
p1->g(); // B::g | |
cout << endl; | |
p1->k(); // B::k A::n | |
cout << endl; | |
p2->f(); // A::f B::g A::j | |
cout << endl; | |
p2->m(); // A::m B::g A::j | |
cout << endl; | |
p3->k(); // A::k C::j A::m A::g C::j | |
cout << endl; | |
p3->f(); // A::f A::g C::j | |
cout << endl; | |
p4->m(); // D::m B::g D::j | |
cout << endl; | |
p4->k(); // B::k A::n | |
cout << endl; | |
p5->g(); // A::g | |
cout << endl; | |
(p3->n())->m(); // A::n A::m A::g C::j | |
cout << endl; | |
(p3->n())->n()->g(); // A::n A::n A::g | |
cout << endl; | |
(p4->n())->m(); // D::n A::m B::g D::j | |
cout << endl; | |
// (p5->n())->g(); // NON COMPILA | |
(dynamic_cast<B*>(p1))->m(); // D::m B::g D::j | |
cout << endl; | |
// (static_cast<C*>(p2))->k(); // C::k ERRORE RUNTIME | |
(static_cast<B*>(p3->n()))->g(); // A::n A::g | |
getchar(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment