/* Temperature.txt is the documentation file for class Temperature. * * INVARIANT: myScale == 'F' || myScale == 'C' || myScale == 'K' * && myDegrees is valid for myScale. */ #ifndef TEMPERATURE #define TEMPERATURE #include 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