有人帮我理解这个问题 [英] Someone help me understand this question

查看:85
本文介绍了有人帮我理解这个问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好。好的,我有这个任务,这是问题和我的方法:

_______________________________________________________________________

考虑以下温度等级声明。该课程保存有关高额b $ b和低温值的信息。构造函数将初始值分配给两个私有数据成员

highTemp和lowTemp。 updateTemp函数获取一个新的数据值,并确定是否应该更新对象中的温度值之一

。如果该值标记为新的低值,则更新lowTemp

。同样,新的高点会改变highTemp。访问函数getHightTemp()和

getLowTemp()分别返回高温和低温。

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.

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();
};



必需:

i)将Temperature类的构造函数实现为外部函数。

ii)实现成员函数updateTemp()。

iii)实现访问函数getHighTemp()和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:

#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}

推荐答案

我会从适当的课堂声明开始:

I would start with a proper class declaration:
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:

Temperature::Temperature(double h, double l) : high(h), low(l)
{
}



现在,考虑一下你所坚持的方法,即更新


Now, consider the method you're stuck at, namely update

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.
}


这是你的功课,我不会给你准确的答案。

从您的代码我可以看到您没有正确理解您的任务。

首先关注要求。

1.实现构造函数。你已经完成了它,它是b $ b 2.实现updateTemp;你还没有。实现成员函数时,您需要遵循这个简单的规则。请遵循本指南 C ++类成员函数 [ ^ ]

您仍然需要了解updateTemp的问题;您的作业提出了具体要求。你需要遵循这一点。
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

。专注于此。

3.实现访问功能。在类声明中没有设置参数,但是你的定义显示了参数。那些论点你会怎么做?似乎什么都没有。

". 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.


当您获得温度等级的更新时,它可以是新的高或新低。因此,请考虑而不是更新值...
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...


这篇关于有人帮我理解这个问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆