Question discussion

What is the output of the following C++ program?#include using namespace std; struct A { A() { cout

1506Medium DSSSB TGT Computer Science 0 replies 1 views
0

What is the output of the following C++ program?

#include <iostream>
using namespace std;
struct A { A() { cout << "A"; } };
struct B : virtual A { B() { cout << "B"; } };
struct C : virtual A { C() { cout << "C"; } };
struct D : B, C { D() { cout << "D"; } };
int main() { D d; }
  • AABACD
  • BACBD
  • CABCD
  • DABCAD
Correct answer: C

Explanation

With virtual inheritance the single shared A sub-object is constructed first by the most derived class, then B, C and D in order: ABCD.

Discussion 0

Nobody has replied yet. Start the discussion!