What will be printed out as a result of the following code execution?
#include <iostream>
using namespace std;

class A{
public:    
    int foo(){ return 1; }
};
class B: public A{
public:    
    virtual int foo(){ return 2; }
};
class C: public B{
public:    
    int foo(){ return 3; }
};

int main() { 
    A* obj = new C;
    
    cout << obj->foo();
    cout << ((B*)obj)->foo();
    cout << ((C*)obj)->foo();
   
    return 0; 
}

Follow CodeGalaxy

Mobile Beta

Get it on Google Play
Send Feedback
Cosmo
Sign Up Now
or Subscribe for future quizzes