Run Time Type Information(C++ Programming ) Questions and Answers
Explanation:-
Answer: Option D. -> object is NULLIn 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
Explanation:-
Answer: Option C. -> 4base and 7derivedIn this program, We apply the typeid to the polymorphic class.
Output:
$ g++ rtti2.cpp
$ a.out
4base 7derived
Explanation:-
Answer: Option C. -> Both a & bIn this program, We are finding the typeid of the given variables.
Output:
$ g++ rtti1.cpp
$ a.out
Pii
Question 8.
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
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
Explanation:-
Answer: Option B. -> Null pointer on second type-castIn 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