What will be the output of the following C++ code?
#include <iostream>
using namespace std;
struct Data {
private:
int value = 7;
public:
int getValue() { return value; }
};
int main() {
Data d;
cout << d.getValue();
return 0;
}
Options
-
A.
Compile-time error because struct cannot have private members
-
B.
7
-
D.
More than one of the above
-
E.
None of the above
Correct Answer (Detailed Explanation is Below)
B.
7
Detailed Explanation
Correct Answer: Option B — 7Explanation: A struct can have private members when the private access specifier is used. The private value member is accessed inside the public getValue() method, which returns 7 to main().