Create a temperature class that internally stores a temperature in degrees Kelvin. However, create functions named setTempKelvin, setTempFahrenheit, setTempCelcius that takes input temperature in the specified scale, converts the temperature to kelvin, and stores that temperature in the class member variable. Also, create functions that return the stored temperature in degrees Kelvin, Fahrenheit, or Celcius.

Kelvin = Celcius + 273.15
Celcius = (5/9)*(fahrenheit - 32)

not sure if I'm on the right track with this or not ..... would appreciate any input .....

here is the code I have so far....

[

#include <iostream>
#include <cmath>

using namespace std;
//Class to implement Temperature object
class Temperature
{
private:
double KelvinTemperature;//instance variable
public:
int Temp();
double getCelsius();
double getFahrenheit();
void setTempCelsius(double Temperature);
void setTempFahrenheit(double Temperature);
void setTempKelvin(double Temperature);
};

//Default constructor
Temp::Temp()
{
CelciusTemperature = 0;
}
//Accessor to get Celsius temperature
double Temperature::getCelsius()
{
return CelsiusTemperature;
}
//Accessor to get Fahrenheit temperature
double Temperature::getFahrenheit()
{
return 9.0*CelsiusTemperature/5.0 + 32.0;
}

{

KelvinTemperature = 0;
}
//Accessor to get Kelvin temperature
double Temperature::getKelvin()
(
return CelsiusTemperature + 273.15
}

//Mutator to set Celcius temperature
void Temperature::setTempCelsius(double Temperature)
{
CelsiusTemperature = Temperature;
}

//Mutator to set Fahrenheit temperature
void Temperature::setTempFahrenheit(double Temperature)
{
CelsiusTemperature = (Temperature - 32.0 ) * 5.0 / 9.0 ;
}

//Mutator to set Kelvin temperature
void Temperature::setTempKelvin(double Temperature)
{
KelvinTemperature = (Temperature + 273.15);

int main()
{
//Test temperature conversion
double Celsius;
double Fahrenheit;
double Kelvin;

return 0;
}

/]

Dani AI

Generated

Good start, . As noted, the main issue is consistency: pick one internal representation (Kelvin) and derive the others from it. Also be careful with names and spelling: the class is Temperature, so the constructor is Temperature::Temperature(), not Temp::Temp(). And if you store Kelvin, do not also store Celsius; that invites bugs. Lastly, use 9.0/5.0 (not 9/5) to avoid integer division.

Here is a minimal pattern that keeps only Kelvin internally and converts on the fly. Notice how the Kelvin setter just stores the value; only the Celsius/Fahrenheit setters convert. I also clamp below absolute zero to catch mistakes.

#include <stdexcept>

class Temperature {
    double kelvin_;

    static double checkKelvin(double k) {
        if (k < 0.0) throw std::out_of_range("temperature below absolute zero");
        return k;
    }

public:
    Temperature() : kelvin_(273.15) {}                 // default: 0 C
    explicit Temperature(double kelvin) : kelvin_(checkKelvin(kelvin)) {}

    // setters
    void setTempKelvin(double k)      { kelvin_ = checkKelvin(k); }
    void setTempCelsius(double c)     { kelvin_ = c + 273.15; }
    void setTempFahrenheit(double f)  { kelvin_ = (f - 32.0) * 5.0/9.0 + 273.15; }

    // getters
    double getKelvin() const          { return kelvin_; }
    double getCelsius() const         { return kelvin_ - 273.15; }
    double getFahrenheit() const      { return (kelvin_ - 273.15) * 9.0/5.0 + 32.0; }
};

Quick checklist for your current code:

  • Make the constructor Temperature::Temperature() and remove int Temp();.
  • Use one member like double kelvin_; and delete CelsiusTemperature.
  • Fix the Kelvin setter: do not add 273.15 when the input is already Kelvin.
  • Add const to getters, and prefer clear, consistent names.
  • Compile with warnings (-Wall -Wextra) to catch mismatched names and missing braces.

You should figure out how to use [code=syntax]...code tags... [/code] so that people can read your code.

It's hard to tell, but it appears that your constructors are definitely not right. Also, how do you plan to get Celsius from Kelvin if you only return CelsiusTemperature; ? From what I can tell CelsiusTemperature doesn't exist. In addition, last I checked Kelvin and Celsius are not the same...

Some reading would be suggested.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.