c++学習ノートの静的メンバー関数
868 ワード
静的メンバー関数の設定の目的は、プログラム内のいくつかの変数の保値のために設定されます.
静的メンバー関数の定義に注意すべき点
(1)静的メンバー関数は主に静的データメンバーにアクセスするために使用され、非静的メンバーにアクセスしない
(2)静的メンバー関数は,オブジェクトの一部ではなくクラスの一部である.
統計学生の平均成績コード
静的メンバー関数の定義に注意すべき点
(1)静的メンバー関数は主に静的データメンバーにアクセスするために使用され、非静的メンバーにアクセスしない
(2)静的メンバー関数は,オブジェクトの一部ではなくクラスの一部である.
統計学生の平均成績コード
#include
using namespace std;
class student
{
public:
student(int n,int a,float s):num(n),age(a),score(s){}//
void total();
static float average();//
private:
int num;
int age;
float score;
static float sum;
static int count;
} ;
void student::total()
{
sum+=score;
count ++;
}
float student::average()
{
return (sum/count);
}
float student::sum=0;
int student::count=0;
int main()
{
student stud[3]=
{
student(1001,18,70),
student(1002,19,78),
student(1005,20,98)
};
int n;
cout<>n;
for(int i=0;i