#include <iostream>
using namespace std;
class Temperature
{
public:
//these static functions return a temporary object of type Temperature
static Temperature Fahrenheit (double f);
static Temperature Celsius (double c);
static Temperature Kelvin (double k);
//A static member function can't refer to non-static data.
double getTemperature() { return _temp; }
private:
Temperature (double temp); //Constructor is private.
double _temp;
};
//_temp instantiated outside of Temparature class.
Temperature::Temperature (double temp):_temp (temp) {}
Temperature Temperature::Fahrenheit (double f)
{ return Temperature ((f + 459.67) / 1.8); }
Temperature Temperature::Celsius (double c)
{ return Temperature (c + 273.15); }
Temperature Temperature::Kelvin (double k)
{ return Temperature (k); }
int main() {
// We are initializing the objects created here via kelvin(), celsius() and fahrenheit()
// to objects
// kelvin, celsius and fahrenheit.
Temperature kelvin = Temperature::Kelvin (300.0);
Temperature celsius = Temperature::Celsius (26.85);
Temperature fahrenheit = Temperature::Fahrenheit(80.33);
cout << "Entered kelvin " << kelvin.getTemperature () << endl;
cout << "Entered celsius " << celsius.getTemperature () << endl;
cout << "Entered fahrenheit " << fahrenheit.getTemperature () << endl;
return 0;
}
/*
Output:
Entered kelvin 300
Entered celsius 300
Entered fahrenheit 300
*/