嘻唰唰第六批Problem N: 填空題:靜態成員---計算學生個數
阿新 • • 發佈:2019-02-06
Description
學生類宣告已經給出,在主程式中根據輸入資訊輸出實際建立的學生物件個數,以及所有學生物件的成績總和。 在下面的程式段基礎上完成設計,只提交begin到end部分的程式碼 #include <iostream> #include <string> using namespace std; class student { private: string name; //學生姓名 int age; //學生年齡 int score; //學生成績 static int count; //記錄學生物件個數 static int sum; //記錄所有學生的總成績 public: student(string n,int a,int s); //建構函式 static int get_count(); //靜態成員函式,獲取count的值 static int get_sum(); //靜態成員函式,獲取sum的值 }; //將程式需要的成份寫下來,只提交begin到end部分的程式碼Input
學生個數
對應學生個數的學生資訊(姓名 年齡 成績)
Output
學生個數
所有學生的成績之和
Sample Input
3
guo 34 98
zhang 56 60
li 23 87
Sample Output
the count of student objects=3
the sum of all students score=245
程式碼:
#include <iostream>
#include <string>
using namespace std;
class student
{
private:
string name; //學生姓名
int age; //學生年齡
int score; //學生成績
static int count; //記錄學生物件個數
static int sum; //記錄所有學生的總成績
public:
student(string n,int a,int s); //建構函式
static int get_count(); //靜態成員函式,獲取count的值
static int get_sum(); //靜態成員函式,獲取sum的值
};
int student::count=0;
int student::sum=0;
student::student(string n,int a,int s)
{
name=n;
age=a;
score=s;
count++;
sum+=s;
}
int student::get_count()
{
return count;
}
int student::get_sum()
{
return sum;
}
int main()
{
string name;
int age;
int score;
int n;
cin>>n; //輸入學生物件個數
while(n--)
{
cin>>name>>age>>score;
new student(name,age,score);
}
cout<<"the count of student objects=";
cout<<student::get_count()<<endl;
cout<<"the sum of all students score=";
cout<<student::get_sum()<<endl;
return 0;
}