Question discussion

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

945Medium DSSSB TGT Computer Science 0 replies 1 views
0

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

#include <iostream>
using namespace std;
class A { public: A() { cout << "A"; } ~A() { cout << "a"; } };
class B : public A { public: B() { cout << "B"; } ~B() { cout << "b"; } };
int main() { B obj; return 0; }
  • AABab
  • BBAba
  • CABba
  • DAaBb
Correct answer: C

Explanation

Constructors run base-first (A, B) and destructors in reverse order (derived b, then base a): ABba.

Discussion 0

Nobody has replied yet. Start the discussion!