YTU 2641: 填空题:静态成员---计算学生个数
2016-02-08 12:56
295 查看
2641: 填空题:静态成员---计算学生个数
时间限制: 1 Sec 内存限制: 128 MB提交: 267 解决: 206
题目描述
学生类声明已经给出,在主程序中根据输入信息输出实际建立的学生对象个数,以及所有学生对象的成绩总和。在下面的程序段基础上完成设计,只提交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部分的代码
//******************** 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 of student objects=";
cout<<student::get_count()<<endl;
cout<<"the sum of all students 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; }
相关文章推荐
- Python成长之路第二篇(1)_数据类型内置函数用法
- [5] CMake总结 - 2 基本命令
- YTU 2640: 编程题:运算符重载---矩阵求和
- YTU 2639: 改错题:类中私有成员的访问
- 需求 - 17 - “录制圈”
- YTU 2636: B3 指向基类的指针访问派生类的成员函数
- YTU 2635: P4 游戏中的Human角色
- 椭圆形的窗体
- YTU 2634: E3 继承了,成员函数却不可访问
- hdu 5620
- YTU 2633: P3 数钱是件愉快的事
- YTU 2632: B2 友元光顾
- YTU 2631: B1 能存各种类型数据的Store类
- Codeforces 625 D Finals in arithmetic
- YTU 2630: E2 驾驭const
- YTU 2629: E1 一种颜色,三个分量
- cocos2dx3.2 异步加载和动态加载
- 哈理工 1170 语法检查-括号匹配【stack应用】【水题~】
- YTU 2626: B 统计程序设计基础课程学生的平均成绩
- JAVA 内部类、外部类、继承、抽象类、接口实战