What will be printed to the console as a result of the following program?
#include <iostream>

struct A {
    char foo() { return 'A';}

template<class T> struct B : public T {
    virtual char foo() {return 'B';}

template<class T> struct C : public T {
    virtual char foo() {return 'C';}

int main(int argc, char* argv[])
    A* a = new A();
    A* b = new B< A >();
    A* c = new C< A >();
    A* d = new B< C< A > >();

    std::cout << a->foo() << b->foo() << c->foo() << d->foo();

    return 0;
Template parameters B and C determine the parent of the generated class.
Since the foo() method of the base class (class A) is not virtual, in this case, later binding will not be used and as a result in all cases the foo() method of class A will be called.

Follow CodeGalaxy

Mobile Beta

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