MCQs
Static_cast can be applied to only compile-time construct and not during run time construct.
None.
In this program, We applied the dynamic casting to structure and produced the output.
Output:
$ g++ rtti4.cpp
$ a.out
Class C
Class A
In this program, We are using the bad typeid() for a. So it is arising an exception.
Output:
$ g++ rtti3.cpp
$ a.out
object is NULL
In this program, We apply the typeid to the polymorphic class.
Output:
$ g++ rtti2.cpp
$ a.out
4base 7derived
In this program, We are finding the typeid of the given variables.
Output:
$ g++ rtti1.cpp
$ a.out
Pii
The dynamic_cast<> operation and typeid operator in C++ are part of RTTI.
What is the output of this program?
1.
#include
2.
#include
3.
using namespace std;
4.
class base { virtual void dummy() {} };
5.
class derived: public base { int a; };
6.
int main ()
7.
{
8.
try
9.
{
10.
base * pba = new derived;
11.
base * pbb = new base;
12.
derived * pd;
13.
pd = dynamic_cast(pba);
14.
if (pd == 0)
15.
cout
In this program, We apply the dynamic cast to pd. Based on the value in the pd, it produces
the output.
Output:
$ g++ rtti.cpp
$ a.out
Null pointer on second type-cast
RTTI is available only for classes which are polymorphic, which means they have at least one
virtual method.
With the help of RTTI, We can get the information about the data type at the runtime.