I am curious if it would ever be in good practice to omit constructor definitions from a class. Here, the intention of the Temperature class is to simply convert between one temperature scale to another. Since there are three scale measurements, kelvin, Fahrenheit, and Celsius, I decided it was best to define specific functions for the desired conversion. All of the functions take a single argument of type double. This leaves me to think it's best to delete the constructors I defined. Also, any other feedback is welcomed too.
/*
Create a Temperature class that returns a conversion to a user
selected scale
Kelvin = Celsius + 273.15
Celsius = (5.0/9) * (Fahrenheit - 32)
*/
#include <iostream>
class Temperature
{
public:
//default constructor
Temperature() : kelvin(NULL), fahrenheit(NULL), celsius(NULL)
{/*body intentionally left blank*/}
//constructor: Kelvin, Fahrenheit, Celsius
Temperature(double kel, double fahr, double cel)
: kelvin(kel), fahrenheit(fahr), celsius(cel)
{/*body intentionally left blank*/}
double kelToCel(double);
double kelToFahr(double);
double celToKel(double);
double celToFahr(double);
double fahrToCel(double);
double fahrToKel(double);
private:
double kelvin = 0;
double fahrenheit = 0;
double celsius = 0;
};
double Temperature::kelToCel(double kel)
{
kelvin = kel;
return celsius = kel - 273.15;
}
double Temperature::kelToFahr(double kel)
{
kelvin = kel;
return fahrenheit = (9.0 / 5) * (kel - 273.15) + 32;
}
double Temperature::celToKel(double cel)
{
celsius = cel;
return kelvin = cel + 273.15;
}
double Temperature::celToFahr(double cel)
{
celsius = cel;
return fahrenheit = cel * (9.0 / 5) + 32;
}
double Temperature::fahrToCel(double fahr)
{
fahrenheit = fahr;
return celsius = (5.0 / 9) * (fahr - 32);
}
double Temperature::fahrToKel(double fahr)
{
fahrenheit = fahr;
return kelvin = (5.0 / 9) * (fahr - 32) + 273.15;
}
int main()
{
std::cout << "This program converts between temperature scales.\n\n"
<< "1: Kelvin to Celsius 3: Fahrenheit to Kelvin 5:Celsius to Fahrenheit\n"
<< "2: Kelvin to Fahrenheit 4: Fahrenheit to Celsius 6: Celsius to Kelvin\n\n"
<< "Enter the number of your selection: ";
short selection;
std::cin >> selection;
Temperature temp;
double degrees, count = 0;
switch (selection)
{
// input: Kelvin, output: Celsius
case 1:
std::cout << "Enter degrees in Kelvin: ";
std::cin >> degrees;
std::cout << temp.kelToCel(degrees) << " Celsius.\n";
break;
// input Kelvin, output: Fahrenheit
case 2:
std::cout << "Enter degrees in Kelvin: ";
std::cin >> degrees;
std::cout << temp.kelToFahr(degrees) << " Fahrenheit.\n";
break;
// input: Fahrenheit, output: Kelvin
case 3:
std::cout << "Enter degrees in Fahrenheit: ";
std::cin >> degrees;
std::cout << temp.fahrToKel(degrees) << " Kelvin.\n";
break;
// input: Fahrenheit, output Celsius
case 4:
std::cout << "Enter degrees in Fahrenheit: ";
std::cin >> degrees;
std::cout << temp.fahrToCel(degrees) << " Celsius.\n";
break;
// input: Celsius, output: Fahrenheit
case 5:
std::cout << "Enter degrees in Celsius: ";
std::cin >> degrees;
std::cout << temp.celToFahr(degrees) << " Fahrenheit.\n";
break;
// input: Celsius, output Kelvin
case 6:
std::cout << "Enter degrees in Celsius: ";
std::cin >> degrees;
std::cout << temp.celToKel(degrees) << " Kelvin\n";
break;
}
}
Subtracting 273.16 K from the temperature of the triple point of water (0.01 °C) makes absolute zero (0 K) equivalent to −273.15 °C.
It's −273.15 °C. \$\endgroup\$