Question: #include using namespace std; / / Program to demonstrate Temperature class / / - - - - - - - - - - - -

#include
using namespace std;
// Program to demonstrate Temperature class
//-----------------------------------------
int main()
{
Temperature freezing;
Temperature boiling;
freezing.setCelsius(0);
boiling.setCelsius(100);
cout << "freezing: "<< freezing.getFahrenheit()<<"F
";
cout << "boiling: "<< boiling.getFahrenheit()<<"F
";
return 0;
}
Step 1: Copy this program into your C++ program editor, and compile it. You should see several error messages complaining that "Temperature was not declared in this scope". This is because this is a user defined class, and we have not included the declaration or implementation.
Step 2: In order to make the compiler errors go away, you need to copy/paste the following class interface just above the main program. As you can see, the interface for the Temperature class has two parts: the public methods (visible to users of this class) and the private attributes (hidden from users of this class). Compile your program. Now you should get a long list of "undefined reference" error messages. This is because we have not implemented the Temperature class methods.
// Interface for Temperature class
//--------------------------------
const double ABSOLUTE_ZERO =-273.15;
class Temperature
{
public:
Temperature();
Temperature(const Temperature & Temp);
~Temperature();
double getCelsius() const;
double getFahrenheit() const;
void setCelsius(double Temp);
void setFahrenheit(double Temp);
private:
double CelsiusTemperature;
};
Step 3: To complete your program, you need to copy/paste the following code between the class interface and the main program. The first three methods are the "constructors" for the class that are automatically called when a user declares a Temperature object. The next four methods are used to store and access temperatures in either Celsius or Fahrenheit. Notice that all method names in the implementation section begin with "Temperature::". This tells the C++ compiler that these methods are part of the Temperature class.
// Implementation of Temperature class
//-----------------------------------
Temperature::Temperature()
{
CelsiusTemperature =0;
}
Temperature::Temperature(const Temperature & Temp)
{
CelsiusTemperature = Temp.CelsiusTemperature;
}
Temperature::~Temperature()
{
}
double Temperature::getCelsius() const
{
return CelsiusTemperature;
}
double Temperature::getFahrenheit() const
{
return 9.0* CelsiusTemperature /5.0+32.0;
}
void Temperature::setCelsius(double Temp)
{
CelsiusTemperature = Temp;
if (CelsiusTemperature < ABSOLUTE_ZERO)
CelsiusTemperature = ABSOLUTE_ZERO;
}
void Temperature::setFahrenheit(double Temp)
{
CelsiusTemperature =(Temp -32.0)*5.0/9.0 ;
if (CelsiusTemperature < ABSOLUTE_ZERO)
CelsiusTemperature = ABSOLUTE_ZERO;
}
Step 4: Compile your program again. Hopefully you will not get any error messages. Run the program to see what is printed out.
Step 5: To see what is happening, you need to "hand trace" the code. When we declare "Temperature freezing;" in the main program, we are creating an object of the Temperature class. Then when we call "freezing.setCelsius(100);" the value of the parameter (100) is stored in the private variable "CelsiusTemperature". This variable is global to all of the Temperature methods, but hidden from the main program. If you look at the implementation of "getFahrenheit" you will see that this method uses the value of "CelsiusTemperature" to calculate and return the corresponding temperature in Fahrenheit (212).
Step 6: Edit the main program to add the line "cout << freezing.CelsiusTemperature << endl;" before the other cout statements. When you compile the code, the compiler should give you an error message saying this variable is private. This is an example of "information hiding" in C++. Only the methods in a class are allowed to make changes to private variables, which prevents average users of the class from messing up their values. Now delete this cout line from your program.
Step 7: You all know that absolute zero is the coldest temperature possible. To enforce this, both of the "set" methods compare the temperature parameter to the constant ABSOLUTE_ZERO. If the temperature is above ABSOLUTE_ZERO the method saves it. Otherwise the temperature is set to ABSOLUTE_ZERO. To test this error checking feature, edit your main program to declare a new Temperature object called "coldcold" after the other Temperature objects. Call the "setCelsius" method to initialize this object to -500 Celsius. Then, add a cout line just above the "return 0;" to print this temperature in Celsius. Use the same format as the other two cout lines. Compile and run your program to see what is output this time.
Step 8: There is still some debate on the hottest temperatur

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!