adplus-dvertising
frame-decoration

Question

What is the output of this program?
    #include <iostream>
    using namespace std;
    struct A 
    {
        virtual void f()  
        { 
            cout << "Class A" << endl; 
        }
    };
    struct B : A 
    {
        virtual void f() 
        { 
            cout << "Class B" << endl;
        }
    };
    struct C : A 
    {
        virtual void f() 
        {
            cout << "Class C" << endl; 
        }
    };
    void f(A* arg) 
    {
        B* bp = dynamic_cast<B*>(arg);
        C* cp = dynamic_cast<C*>(arg);
        if (bp)
            bp -> f();
        else if (cp)
            cp -> f();
        else
            arg -> f();  
    };
    int main() 
    {
        A aobj;
        C cobj;
        A* ap = &cobj;
        A* ap2 = &aobj;
        f(ap);
        f(ap2);
    }

a.

Class C

b.

Class A

c.

Both Class C & A

d.

None of the mentioned

Answer: (c).Both Class C & A

Engage with the Community - Add Your Comment

Confused About the Answer? Ask for Details Here.

Know the Explanation? Add it Here.

Q. What is the output of this program?