Click here to Skip to main content
15,902,114 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hello. Okay i have this assignment, This is the question and my approach:
_______________________________________________________________________
Consider the following declaration of a Temperature class. The class holds information on high
and low temperature values. A constructor assigns initial values to the two private data members
highTemp and lowTemp. The updateTemp function takes a new data value and determines if one of
the temperature values in the object should be updated. If the value marks a new low, then lowTemp
is updated. Similarly a new high would change highTemp. The access functions getHightTemp() and
getLowTemp() returns the high and low temperature respectively.
C++
Class Temperature
{
private:
float highTemp, lowTemp;
public:
//constructor
Temperature(float h, float l)
//update low and high temperatures.
Void updateTemp(float temp);
//access functions
float getHighTemp();
float getLowTemp();
};

Required:
i) Implement the constructor of the Temperature class as an external function.
ii) Implement the member function updateTemp().
iii) Implement the access functions getHighTemp() and getLowTemp()
_______________________________________________________________________

What I have tried:

C++
#include <iostream>
using namespace std;
class Temperature{
    private:
        float HighTemp, LowTemp;
    public:
        Temperature(float h, float l);

        void updateTemp();

        float getHighTemp(float h){
            return HighTemp;
        }

        float getLowTemp(float l){
            return LowTemp;
        }

        float setHighTemp(float h){
            HighTemp=h;
        }

        float setLowTemp(float l){
            LowTemp=l;
        }
};

Temperature::Temperature(float h, float l){
        setHighTemp(h);
        setLowTemp(l);
}
void updateTemp(float temp){
//I'm stuck here
}
int main()
{
//i unno what to do}
Posted
Updated 1-Aug-18 23:35pm
v2
Comments
Richard MacCutchan 2-Aug-18 3:43am    
he updateTemp function takes a new data value and determines if one of
the temperature values in the object should be updated. If the value marks a new low, then lowTemp
is updated. Similarly a new high would change highTemp.

That seems perfectly clear, if the new temperature is less than the current low value, then store it in the low value variable. If it is higher than the current high value ...

I would start with a proper class declaration:
C++
class Temperature
{
private:
  double high, low;
public:
  Temperature(double h, double l);
  void update(double temperature);
  double getHigh();
  double getLow();
};

Your constructor can be improved: consider using the initialization list:
C++
Temperature::Temperature(double h, double l) : high(h), low(l)
{
}

Now, consider the method you're stuck at, namely update
C++
void Temperature::update(double temperature )
{
  // here: if 'high' is lower than 'temperature' then update it.
  // here: if 'low' is higher than  'temperature' then update it.
}
 
Share this answer
 
v2
Comments
KarstenK 2-Aug-18 5:54am    
that what I call "assistd coding" ;-)
It's your homework, I will not give you exact answer.
From your code I can see that you have not properly understand your assignment.
First focus on the requirement.
1. implement constructor. That you have done, it
2. implement updateTemp; you have not. When you implement a member function you need to follow this simple rule. Follow this guide C++ Class Member Functions[^]
You still have issue understanding requirement of updateTemp; Your assignment suggested a specific requirement. You need to follow that. "
The updateTemp function takes a new data value and determines if one of the temperature values in the object should be updated
". Focus on this.
3. Implement access function. in the class declaration no argument is set, but your definition shows argument. And what would you do with those argument? Seems nothing.
 
Share this answer
 
When you get an update of the temperature class, so it can be an new high or a new low. So consider than updating the values...
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900