Home » C++ Programming » Exception Handling » Question
  1. What is the output of this program?
    #include <iostream>
    #include <typeinfo>
    using namespace std;
    class shapeClass
    {
    public:
    virtual void Virtualfunc() const {}
    };
    class NewTriangle: public shapeClass
    {
    public:
    virtual void Virtualfunc() const
    {
    };
    };
    int main()
    {
    shapeClass shape_object;
    shapeClass &Ref_shape = shape_object;
    try
    {
    NewTriangle &Ref_NewTriangle = dynamic_cast<NewTriangle&>(Ref_shape);
    }
    catch (bad_cast)
    {
    cout << "Exception caught by bad_cast";
    }
    return 0;
    }
    1. Compilation Error
    2. Exception caught by bad_cast
    3. Runtime Error
    4. Garbage Value
    5. None of these
Correct Option: B

As we are not able to allocate the values by using dynamic cast,
So it is arising an exception.



Your comments will be displayed only after manual approval.