Question: Consider the following C++ program. As you can see it consists of a very short main program that declares and uses two objects of the
Consider the following C++ program. As you can see it consists of a very short main program that declares and uses two objects of the Temperature class.
#includeusing 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 //-------------------------------- class Temperature { public: Temperature(); Temperature(const Temperature & Temp); ~Temperature(); double getCelsius() const; double getFahrenheit() const; void setCelsius(double Temp); void setFahrenheit(double Temp); private: static const double ABSOLUTE_ZERO = -273.15; 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 temperature possible. For the purposes of this program, lets use the temperature at the center of our sun, since it is the hottest thing around. Edit your class interface to add a constant "SUN_CORE_TEMP" equal to 15,710,000 Celsius. Then edit your two "set" methods to add some error checking code to make sure that we do not save a temperature larger than this value. Once you have this written, compile and run your program to make sure it is still working correctly.
Step 9: Now modify the main program to declare a new Temperature object called "hothot" after the "coldcold" object. Call the "setCelsius" method to initialize this object to 20,000,000 Celsius. Then, add a cout line to print this temperature in Celsius just after the other three couts. Use the same format as the other three cout lines. Compile and run your program to see what is output this time. Hopefully your value of "hothot" will not be 20,000,000 Celsius.
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
