Home » C++ Programming » Questions and Answers » Question
  1. 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);
    }
    1. Class N
      Class O
    2. Class M
      Class N
    3. Class M
      Class N
    4. Class M
      Class O
    5. Class O
      Class N
Correct Option: E

In this program, We applied the dynamic casting to structure and produced the output.



Your comments will be displayed only after manual approval.