CJCoding With Joseph
15free / daySign inCreate account

A Thermostat That Refuses Bad Settings

Complete the class Thermostat. Its constructor Thermostat(int celsius) checks the value first: when it is below 5 or above 30 it throws a std::out_of_range with the message Target must be between 5 and 30; otherwise it stores the value and prints Thermostat set to followed by it. Add int getTarget() const returning the stored value. A constructor that throws never finishes, so the object does not exist afterwards: no destructor runs for it and the code after the failed declaration is skipped straight to the catch.

The tests create a few thermostats inside one try, printing Target: and the value after each successful one, print Not created: and the message when one fails, and print End after the handler.

Do NOT write a main function.

Expected Output:

Thermostat set to 21
Target: 21
Not created: Target must be between 5 and 30
End

= newline. The output ends with a newline — print one after the last line.

Hints4 available
Topics:
Exceptions
1
Tab to indent (Esc then Tab to move on) · Ctrl+Enter to run · Shift+Alt+F to format · Ctrl+Space to expand shortcuts (cout, fori)

Your Output

Run your code to see the output here...

Test Cases

Run your code to see test case results.