Online Book Reader

Home Category

Professional C__ - Marc Gregoire [111]

By Root 1392 0

// Returns string representation of the temperature

virtual std::string getTemperature() const;

protected:

int mCurrentTempFahrenheit;

int mDistanceFromMars;

};

Code snippet from WeatherPrediction\WeatherPrediction.h

Note that this class marks all methods as virtual, because the class presumes that its methods might be overridden in a subclass.

This class solves most of the problems for your program. However, as is usually the case, it’s not exactly right for your needs. First, all the temperatures are given in Fahrenheit. Your program needs to operate in Celsius as well. Also, the showResult() method might not display the result in a way you require.

Adding Functionality in a Subclass

When you learned about inheritance in Chapter 3, adding functionality was the first technique described. Fundamentally, your program needs something just like the WeatherPrediction class but with a few extra bells and whistles. Sounds like a good case for inheritance to reuse code. To begin, define a new class, MyWeatherPrediction, that inherits from WeatherPrediction.

#include "WeatherPrediction.h"

class MyWeatherPrediction : public WeatherPrediction

{

};

Code snippet from WeatherPrediction\MyWeatherPrediction.h

The preceding class definition will compile just fine. The MyWeatherPrediction class can already be used in place of WeatherPrediction. It will provide the same functionality, but nothing new yet.

For the first modification, you might want to add knowledge of the Celsius scale to the class. There is a bit of a quandary here because you don’t know what the class is doing internally. If all of the internal calculations are made by using Fahrenheit, how do you add support for Celsius? One way is to use the subclass to act as a go-between, interfacing between the user, who can use either scale, and the superclass, which only understands Fahrenheit.

The first step in supporting Celsius is to add new methods that allow clients to set the current temperature in Celsius instead of Fahrenheit and to get tomorrow’s prediction in Celsius instead of Fahrenheit. You will also need protected helper methods that convert between Celsius and Fahrenheit. These methods can be static because they are the same for all instances of the class.

#include "WeatherPrediction.h"

class MyWeatherPrediction : public WeatherPrediction

{

public:

virtual void setCurrentTempCelsius(int inTemp);

virtual int getTomorrowTempCelsius();

protected:

static int convertCelsiusToFahrenheit(int inCelsius);

static int convertFahrenheitToCelsius(int inFahrenheit);

};

Code snippet from WeatherPrediction\MyWeatherPrediction.h

The new methods follow the same naming convention as the parent class. Remember that from the point of view of other code, a MyWeatherPrediction object will have all of the functionality defined in both MyWeatherPrediction and WeatherPrediction. Adopting the parent class’s naming convention presents a consistent interface.

We will leave the implementation of the Celsius/Fahrenheit conversion methods as an exercise for the reader — and a fun one at that! The other two methods are more interesting. To set the current temperature in Celsius, you need to convert the temperature first and then present it to the parent class in units that it understands.

void MyWeatherPrediction::setCurrentTempCelsius(int inTemp)

{

int fahrenheitTemp = convertCelsiusToFahrenheit(inTemp);

setCurrentTempFahrenheit(fahrenheitTemp);

}

Code snippet from WeatherPrediction\MyWeatherPrediction.cpp

As you can see, once the temperature is converted, the method calls the existing functionality from the superclass. Similarly, the implementation of getTomorrowTempCelsius() uses the parent’s existing functionality to get the temperature in Fahrenheit, but converts the result before returning it.

int MyWeatherPrediction::getTomorrowTempCelsius()

{

int fahrenheitTemp = getTomorrowTempFahrenheit();

return convertFahrenheitToCelsius(fahrenheitTemp);

}

Code snippet from WeatherPrediction\MyWeatherPrediction.cpp

The two new methods effectively reuse

Return Main Page Previous Page Next Page

®Online Book Reader