-
What is the output of this program?
#include <iostream>
using namespace std;
struct N
{
virtual void fun()
{
cout << "Class N" << endl;
}
};
struct M : N
{
virtual void fun()
{
cout << "Class M" << endl;
}
};
struct O : N
{
virtual void fun()
{
cout << "Class O" << endl;
}
};
void fun(N* argumnet)
{
M* ptr1 = dynamic_cast<M*>(argumnet);
O* ptr2 = dynamic_cast<O*>(argumnet);
if (ptr1)
ptr1 -> fun();
else if (ptr2)
ptr2 -> fun();
else
argumnet -> fun();
};
int main()
{
N Object1;
O Object2;
N* ptr1 = &Object2;
N* ptr3 = &Object1;
fun(ptr1);
fun(ptr3);
}
-
- Class N
Class O - Class M
Class N - Class M
Class N - Class M
Class O - Class O
Class N
- Class N
Correct Option: E
In this program, We applied the dynamic casting to structure and produced the output.