add Chapter01 examples

This commit is contained in:
Amar Mahmutbegovic
2024-04-27 20:43:40 +02:00
parent fb4983465f
commit de6d74e093
12 changed files with 435 additions and 0 deletions

29
Chapter01/rtti.cpp Normal file
View File

@@ -0,0 +1,29 @@
#include <cstdio>
struct Base {
virtual void print() {
printf("Base\r\n");
}
};
struct Derived : public Base {
void print() override {
printf("Derived\r\n");
}
};
void printer(Base &base) {
base.print();
if(Derived *derived = dynamic_cast<Derived*>(&base); derived!=nullptr) {
printf("We found Base using RTTI!\r\n");
}
}
int main() {
Base base;
Derived derived;
printer(base);
printer(derived);
return 0;
}