For given class hierarchy in C++, what will be the output of the following code?

#include  <iostream> 
using namespace  std;
class  A
{
public:
    virtual void  Foo(int  n = 10)
    {
        cout << "A::Foo, n = " << n << endl;
    }
};
class  B : public  A{
public:
    virtual void  Foo(int  n = 20) {
        cout << "B::Foo, n = " << n << endl;
    }
};

int  main()
{
    A * pa = new B();
    pa->Foo();
    return  0;
}
Explanation
According to the C++ standard, input function parameters are defined by the static pointer (or reference) type of the object on which the function call is performed.
ISO/IEC 14882: Programming Language C++, §8.3.6, p10

Следи за CodeGalaxy

Мобильное приложение Beta

Get it on Google Play
Обратная Связь
Cosmo
Зарегистрируйся сейчас
или Подпишись на будущие тесты