//
// main.cpp
// score
//
// Created by duanqibo on 2019/7/12.
// Copyright © 2019年 duanqibo. All rights reserved.
// 面向对象程序设计--简单
#include <iostream>
using namespace std;
class student
{
private:
double score;
static double total;
static double count;
public:
void scoretotalcount(double s)
{
score=s;
total=total+score;
count++;
}
static double sum()
{
return total;
}
static double average()
{
return total/count;
}
};
double student::total=0;
double student::count=0; //静态变量初始化
int main(int argc, const char * argv[])
{
int n;
double s;
cout<<"请输入学生人数:";
cin>>n;
student stu;
for(int i=0;i<n;i++)
{
cout<<"请输入第"<<i+1<<"个学生的分数:";
cin>>s;
stu.scoretotalcount(s);
}
cout<<"总分:"<<student::sum()<<endl;
cout<<"平均分:"<<student::average()<<endl;
return 1;
}
运行结果: