Factory That Falls Back on a Bad Temperature
Factory That Falls Back on a Bad Temperature
A constructor has no return value to report a problem with, so it throws. A factory function is a good place to catch that and hand back something safe instead:
- explicit Temperature(double celsius): a value below -273.15 throws std::out_of_range("below absolute zero"); otherwise the value is stored
- Temperature makeTemperature(double celsius): tries to build a Temperature from the value and returns it; when the constructor throws, it prints invalid reading, using 0 on its own line and returns Temperature(0)
The tests read readings from input, one per line, and print the celsius() of makeTemperature(reading) for each, one per line. For the first test the input is 21.5, -300, -273.15.
Do NOT write a main function.
Expected Output:
21.5 invalid reading, using 0 0 -273.15
= newline. The output ends with a newline — print one after the last line.
Your Output
Run your code to see the output here...