Sail E0 Webinar
Question


What is the output of this program?


1.
#include
2.
#include
3.
using namespace std;
4.
class Myshape
5.
{
6.
public:
7.
virtual void myvirtualfunc() const {}
8.
};
9.
class mytriangle: public Myshape
10.
{
11.
public:
12.
virtual void myvirtualfunc() const
13.
{
14.
};
15.
};
16.
int main()
17.
{
18.
Myshape Myshape_instance;
19.
Myshape &ref_Myshape = Myshape_instance;
20.
try
21.
{
22.
mytriangle &ref_mytriangle = dynamic_cast(ref_Myshape);
23.
}
24.
catch (bad_cast)
25.
{
26.
cout << "Can't do the dynamic_cast lor!!!" << endl;
27.
cout << "Caught: bad_cast exception. Myshape is not mytriangle.n";
28.
}
29.
return 0;
30.
}
Options:
A .  Can't do the dynamic_cast lor!!!
B .  Caught: bad_cast exception. Myshape is not mytriangle.
C .  both a & b
D .  none of the mentioned
Answer: Option C

As we can't able to create the dynamic instance for the triangle, So it is arising an exception.
Output:
$ g++ exs3.cpp
$ a.out
Can't do the dynamic_cast lor!!!
Caught: bad_cast exception. Myshape is not mytriangle.



Was this answer helpful ?
Next Question

Submit Solution

Your email address will not be published. Required fields are marked *

Latest Videos

Latest Test Papers