/****************************************************************************** * This program tests the Temperature class. * * Copyright © 2021 Richard Lesh. All rights reserved. *****************************************************************************/ #undef NDEBUG #include #include #include #include using namespace std; class Temperature { private: // Internal representation for Temperature is in Celsius double temperature_in_celsius = 0.0; static constexpr double CELSIUS_TO_KELVIN = 273.15; static constexpr double FAHRENHEIT_TO_RANKINE = 459.67; static constexpr double KELVIN_TO_RANKINE = 1.8; public: // Constructor Temperature() noexcept { temperature_in_celsius = -CELSIUS_TO_KELVIN; } // Celsius getter double get_celsius() noexcept { return temperature_in_celsius; } // Celsius setter void set_celsius(double temperature_in_celsius) noexcept { this->temperature_in_celsius = temperature_in_celsius; } // Kelvin getter double get_kelvin() noexcept { return get_celsius() + CELSIUS_TO_KELVIN; } // Kelvin setter void set_kelvin(double k) noexcept { set_celsius(k - Temperature::CELSIUS_TO_KELVIN); } // Rankine getter double get_rankine() noexcept { return get_kelvin() * Temperature::KELVIN_TO_RANKINE; } // Rankine setter void set_rankine(double r) noexcept { set_kelvin(r / KELVIN_TO_RANKINE); } // Fahrenheit getter double get_fahrenheit() noexcept { return this->get_rankine() - FAHRENHEIT_TO_RANKINE; } // Fahrenheit setter void set_fahrenheit(double f) noexcept { set_rankine(f + Temperature::FAHRENHEIT_TO_RANKINE); } }; int main(int argc, char **argv) { shared_ptr t(new Temperature()); cout << fmt::format("Celcius: {0:.2f}", t->get_celsius()) << endl; cout << fmt::format("Kelvin: {0:.2f}", t->get_kelvin()) << endl; cout << fmt::format("Fahrenheit: {0:.2f}", t->get_fahrenheit()) << endl; cout << fmt::format("Rankine: {0:.2f}", t->get_rankine()) << endl; cout << endl; t->set_celsius(100.); cout << fmt::format("Celcius: {0:.2f}", t->get_celsius()) << endl; cout << fmt::format("Kelvin: {0:.2f}", t->get_kelvin()) << endl; cout << fmt::format("Fahrenheit: {0:.2f}", t->get_fahrenheit()) << endl; cout << fmt::format("Rankine: {0:.2f}", t->get_rankine()) << endl; cout << endl; t->set_fahrenheit(-40.); cout << fmt::format("Celcius: {0:.2f}", t->get_celsius()) << endl; cout << fmt::format("Kelvin: {0:.2f}", t->get_kelvin()) << endl; cout << fmt::format("Fahrenheit: {0:.2f}", t->get_fahrenheit()) << endl; cout << fmt::format("Rankine: {0:.2f}", t->get_rankine()) << endl; cout << endl; t->set_kelvin(373.15); cout << fmt::format("Celcius: {0:.2f}", t->get_celsius()) << endl; cout << fmt::format("Kelvin: {0:.2f}", t->get_kelvin()) << endl; cout << fmt::format("Fahrenheit: {0:.2f}", t->get_fahrenheit()) << endl; cout << fmt::format("Rankine: {0:.2f}", t->get_rankine()) << endl; cout << endl; t->set_rankine(671.67); cout << fmt::format("Celcius: {0:.2f}", t->get_celsius()) << endl; cout << fmt::format("Kelvin: {0:.2f}", t->get_kelvin()) << endl; cout << fmt::format("Fahrenheit: {0:.2f}", t->get_fahrenheit()) << endl; cout << fmt::format("Rankine: {0:.2f}", t->get_rankine()) << endl; cout << endl; return 0; }