/* Temperature.doc is the documentation file for class Temperature. *

advertisement
/* Temperature.doc is the documentation file for class Temperature.
*
* INVARIANT: myScale == 'F' || myScale == 'C' || myScale == 'K'
*
&& myDegrees is valid for myScale.
*/
#ifndef TEMPERATURE
#define TEMPERATURE
#include<iostream>
using namespace std;
const double MIN_CELSIUS
= -273.15;
const double MIN_FAHRENHEIT = -459.67;
const double MIN_KELVIN
= 0.0;
class Temperature
{
public:
ÊÊÊ/* Default-value constructor
* POST: myDegrees, myScale have been initialized to default values.
*/
Temperature();
ÊÊÊ/* Explicit-value constructor
* Receive: initialDegrees, a double;
*
initialScale, a char.
* PRE: initialScale is one of {'f', 'F', 'c', 'C', 'k', 'K'}
*
and initialDegrees is valid for initialScale.
* POST: myDegrees == initialDegrees && myScale == initialScale.
*/
Temperature(double initialDegrees, char initialScale);
ÊÊÊ/* Degrees accessor
* Return: myDegrees.
*/
double getDegrees() const;
ÊÊÊ/* Scale accessor
* Return: myScale.
*/
char getScale() const;
ÊÊÊ/* Fahrenheit converter
* Return: my Fahrenheit equivalent.
*/
Temperature inFahrenheit() const;
ÊÊÊ/* Celsius converter
* Return: my Celsius equivalent.
*/
Temperature inCelsius() const;
ÊÊÊ/* Kelvin converter
* Return: my Kelvin equivalent.
*/
Temperature inKelvin() const;
/* less-than relational operator
* Receive: rightOperand, a Temperature.
* Return: true, if and only if I am less than rightOperand.
*/
bool operator< (const Temperature & rightOperand) const;
/* equality relational operator
* Receive: rightOperand, a Temperature.
* Return: true, if and only if I am equal to rightOperand.
*/
bool operator== (const Temperature & rightOperand) const;
ÊÊÊ/* Output
* Receive: out, an ostream.
* PRE: out.good()
* Send back: out, containing myDegrees, a blank, and myScale.
*/
void print(ostream& out) const;
ÊÊÊ/* Input
* Receive: in, an istream.
* PRE: in.good(), and in contains a double and a char that
*
together comprise a valid temperature.
* Send back: in, with the double and char extracted from it.
* POST: myDegrees == the double && myScale == the char.
*/
void read(istream & in);
ÊÊÊ/* Validity checking
* Receive: degrees, a double;
*
scale, a char.
* Return: true if and only iff degrees and scale together
*
comprise a valid temperature value.
*/
static bool isValid(double degrees, char scale);
private:
/* relational comparison utility method
* Receive: rightOperand, a Temperature.
* Return: a negative value if I am less-than rightOperand;
*
zero if I am equal to rightOperand;
*
a positive value if I am greater than rightOperand.
*/
int compare(const Temperature& rightOperand) const;
double myDegrees;
char
myScale;
};
// 'F', 'C', or 'K'
// -------- Default-value constructor ----------------------------inline Temperature::Temperature()
{
myDegrees = 0.0;
myScale = 'C';
}
// -------- Degrees extractor ------------------------------------inline double Temperature::getDegrees() const
{
return myDegrees;
}
// -------- Scale extractor --------------------------------------inline char Temperature::getScale() const
{
return myScale;
}
// -------- Output method -------------------------------inline void Temperature::print(ostream& out) const
{
out << myDegrees << ' ' << myScale;
}
/* -------- Temperature ostream output --------------------------* Receive: out, an ostream;
*
theTemp, a Temperature.
* PRE: out is an open ostream.
* Output: theTemp, to out.
* Send back: out, containing the values that comprise theTemp.
* Return: out.
*/
inline ostream& operator<<(ostream& out, const Temperature& theTemp)
{
theTemp.print(out); // tell theTemp to print itself
return out;
}
/* -------- Temperature istream input --------------------------* Receive: in, an istream;
*
theTemp, a Temperature.
* PRE: in is an open istream containing a Temperature value.
* Input: values for theTemp, via in.
* Send back: in, with the Temperature value extracted from it.
* Return: in.
*/
inline istream& operator>>(istream& in, Temperature& theTemp)
{
theTemp.read(in);
return in;
}
// -------- less-than ---------------------------------------------inline bool Temperature::operator< (const Temperature & rightOperand)
const
{
return compare(rightOperand) < 0;
}
// -------- equality ---------------------------------------------inline bool Temperature::operator== (const Temperature & rightOperand)
const
{
return compare(rightOperand) == 0;
}
#endif
Download