C++ 上课。有人能告诉我这个练习是否正确吗?
我正试图根据以下说明创建温度等级: 考虑一个叫做温度的类。这个类由一个名为degrees的双精度变量和一个名为type的字符串组成。此类具有默认构造函数。该类还有一个构造函数,该构造函数只接受degrees变量的参数。它有一个名为get_temperature()的访问器,它返回一个双精度值。它有一个重载bool运算符<,该运算符将另一个温度对象T作为参数。它还有一个名为set_type的mutator成员函数,该函数将字符串作为参数,并且不返回任何内容。编写类温度的声明。适当时使用const关键字C++ 上课。有人能告诉我这个练习是否正确吗?,c++,class,C++,Class,我正试图根据以下说明创建温度等级: 考虑一个叫做温度的类。这个类由一个名为degrees的双精度变量和一个名为type的字符串组成。此类具有默认构造函数。该类还有一个构造函数,该构造函数只接受degrees变量的参数。它有一个名为get_temperature()的访问器,它返回一个双精度值。它有一个重载bool运算符
#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;
};
#包括
#包括
#包括
使用名称空间std;
课堂温度{
公众:
温度(双摄氏度){
双度=度×度;
}
无效集类型(字符串类型){
字符串类型=类型_x;}
双温度常数;
布尔运算符<(温度T)常数;
私人:
双学位;
字符串类型;
};
有几件事可以/应该纠正
运算符<参数可以是常量引用
bool operator < (const Temperature& T) const;
定义一个局部变量,将其分配给传递给构造函数和set_type方法的参数中的值。因此,对象成员字段并不像您期望的那样初始化。
对于构造函数,您可以简单地
Temperature (double degrees_x) : degrees(degrees_x), type(""){}
对于set_类型方法:
type = type_x;
此外,该方法还可以采用const&参数,而不是字符串值
另外两个include(time.h和iostream)在这里似乎是不必要的。这里是一个修改版本,其中包含一些关于更改的内联注释
#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
};
#包括
#包括
#包括
使用名称空间std;
课堂温度{
公众:
温度(双摄氏度):
度(degrees_x)//设置对象变量的值
//这意味着我们可以在以后使用const double degrees
{}
//请注意,签名已更改为常量&
无效集类型(常量字符串和类型){
type=type_x;//将对象变量类型设置为值。
//也可以使用non-const,然后使用std::move。
//string type=type_x;但这会创建一个局部变量,并指定值…而不是您想要的值。
}
//const在函数声明中的良好使用。
双温度常数;
bool运算符<(const-Temperature&t)const;//但这应该是const&argument,因为您不应该更改它
//作为样式说明,T是模板参数的常用名称,因此我建议将其命名为T而不是T
私人:
const double degrees;//此值在构造函数之后不会更改
字符串类型;//但是可以这样做,所以不能是常量
};
#包括
#包括
#包括
使用名称空间std;
课堂温度{
//无需将私有成员声明为private,默认值为private
双学位;
字符串类型;
公众:
温度(双摄氏度){
度=度x;
}
//设置类型时,使用“set_type(字符串类型_x)”效率低下,因为如果给定l值,类型_x将被复制,然后再次复制到“type”中
//将“type_x”获取为const&将确保创建单个副本,当调用类“string”的副本分配时,该副本位于“string type=type_x”中
无效集类型(常量字符串和类型){
字符串类型=类型x;
}
双温度常数;
//如前所述,最合理的做法是将T作为常量传递,因为在检查t1时,您可能不会修改第二个操作数,您的问题是……?double degrees=…
不属于该构造函数。这声明了一个局部变量,它隐藏了成员变量degrees
。它应该是degrees=…
,理想情况下,它应该在成员初始化列表中。这同样适用于set\u type
中的type
。您也可以在
方法中将参数Temperature T
替换为const Temperature&T
。它看起来像这样吗?#包含#包含##包括使用命名空间std;类Temperature{public:Temperature(){degrees=0.0;type=”“;};Temperature(double degrees\ux){degrees=degrees\ux;}void set\u type(string type\ux){type=type\ux;}double get\u Temperature()const;bool操作符<(Temperature T)常量;私有:双度;字符串类型;};
#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
};
#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;
};