2011-11-14 19 views
0

こんにちは私は、ユーザに変数を入力し、配列に時間を渡す気象ステーションのクラスに取り組んでいます。平均、高および低の値を計算します。私はそれを動作させるが、配列[要素]をプライベートにしたい。これは可能ですか?C++クラス内の配列要素の数を設定する

ここまでは私のコードです。助けをありがとうございました。

ブライアン

#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 valid\n"; 
       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; 
} 
+2

あなたのコードには、多くのローカル/グローバルな名前の競合があります。 C++コンパイラは、ローカルとグローバルが同じ名前を持つ場合、常にローカルを使用します。あなたがそれを認識しているかどうかはわかりません。いずれにせよ、それは単なる観測である。あなたの質問が本当にはっきりしないのですが、明確にしてください。 – littleadv

+0

'aTemps'や' atemps'のように似ている変数名の使用は避けてください。 'aTemps'のより良い名前は' size'か 'length'でしょう。 –

+0

明確にするために、すべての関数はarray-atemps []を使い、その配列の要素の数はaTempsとして定義され、24に等しくなります。 – Brian

答えて

1

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 valid\n"; 
       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; 
} 
+0

ありがとう、私は命名変数をきれいにすることを確認します。メンバ関数は配列[]はatemp []で、配列要素[aTemp]は引数である必要があります。どのようにこれらを渡し、メインでオブジェクト上の関数を呼び出してプログラムを実行させるのですか? – Brian

+0

@Brian - あなたのクラスにはすでにデータが含まれているのでわかりません。通常、C++では、main()はそのデータに直接アクセスすることはできません。オブジェクトを使用する理由の1つは、外部コードからオブジェクトを隠すことです。しかし、main()が実際に配列を所有し、それをオブジェクトに提供している場合は、GetATemperatures()を定義したように配列を提供できますが、オブジェクトの変数を使用していない場合は、 main()の)。最終的にデータにアクセスする関数がクラスに追加される場合は、データをローカルにコピーするか、ポインタを配列(およびその長さ)に保存します。 – mah

+0

これまでのコードよりも多くのコードが表示されています。もう少し明確です。クラスからatempsとaTempsを削除してmain()に入れると、あなたのコードは動作します。あるいは、それらをクラスに残して、それらをクラスの要素を使用するために関数のパラメータから削除することができます。これは、オブジェクト指向プログラミングの方がより期待されるアプローチになります。 – mah

関連する問題