adplus-dvertising
frame-decoration

Question

What is the output of this program?
    #include <iostream>
    #include <exception>
    using namespace std;
    class base { virtual void dummy() {} };
    class derived: public base { int a; };
    int main () 
    {
        try 
        {
            base * pba = new derived;
            base * pbb = new base;
            derived * pd;
            pd = dynamic_cast<derived*>(pba);
            if (pd == 0) 
                cout << "Null pointer on first type-cast" << endl;
            pd = dynamic_cast<derived*>(pbb);
            if (pd == 0) 
                cout << "Null pointer on second type-cast" << endl;
        } 
        catch (exception& e) 
        {
            cout << "Exception: " << e.what();
        }
        return 0;
    }

a.

Null pointer on first type-cast

b.

Null pointer on second type-cast

c.

Exception

d.

None of the mentioned

Answer: (b).Null pointer on second type-cast

Engage with the Community - Add Your Comment

Confused About the Answer? Ask for Details Here.

Know the Explanation? Add it Here.

Q. What is the output of this program?