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

class A {
    virtual void foo(){ cout<<"A"; }

class B: public virtual A {
    virtual void foo(){ cout<<"B"; }

class C: public virtual A {};
class D: public B, public C {};

int main(void) {
    D d;
    C &c = d;
    return 0;
"B" is printed out, as classes B and C have virtual inheritance and therefore, class D will have only one instance of class A and, correspondignly, one pointer to the virtual function table, where B::foo() is specified.

Follow CodeGalaxy

Mobile Beta

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