← Classes & Objects

Fix: Private by Default

hard

This Counter class doesn’t compile. Submit it as-is and read the errors before changing anything – you’ll see two different members, increment and count, each reported as is private within this context, right at the lines in main that try to use them.

Neither member has an explicit private: label anywhere in the class – and that’s exactly the bug. class defaults to private access for everything above the first access-specifier label, unlike struct, which defaults to public. Since main needs to call c.increment() and read c.count from outside the class, both members need to be reachable from there. Add a public: label at the top of the class body, before both members, to fix it – don’t change struct/class itself, and don’t change any of the logic.

Your solution

"Run" uses the sample stdin (none). "Submit" checks your code against all 1 test case.

Test results

Submit your solution to run it against all test cases.

Reference solution