ProgrammingHardProduct-company placement · C++

What happens when this is compiled?

What happens when this is compiled?
struct P { int a; private: int b; };
int main() { P p{1, 2}; }
  1. A. It compiles; a = 1 and b = 2
  2. B. It compiles; b is ignored
  3. C. It compiles with b default-initialised
  4. D. Compile-time error: P is not an aggregate and has no matching constructor

Correct answer: D. Compile-time error: P is not an aggregate and has no matching constructor

A class with a private non-static data member is not an aggregate, so brace initialisation must call a constructor, and P only has the implicit default one.

More OOP with C++ and Java questions

Exams that ask this topic