Examveda

What will be the output of the following C++ code?
#include <typeinfo>
#include <iostream>
using namespace std;
class Myshape
{
    public:
    virtual void myvirtualfunc() const {}
};
class mytriangle: public Myshape
{
    public:
    virtual void myvirtualfunc() const
    {   
    };
};
int main()
{
    Myshape Myshape_instance;
    Myshape &ref_Myshape = Myshape_instance;
    try 
    {
        mytriangle &ref_mytriangle = dynamic_cast<mytriangle&>(ref_Myshape);
    }
    catch (bad_cast)
    {
        cout << "Can't do the dynamic_cast lor!!!" << endl;
        cout << "Caught: bad_cast exception. Myshape is not mytriangle.\n";
    }
    return 0;
}

A. Can't do the dynamic_cast lor!!!

B. Caught: bad_cast exception. Myshape is not mytriangle.

C. Can't able to create the dynamic instance for the triangle, So it is arising an exception

D. Myshape is not mytriangle

Answer: Option C


Join The Discussion

Related Questions on C plus plus miscellaneous

What is the difference between '++i' and 'i++' in C++?

A. None of the above

B. They both have the same effect

C. '++i' increments the value of 'i' before returning it, while 'i++' increments the value of 'i' after returning it

D. '++i' increments the value of 'i' after returning it, while 'i++' increments the value of 'i' before returning it