What will be the output of the following C++ code?
#include <iostream>
using namespace std;
class Base
{
public:
int x = 10;
};
class Derived : public Base
{
};
int main()
{
Derived d;
cout << d.x;
return 0;
}
Options
-
B.
10
-
C.
Compilation error
-
D.
More than one of the above
-
E.
None of the above
Correct Answer (Detailed Explanation is Below)
B.
10
Detailed Explanation
Correct Answer: Option BExplanation:Because
x is public in the base class and inheritance is public, the derived object can access it directly. The output is
10.