What will be displayed on the console as a result of the program execution?
#include <iostream>

class A {
    char (*provider)();

public:
    A(char (*p)()) : provider(p) {}
    char Get() { return provider(); }
};

char foo1() { return '1'; }
char foo2() { return '2'; }

int main(int argc, char* argv[])
{    
    A x(foo1);
    A y(foo2);

    std::cout << x.Get() << y.Get();

    return 0;
}
Explanation
The Get() method for calculating the return value calls the function stored in the provider.
Thus, the behavior of the Get() method for each particular object of the A type depends entirely on the constructor parameter.

Follow CodeGalaxy

Mobile Beta

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