← Classes & Objects

Setter with this

medium

setBalance’s parameter is named balance – the same as the member it’s supposed to update. This is deliberate: it’s the exact situation from the lesson where this-> stops being optional. Write this->balance to mean the member, plain balance to mean the parameter.

If your solution passes the 0 case but fails the others, that’s the tell: acc.balance starts at 0 in main, so a setBalance that accidentally does nothing (via unqualified balance = balance;, a no-op self-assignment) would still happen to print the right answer for input 0, while silently failing for everything else.

Your solution

"Run" uses the sample stdin ("100"). "Submit" checks your code against all 3 test cases.

Test results

Submit your solution to run it against all test cases.

Hints

    Reference solution