Last active
January 11, 2019 11:11
-
-
Save GladOSkar/267cadc811838f9051c9de0af2a65159 to your computer and use it in GitHub Desktop.
CPP inheritance tests
This file contains 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 Tier { | |
public: | |
int id; | |
virtual ~Tier() = default; | |
Tier(int x) { | |
id = x; | |
} | |
}; | |
class Ente: public Tier { | |
public: | |
virtual ~Ente() = default; | |
Ente(int x) : Tier(x) {} | |
}; | |
class Person: public Tier { | |
public: | |
virtual ~Person() = default; | |
Person(int x) : Tier(x) {} | |
}; | |
class Student: public Person { | |
public: | |
virtual ~Student() = default; | |
Student(int x) : Person(x) {} | |
}; | |
int main() { | |
Tier* dagobertduck = new Ente(1); | |
Tier* tutor = new Student(2); | |
Person* p = new Student(3); | |
Person alumni = *p; | |
cout << alumni.id << endl; | |
Person* tutorprivat = dynamic_cast<Person*>(tutor); | |
cout << tutorprivat << endl; | |
Person* dagobertmensch = dynamic_cast<Person*>(dagobertduck); | |
cout << dagobertmensch << endl; | |
//p = dynamic_cast<Tier*>(tutor); <-- Funktioniert natürlich nicht weil inkompatible typen | |
p = dynamic_cast<Person*>(tutor); | |
cout << p << endl; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment