c++/dcast.cpp
author Markus Bröker <mbroeker@largo.dyndns.tv>
Fri, 27 Dec 2013 16:00:09 +0100
changeset 167 7c6b221900bf
parent 165 f551b78c3eee
permissions -rw-r--r--
Proper Datatypes to leave the 80's
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
165
f551b78c3eee a bluetooth and a c++ demo
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
     1
#include <typeinfo>
f551b78c3eee a bluetooth and a c++ demo
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
     2
#include <iostream>
f551b78c3eee a bluetooth and a c++ demo
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
     3
f551b78c3eee a bluetooth and a c++ demo
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
     4
using namespace std;
f551b78c3eee a bluetooth and a c++ demo
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
     5
f551b78c3eee a bluetooth and a c++ demo
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
     6
class A {
f551b78c3eee a bluetooth and a c++ demo
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
     7
  public:
f551b78c3eee a bluetooth and a c++ demo
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
     8
    void f () {
f551b78c3eee a bluetooth and a c++ demo
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
     9
        cout << "Ich bin f() in der Klasse A\n";
f551b78c3eee a bluetooth and a c++ demo
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    10
    }
f551b78c3eee a bluetooth and a c++ demo
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    11
};
f551b78c3eee a bluetooth and a c++ demo
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    12
f551b78c3eee a bluetooth and a c++ demo
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    13
class B:public A {
f551b78c3eee a bluetooth and a c++ demo
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    14
  public:
f551b78c3eee a bluetooth and a c++ demo
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    15
    void f () {
f551b78c3eee a bluetooth and a c++ demo
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    16
        cout << "Ich bin f() in der Klasse B\n";
f551b78c3eee a bluetooth and a c++ demo
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    17
    }
f551b78c3eee a bluetooth and a c++ demo
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    18
};
f551b78c3eee a bluetooth and a c++ demo
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    19
f551b78c3eee a bluetooth and a c++ demo
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    20
class C {
f551b78c3eee a bluetooth and a c++ demo
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    21
  public:
f551b78c3eee a bluetooth and a c++ demo
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    22
    void f () {
f551b78c3eee a bluetooth and a c++ demo
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    23
        cout << "Ich bin auch ein f()\n";
f551b78c3eee a bluetooth and a c++ demo
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    24
    }
f551b78c3eee a bluetooth and a c++ demo
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    25
};
f551b78c3eee a bluetooth and a c++ demo
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    26
f551b78c3eee a bluetooth and a c++ demo
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    27
int main ()
f551b78c3eee a bluetooth and a c++ demo
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    28
{
f551b78c3eee a bluetooth and a c++ demo
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    29
    A *ap = new A ();
f551b78c3eee a bluetooth and a c++ demo
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    30
    B *bp = new B ();
f551b78c3eee a bluetooth and a c++ demo
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    31
f551b78c3eee a bluetooth and a c++ demo
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    32
    // instanceof
f551b78c3eee a bluetooth and a c++ demo
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    33
    C *cp = dynamic_cast < A * >(bp);
f551b78c3eee a bluetooth and a c++ demo
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    34
    if (cp != NULL)
f551b78c3eee a bluetooth and a c++ demo
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    35
        cp->f ();
f551b78c3eee a bluetooth and a c++ demo
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    36
f551b78c3eee a bluetooth and a c++ demo
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    37
    return 0;
f551b78c3eee a bluetooth and a c++ demo
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    38
}