设置类中数组元素的数目
visual C++ Setting the number of array elements within a class
嗨,我正在为气象站编写一个类,该类要求用户输入变量,并将小时数传递给一个数组:计算平均值、高点和低点的值。我得到了它的工作,但想使数组[元素]私有。这可能吗?
这是我到目前为止的代码。提前感谢您的帮助。
布赖恩#include <iostream>
#include <iomanip>
using namespace std;
class WeatherStation
{
public:
WeatherStation();
void GetATemperatures(int[], int);
void DisplayATemperatures( int[], int);
void arrayCalcs(int[], int);
private:
static const int aTemps = 24;
static const int atemps[aTemps];
};
WeatherStation::WeatherStation()
{
int atemps[aTemps];
}
void WeatherStation::GetATemperatures(int atemps[], int aTemps)
{
for (int i = 0; i < aTemps; i++ )
{
cout << "Please enter the temperature for " << i << ":00 ";
while(true)
{
cin >> atemps[i];
if(atemps[i] >= -50 && atemps[i] <= 130)
{
break;
} else {
cout << "This temperature is not validn";
cout << "Please enter a temperature between -50 and 130 degrees F n";
cout << "Please enter a new temperature: ";
}
}
}
}
void WeatherStation::DisplayATemperatures( int atemps[], int aTemps)
{
cout << setw (5) << "Hour" << setw(24)<< "Temperature n";
cout << "n";
for (int k = 0; k < aTemps; k++)
{
cout << setw (3) << k << ":00" << setw (16) << atemps[k]<<endl;
}
cout <<"n";
}
void WeatherStation::arrayCalcs(int atemps[], int aTemps)
{
int sumA = 0;
double average = 0.0;
int minA = atemps[0];
int maxA = atemps[0];
int lowest = 0;
int highest = 0;
//Sum of the AM temps
for (int kk = 0; kk < aTemps; kk++)
{
sumA = sumA + atemps[kk];
}
//calculation for average
average = sumA / aTemps;
//Figuring out the Min and Max AM temps
for (int MM = 0; MM < aTemps; MM++)
{
if(minA > atemps[MM])
{
minA = atemps[MM];
}
else if(maxA < atemps[MM])
{
maxA = atemps[MM];
}
lowest = minA;
highest = maxA;
}
//Display of the Calculation results
cout << "This is the average of todays temperatures: " << average <<endl;
cout <<endl;
cout << "Todays High temperature is: " << highest <<endl;
cout <<endl;
cout << "Todays Low temperature is: " << lowest <<endl;
}
int main()
{
cout <<"Welcome to the weather station.n";
cout <<"Please enter Ferenheit temperatures for calculations: n";
WeatherStation alpha;
alpha.GetATemperatures(atemps, aTemps);
alpha.DisplayATemperatures(temps, Temps);
alpha.arrayCalcs(temps,Temps);
cout << "n";
system("pause");
return 0;
}
1)数组是atemps[]
?如果是这样,它已经是私有的……有什么问题吗?
2)为什么数组类成员是静态的?如果没有充分的理由,不要这样做(因为这看起来像是家庭作业,我几乎可以肯定你没有一个充分的理由)。
3)构造函数中有一行无用的代码——这是函数中唯一的一行。
4)你的教授不会接受你将变量命名为atemps
和aTemps
——如果他们忽略了这一点,我会非常担心你所接受的教育质量。这并不是说变量名本身是一个大问题,而是你命名它们如此相似,因为如果在实际代码中发生这种情况,这是一个维护噩梦的秘诀。
编辑——基于我们的评论聊天,这是我的建议。我没有尝试编译这个,我不声称这是最好的(甚至是建议的)方式来写你的程序…我的建议仅限于将数据留在对象中(以一种有超出这个问题/讨论的增长空间的方式)。
#include <iostream>
#include <iomanip>
using namespace std;
class WeatherStation
{
public:
WeatherStation();
void GetATemperatures();
void DisplayATemperatures();
void arrayCalcs();
private:
static const int aTemps = 24;
int atemps[aTemps];
};
WeatherStation::WeatherStation()
{
}
void WeatherStation::GetATemperatures()
{
for (int i = 0; i < aTemps; i++ )
{
cout << "Please enter the temperature for " << i << ":00 ";
while(true)
{
cin >> atemps[i];
if(atemps[i] >= -50 && atemps[i] <= 130)
{
break;
} else {
cout << "This temperature is not validn";
cout << "Please enter a temperature between -50 and 130 degrees F n";
cout << "Please enter a new temperature: ";
}
}
}
}
void WeatherStation::DisplayATemperatures()
{
cout << setw (5) << "Hour" << setw(24)<< "Temperature n";
cout << "n";
for (int k = 0; k < aTemps; k++)
{
cout << setw (3) << k << ":00" << setw (16) << atemps[k]<<endl;
}
cout <<"n";
}
void WeatherStation::arrayCalcs()
{
int sumA = 0;
double average = 0.0;
int minA = atemps[0];
int maxA = atemps[0];
int lowest = 0;
int highest = 0;
//Sum of the AM temps
for (int kk = 0; kk < aTemps; kk++)
{
sumA = sumA + atemps[kk];
}
//calculation for average
average = sumA / aTemps;
//Figuring out the Min and Max AM temps
for (int MM = 0; MM < aTemps; MM++)
{
if(minA > atemps[MM])
{
minA = atemps[MM];
}
else if(maxA < atemps[MM])
{
maxA = atemps[MM];
}
lowest = minA;
highest = maxA;
}
//Display of the Calculation results
cout << "This is the average of todays temperatures: " << average <<endl;
cout <<endl;
cout << "Todays High temperature is: " << highest <<endl;
cout <<endl;
cout << "Todays Low temperature is: " << lowest <<endl;
}
int main()
{
cout <<"Welcome to the weather station.n";
cout <<"Please enter Ferenheit temperatures for calculations: n";
WeatherStation alpha;
alpha.GetATemperatures();
alpha.DisplayATemperatures();
alpha.arrayCalcs();
cout << "n";
system("pause");
return 0;
}
相关文章:
- 数组元素打印的递归方法
- 如何将字节数组元素替换为修改的十六进制 ASCII 符号?
- 为什么使用数组元素查找最大数字的程序不起作用?
- 数组元素更改值?
- 如何访问宏中定义的数组元素
- 将数组元素递增 1
- C++函数,它将数组、谓词和运算符作为参数,并将运算符应用于满足谓词的数组元素
- 存储指向动态数组元素的指针
- C++通过别名指针以静默方式将错误的类型分配给数组元素
- 在 std::map 中插入数组元素
- 使用指针访问数组元素时出现意外结果
- c++ 使用动态分配运算符反向数组元素
- 使用 while / do-while 循环进行数组元素设置
- C++:数组元素设置为0
- 设置数组元素时MSVC访问冲突
- 通过memset函数设置2D int数组元素为1
- 设置类中数组元素的数目
- c++默认初始化是否将数组元素设置为默认值?
- 如何使用类方法设置单个类数组元素
- 优化了数组元素的计算和设置