-
What is the output of this program?
#include <iostream>
#include <exception>
using namespace std;
class BaseClass
{
virtual void dummy() {}
};
class DerivedClass: public BaseClass
{
int num;
};
int main ()
{
try
{
BaseClass * ptr1 = new DerivedClass;
BaseClass * ptr2 = new BaseClass;
DerivedClass * ptr3;
ptr3 = dynamic_cast(ptr1);
if (ptr3 == 0)
cout << "Null pointer on first type-cast" << endl;
ptr3 = dynamic_cast(ptr2);
if (ptr3 == 0)
cout << "Null pointer on second type-cast" << endl;
}
catch (exception& excep)
{
cout << "Exception: " << excep.what();
}
return 0;
}
-
- Null pointer on first type-cast
- Null pointer on second type-cast
- Exception:
- All of above
- None of these
Correct Option: B
In this program, We apply the dynamic cast to ptr3. Based on the value in the ptr3, it produces the output.