类的访问器和修改器函数的问题
我正在尝试编写一个简单的程序,该程序接收用户输入的开尔文温度,并输出其法氏温度和摄氏度转换。然而,对于开尔文输入 273.15,我的程序不断输出奇怪的数字,例如华氏温度为“6.95274e-310”,摄氏度为“4.6519e-310”。我相信类中的访问器和修改器函数存在问题,但我不太确定问题是什么。
#include <iostream>
using namespace std;
class Temperature{
private:
double Kelvin;
double Celsius;
double Farenheit;
public:
void setKelvin(double);
void setCelsius();
void setFarenheit();
double getKelvin();
double getCelsius();
double getFarenheit();
};
void Temperature::setKelvin(double x){
Kelvin = x;
}
void Temperature::setCelsius(){
Celsius = Kelvin-273.15;
}
void Temperature::setFarenheit(){
Farenheit = (Celsius/(5.0/9))+32;
}
double Temperature::getKelvin(){
return Kelvin;
}
double Temperature::getCelsius(){
return Celsius;
}
double Temperature::getFarenheit(){
return Farenheit;
}
int main(){
cout<<"Enter a temperature in degrees Kelvin: ";
double y;
cin>>y;
Temperature K;
K.setKelvin(y);
cout<<"Temperature after converting to Celsius: ";
cout<<K.getCelsius();
cout<<"Temperature after converting to Farenehti: ";
cout<<K.getFarenheit();
}
I'm trying to write a simple program that takes in a Kelvin temperature input from the user, and output the Farneheit and Celsius conversions of it. However, for a Kelvin input of 273.15, my program keeps outputting strange numbers like "6.95274e-310" for Farenheit and "4.6519e-310" for Celsius. I believe there is a problem with the accessor and mutator functions within the class, but I'm not quite sure what the issue is.
#include <iostream>
using namespace std;
class Temperature{
private:
double Kelvin;
double Celsius;
double Farenheit;
public:
void setKelvin(double);
void setCelsius();
void setFarenheit();
double getKelvin();
double getCelsius();
double getFarenheit();
};
void Temperature::setKelvin(double x){
Kelvin = x;
}
void Temperature::setCelsius(){
Celsius = Kelvin-273.15;
}
void Temperature::setFarenheit(){
Farenheit = (Celsius/(5.0/9))+32;
}
double Temperature::getKelvin(){
return Kelvin;
}
double Temperature::getCelsius(){
return Celsius;
}
double Temperature::getFarenheit(){
return Farenheit;
}
int main(){
cout<<"Enter a temperature in degrees Kelvin: ";
double y;
cin>>y;
Temperature K;
K.setKelvin(y);
cout<<"Temperature after converting to Celsius: ";
cout<<K.getCelsius();
cout<<"Temperature after converting to Farenehti: ";
cout<<K.getFarenheit();
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
每当您看到这样的怪异价值时,这些都很可能是不可分化的内存(又称垃圾)。它是之前由另一个应用程序编写的数据,而不是由您的(初始化)更改。这就是为什么始终初始化变量非常重要的原因。
内置类型(
int
,char
,double
等)未初始化< /strong>:这就是为什么您经常看到int x(0);
之类的语句的原因。另一方面,其他对象是通过默认构造函数初始化的。这就是为什么std :: String str;
不创建垃圾:一个空std :: String
是由std :: String :: String :: String()创建的
。您的程序中清楚地证明了此行为:
温度:: Celsius
和温度:: Farenheit
永远不会初始化。一个简单的解决方法是在k.setkelvin(y)
:替代解决方案之后添加以下几行,可能涉及在其各自的getters中重新计算摄氏和法伦海特值,或在
setKelvin中系统地“同步”它们
方法。Whenever you see weird values like this, it's very likely that these are uninitialized memory (aka garbage). It is data that was written by another application before and not been changed (initialized) by yours. That is why it's very important to always initialize variables.
Built-in types (
int
,char
,double
, etc) aren't initialized automatically: that is why you often see statements likeint x(0);
. Other objects, on the other hand, are initialized via the default constructor. This is whystd::string str;
doesn't create garbage: an emptystd::string
is created bystd::string::string()
.This behavior is clearly demonstrated in your program:
Temperature::Celsius
andTemperature::Farenheit
are never initialized. An easy fix is to add the following lines afterK.setKelvin(y)
:Alternative solutions may involve recalculating the Celsius and Farenheit values in their respective getters or systematically "synchronizing" them in the
setKelvin
method.