class Temperature { private: float highTemp, lowTemp; //private data members. public: Temperature(float h, float l); void UpdateTemp(float temp); float GetHighTemp (void) const; float GetLowTemp (void) const; };Discussion The constructor must be passed the initial high and low temperature for the object. These values can be changed by the method UpdateTemp. The methods GetLowTemp and GetHighTemp are const functions since they do not alter any data members in the class. The class is in "temp.h". Example
//boiling/freezing point of water in Fahrenheit Temperature fwater(212, 32); //boiling/freezing point of water in Celsius Temperature cwater(100,0); cout << "Water freezes at " << cwater.GetLowTemp << " C" << endl; cout << "Water boils at " << fwater.GetHighTemp << " F" << endl; Output: Water freezes at 0 C Water boils at 212 F
Temperature Class Implementation
//constructor. assign data h to highTemp and l to lowTemp Temperature:: Temperature (float h, float l): highTemp(h) lowTemp(l) {} //update current temperature readings if temp produces new high or low void Temperature::UpdateTemp (float temp) { if(temp> highTemp) highTemp=temp; else if(temp<lowTemp) lowTemp=temp; } //return the high float Temperature::GetHighTemp (void) const { return highTemp; } //return the low float Temperature::GetLowTemp (void) const { return lowTemp; }