What is the output of the following C++ code?
#include <iostream>
using namespace std;
class Base
{
public:
Base() { cout << "B"; }
};
class Derived : public Base
{
public:
Derived() { cout << "D"; }
};
int main()
{
Derived obj;
return 0;
}
Options
-
A.
DB
-
B.
BD
-
C.
B only
-
D.
More than one of the above
-
E.
None of the above
Correct Answer (Detailed Explanation is Below)
B.
BD
Detailed Explanation
Correct Answer: Option BExplanation:The base-class constructor executes before the derived-class constructor. Therefore,
Base() prints B first, followed by
Derived() printing D. The output is
BD.