[YTU]_2641 9 填空题:静态成员---计算学生个数)
題目描述
學生類聲明已經給出,在主程序中根據輸入信息輸出實際建立的學生對象個數,以及所有學生對象的成績總和。
?
在下面的程序段基礎上完成設計,只提交begin到end部分的代碼
#include <iostream>
#include <string>
using namespace std;
?
class student
{?
private:
??????stringname; ?//學生姓名
??????intage; ? ? ?//學生年齡
??????intscore; ? ?//學生成績
? ? ? ?static intcount; //記錄學生對象個數
??????staticint sum; ?//記錄所有學生的總成績
public:
??????student(stringn,int a,int s); ?//構造函數
??????staticint get_count(); ?//靜態成員函數,獲取count的值
??????staticint get_sum(); ? //靜態成員函數,獲取sum的值
};
?
?
?
?
//將程序需要的成份寫下來,只提交begin到end部分的代碼
//******************** begin********************
int student::count=0;
_____(1)_______;
?
________(2)___________
{
? ? ?name=n;
???????age=a;
???????score=s;
???????count++;
???????sum+=s;
}
?
int student::get_count()
{
? ? ______(3)_______;
}
?
int student::get_sum()
{
? ? ______(4)______;
}
?
?
?
//********************* end********************
?
?
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 ofstudent objects=";
?cout<<student::get_count()<<endl;
? cout<<"the sum of allstudents score=";
?cout<<student::get_sum()<<endl;
? return 0;
}
?
?
輸入
學生個數
對應學生個數的學生信息(姓名 ? ?年齡 ? ?成績)
?
輸出
學生個數
所有學生的成績之和
?
樣例輸入
3
guo?34?98
zhang???56?60
li??23??87
樣例輸出
the count of student objects=3
the sum of all students score=245
提示
?
只提交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的值 };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; }總結
以上是生活随笔為你收集整理的[YTU]_2641 9 填空题:静态成员---计算学生个数)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: [YTU]_2639 ( 改错题:类中私
- 下一篇: [YTU]_2642 (填空题:类模板-