TTI
// A pen exam
// Evaluate the knowledge of rtti and identify runtime types
// You also need to enable the rtti function of the VC compiler.
// Select C ++ language for project/settings/C ++/catogory and you will see that option.
# Include < Iostream >
# Include < Typeinfo >
Using Namespace STD;
Class Base
{
Public :
Virtual Void Funca () {cout < " Base " < Endl ;}
};
Class Derived: Public Base
{
Public :
Virtual Void Funcb () {cout < " Derived " < Endl ;}
};
// Requires that func be able to output the actual type of the form parameter. either of the following func can
//Funcc uses the dynamic_cast type conversion to identify the type.
// Dynamic_cast must be performed in hierarchy with virtual functions.
Void Funcc ( Base * P)
{
Derived * DP = Dynamic_cast < Derived *> (P );
If (DP ! = Null)
DP -> Funcb ();
Else
P -> Funca ();
}
// Funcd uses the typeid Operator
Void Funcd ( Base * P)
{
Derived * DP = NULL;
If (Typeid ( * P) = Typeid (derived ))
{
DP = Static_cast < Derived *> (P );
DP -> Funcb ();
}
Else
P -> Funca ();
}
Int Main ()
{
Base * CP = New Derived;
Funcd (CP );
Funcc (CP );
Base * DP = New Base ;
Funcc (DP );
Funcd (DP );
Return 0 ;
}