Home » C++ Programming » Questions and Answers » Question
  1. 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;
    }
    1. Null pointer on first type-cast
    2. Null pointer on second type-cast
    3. Exception:
    4. All of above
    5. 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.



Your comments will be displayed only after manual approval.