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;
boiling.setCelsius;
cout "freezing: freezing.getFahrenheitF
;
cout "boiling: boiling.getFahrenheitF
;
return ;
Step : 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 : In order to make the compiler errors go away, you need to copypaste 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 ABSOLUTEZERO ;
class Temperature
public:
Temperature;
Temperatureconst Temperature & Temp;
~Temperature;
double getCelsius const;
double getFahrenheit const;
void setCelsiusdouble Temp;
void setFahrenheitdouble Temp;
private:
double CelsiusTemperature;
;
Step : To complete your program, you need to copypaste 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 ;
Temperature::Temperatureconst Temperature & Temp
CelsiusTemperature Temp.CelsiusTemperature;
Temperature::~Temperature
double Temperature::getCelsius const
return CelsiusTemperature;
double Temperature::getFahrenheit const
return CelsiusTemperature ;
void Temperature::setCelsiusdouble Temp
CelsiusTemperature Temp;
if CelsiusTemperature ABSOLUTEZERO
CelsiusTemperature ABSOLUTEZERO;
void Temperature::setFahrenheitdouble Temp
CelsiusTemperature Temp ;
if CelsiusTemperature ABSOLUTEZERO
CelsiusTemperature ABSOLUTEZERO;
Step : Compile your program again. Hopefully you will not get any error messages. Run the program to see what is printed out.
Step : 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; the value of the parameter 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
Step : 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 : 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 ABSOLUTEZERO. If the temperature is above ABSOLUTEZERO the method saves it Otherwise the temperature is set to ABSOLUTEZERO. 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 Celsius. Then, add a cout line just above the "return ; 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 : 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
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
