设置类中数组元素的数目

visual C++ Setting the number of array elements within a class

本文关键字:数组元素 设置      更新时间:2023-10-16

嗨,我正在为气象站编写一个类,该类要求用户输入变量,并将小时数传递给一个数组:计算平均值、高点和低点的值。我得到了它的工作,但想使数组[元素]私有。这可能吗?

这是我到目前为止的代码。提前感谢您的帮助。

布赖恩

#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)你的教授不会接受你将变量命名为atempsaTemps——如果他们忽略了这一点,我会非常担心你所接受的教育质量。这并不是说变量名本身是一个大问题,而是你命名它们如此相似,因为如果在实际代码中发生这种情况,这是一个维护噩梦的秘诀。

编辑——基于我们的评论聊天,这是我的建议。我没有尝试编译这个,我不声称这是最好的(甚至是建议的)方式来写你的程序…我的建议仅限于将数据留在对象中(以一种有超出这个问题/讨论的增长空间的方式)。

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