类。有人能告诉我,我是否正确地进行了这项运动?

时间:2016-11-18 12:10:45

标签: c++ class

我正在尝试根据这些说明创建一个Temperature类:

考虑一个叫做温度的类。该类由一个名为degrees的double和一个名为type的字符串组成。该类有一个默认的构造函数。该类还有一个构造函数,它接受只接受degrees变量参数的参数。它有一个名为get_temperature()的访问器,它返回一个double。它有一个过载的bool运算符<它将另一个温度对象T作为参数。它还有一个名为set_type的mutator成员函数,它将一个字符串作为参数并且不返回任何内容。写出类温度的声明。适当时使用const关键字。

#include <iostream>
#include <string>
#include <time.h>

using namespace std;

class Temperature {
public:
    Temperature (double degrees_x){
        double degrees = degrees_x;
    }
    void set_type(string type_x){
        string type = type_x;}
        double get_temperature() const;
    bool operator < (Temperature T) const;
private:
    double degrees;
    string type;


};

3 个答案:

答案 0 :(得分:0)

可以/应该纠正的一些事情。

运营商&lt;参数可以是const引用

$(function(){
        var $list = $('.my-loading-list'),
            $imgs = $list.find('img'),

            animateImages = function(){
                var active = 0;
                setInterval(function(){



                    if(active > $imgs.length){
                        active = 0;
                    } else {
                        active++;
                    }
                    $imgs.attr('src', 'https://cdn3.iconfinder.com/data/icons/cool-me-down-thermometers/70/Weather_cloudy_grey-128.png');
                    $imgs.eq(active).addClass('active').attr('src', 'https://cdn3.iconfinder.com/data/icons/vibrant-weather/70/Colour_Weather_cloudy_grey-128.png');
                }, 150);

                setInterval(function(){
                    $imgs.removeClass('active');
                }, 150);
            };

        animateImages();
    });

避免&#34;使用命名空间&#34;在头文件中 - 污染全局命名空间

行:

bool operator < (const Temperature& T) const;

定义一个局部变量,您可以根据传递给构造函数和set_type方法的参数来赋值。因此,对象成员字段未按预期初始化。 对于构造函数,您可以简单地添加

double degrees = degrees_x;
string type = type_x;

至于set_type方法:

Temperature (double degrees_x) : degrees(degrees_x), type(""){}

此方法也可以采用const&amp;参数而不是字符串值

此外,这里似乎没有两个包含(time.h和iostream)。

答案 1 :(得分:0)

这是一个修改后的版本,其中包含一些有关更改的内联注释。

#include <iostream>
#include <string>
#include <time.h>

using namespace std;

class Temperature {
public:
    Temperature (double degrees_x) : 
      degrees(degrees_x)      // this sets the value of the object variable
                              // it means that we can use const double degrees later
    {}

    // Note here that the signature has changed to be a const & 
    void set_type(const string& type_x) {
        type = type_x;         //sets the object variable type to the value.
        // one could alternatively use the non-const, and then std::move.

        //string type = type_x;  but this creates a local variable, and assigns the value... not what you want.
    }

    // good uses of const here for function declarations.
    double get_temperature() const;
    bool operator < (const Temperature& t) const; // but this should be const & argument, since you shouldn't change it
    // as a style note, T is a common name for template parameters, so I'd suggest naming it t rather than T
private:
    const double degrees;  // this never changes after the constructor
    string type;           // this however can, so mustn't be const
};

答案 2 :(得分:0)

    #include <iostream>
#include <string>
#include <time.h>

using namespace std;

class Temperature {
    //no need to declare private members as private, the default is private
    double degrees;
    string type;
public:
    Temperature (double degrees_x){
        degrees = degrees_x;
    }
    //when setting the type, using 'set_type(string type_x) is inefficient, as type_x will be copied if given an l-value, and then copied again into 'type'
    //getting 'type_x' as const& will make sure you make a single copy, which is in 'string type = type_x' when the copy assigment of the class 'string' is called
    void set_type(const string& type_x){
        string type = type_x;
    }
    double get_temperature() const;
    //the most reasonable thing to do, as already mentioned, is passing T as const&, since you probably won't modify the second operand when checking if T1<T2
    bool operator < (const Temperature &T) const;
};