Run Time Type Information

Q.

What is the output of this program?

 
1.
    #include 
2.
    using namespace std;
3.
    struct A 
4.
    {
5.
        virtual void f()  
6.
        { 
7.
            cout << "Class A" << endl; 
8.
        }
9.
    };
10.
    struct B : A 
11.
    {
12.
        virtual void f() 
13.
        { 
14.
            cout << "Class B" << endl;
15.
        }
16.
    };
17.
    struct C : A 
18.
    {
19.    
        virtual void f() 
20.
        {
21.
            cout << "Class C" << endl; 
22.
        }
23.
    };
24.
    void f(A* arg) 
25.
    {
26.
        B* bp = dynamic_cast(arg);
27.
        C* cp = dynamic_cast(arg);
28.
        if (bp)
29.
            bp -> f();
30.
        else if (cp)
31.
            cp -> f();
32.
        else
33.
            arg -> f();  
34.
    };
35.
    int main() 
36.
    {
37.
        A aobj;
38.
        C cobj;
39.
        A* ap = &cobj;
40.
        A* ap2 = &aobj;
41.
        f(ap);
42.
        f(ap2);
43.
    }
A. Class C
B. Class A
C. Both a & b
D. None of the mentioned

Answer : Option C

Explanation :

In this program, We applied the dynamic casting to structure and produced the output.
Output:
$ g++ rtti4.cpp
$ a.out
Class C
Class A