Question discussion

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

1316Medium 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 << 1; } ~A() { cout << 3; } };
struct B : A { B() { cout << 2; } ~B() { cout << 4; } };
int main() { B x; }
  • A1342
  • B1234
  • C2143
  • D1243
Correct answer: D

Explanation

The base constructor runs before the derived constructor (1, 2); destructors run in the reverse order, derived first (4) and then base (3).

Discussion 0

Nobody has replied yet. Start the discussion!