7-1 計算全班學生C++課程的總成績和平均成績 (10分)

定義一個類Student,記錄學生C++課程的成績。要求使用靜態數據成員或靜態成員函數計算全班學生C++課程的總成績和平均成績。

輸入格式:
輸入5個不超過100的正整數,作爲C++成績。

輸出格式:
在第一行中輸出成績的和,第二行輸出平均成績。

輸入樣例:
90 80 70 60 50
輸出樣例:
350
70

#include<iostream>
using namespace std;
class Student{
    int score;
    static int total;
public:
    Student(int s=0):score(s){
        total+=s;
    }
    static void display();
};
int Student::total=0;
void Student::display(){
    cout<<total<<endl<<total/5;
}
int main(){
    int score;
    for(int i=0;i<5;i++){
        cin>>score;
        Student s(score);//該對象的作用域爲for循環體,相當於重複創建五次,銷燬五次
    }Student s;
    s.display();
    return 0;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章