What will happen when the following C++ code is compiled?
#include <iostream>
using namespace std;
class Base {
public:
int value = 10;
};
class Derived : Base {};
int main() {
Derived d;
cout << d.value;
return 0;
}
Options
-
A.
It prints 10
-
B.
It prints 0
-
C.
A compile-time access error occurs
-
D.
More than one of the above
-
E.
None of the above
Correct Answer (Detailed Explanation is Below)
C.
A compile-time access error occurs
Detailed Explanation
Correct Answer: Option CExplanation: Class inheritance is private by default. Base::value is public in Base, but private inheritance makes it privately accessible as an inherited member of Derived. Therefore, main() cannot access d.value directly.