#include<iostream>#include<string>#include<vector>#include<map>usingnamespace std;classB{public:voidmethod(){ cout <<"from B"<<endl;}virtualvoidprint(){ cout <<"print from B";}};classA:public B
{public:voidmethod(){ cout <<"from A"<< endl;}voidprint(){ cout <<"print from A"<<endl;}};intmain(int argn,char** args){
B b;
b.method();
A a;
a.method();
A* ap =newA();
ap->method();auto bp =dynamic_cast<B*>(ap);
bp->method();// if B hasn't a virtual method,below cast cannot success auto app =dynamic_cast<A*>(bp);
app->print();auto bpp =newB();//this below cast will not success, the "appp" will be nullptrauto appp =dynamic_cast<A*>(bpp);
appp->print();// when use dynamic_cast<A*> .success return A* .failure return nullptr// when use dynamic_cast<A&> .success return A& .failuer will throw a "bad_cast" exceptionreturn0;}