2016-11-18 51 views
-4

我試圖根據這些說明創建一個溫度等級:類。有人可以告訴我,如果我正確地做了這個練習嗎?

考慮一個稱爲溫度的類。這個類由稱爲度的double和稱爲type的字符串組成。這個類有一個默認的構造函數。該類還有一個構造函數,它只接受度變量的參數。它有一個名爲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; 


}; 
+0

而且你的問題是......? –

+2

'double degrees = ...'不屬於該構造函數。這聲明瞭隱藏成員變量'度數'的局部變量。它應該只是'degrees = ...',理想情況下它應該在成員初始化列表中。 'set_type'中的'type'也是一樣的。 – WhozCraig

+0

你也可以用'

回答

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; 
}; 
0

有些事情可以/應該糾正。

操作<說法可能是const引用

bool operator < (const Temperature& T) const; 

避免 「使用命名空間」 中的頭文件 - 污染全局命名空間

的線條:

double degrees = degrees_x; 
string type = type_x; 

定義本地變量,你可以通過傳遞給構造函數和set_type方法的參數來賦值。因此,對象成員字段未按照您的預期進行初始化。 對於構造函數,你可以簡單地把

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

爲set_type方法:

type = type_x; 

而且該方法可能需要一個const &參數,而不是字符串值

另外兩個包括( time.h和iostream)在這裏似乎沒有必要。

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 
}; 
相關問題