#include using namespace std ; class A { public: int x1 ; virtual void function1() { } }; class B : public A { public: int x1B ; }; class C : public A { public: int x1C ; }; class D { }; class Person { public: virtual void function1() { } }; class Employee : public Person { }; class Student : public Person { }; int main() { A* aObject = new B() ; //Should not be allowed. D* dObject = (D*)aObject ; dObject = dynamic_cast (aObject) ; if ( dObject == NULL ) cout << "Invalid cast from A to D." << endl ; else cout << "Valid cast from A to D." << endl ; //Should not be allowed. C* cObject = (C*)aObject ; cObject = dynamic_cast (aObject) ; if ( cObject == NULL ) cout << "Invalid cast from A to C." << endl ; else cout << "Valid cast from A to C." << endl ; B* bObject = (B*)aObject ; bObject = dynamic_cast (aObject) ; if ( bObject == NULL ) cout << "Invalid cast from A to B." << endl ; else cout << "Valid cast from A to B." << endl ; Person* personObject = new Employee() ; Student* studentObject = (Student*)personObject ; studentObject = dynamic_cast (personObject) ; if ( studentObject == NULL ) cout << "Invalid cast from Person to Student." << endl ; else cout << "Valid cast from Person to Student." << endl ; }