Question 4
Back to Exercise
The program below does not compile. Which statement correctly describes the problem?
#include <iostream>
class Thermostat {
int target_ = 22;
public:
void set_target(int t) { if (t >= 16 && t <= 30) target_ = t; }
int target() const { return target_; }
};
void show(const Thermostat& t) {
t.set_target(24);
std::cout << t.target() << '\n';
}
int main() {
Thermostat t;
show(t);
return 0;
}
- A target_ must be public
- B show receives a const reference, so only const member functions can be called; set_target modifies the object and is not const Correct
- C set_target must return bool
- D show must be a friend
Correct Answer: Option B
The const parameter is a promise not to change the thermostat – exactly what const-correctness is for.
Discussion & Comments 0
Share your method, a shortcut, or ask about a step you didn't follow.
No comments yet. Be the first to share your solution!