zoukankan      html  css  js  c++  java
  • 第十六周oj刷题——Problem J: 填空题:静态成员---计算学生个数

    Description

    学生类声明已经给出。在主程序中依据输入信息输出实际建立的学生对象个数,以及全部学生对象的成绩总和。

    Input

    学生个数

    相应学生个数的学生信息(姓名    年龄    成绩)

    Output

    学生个数

    全部学生的成绩之和

    Sample Input

    3
    guo  34  98
    zhang    56  60
    li   23   87
    

    Sample Output

    the count of student objects=3
    
    /* All rights reserved.
     * 文件名:test.cpp
     * 作者:陈丹妮
     * 完毕日期:2015年 6 月 27 日
     * 版 本 号:v1.0
     */
    #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;
    }
    
     
    学习心得:静态数据成员不难。挺easy的,继续加油努力吧!!

     
  • 相关阅读:
    ThreadPoolExecutor源码解析
    AQS框架
    (转)rvm安装与常用命令
    (转).gitignore详解
    (转)可简化iOS 应用程序开发的6个Xcode小技巧
    (转)webView清除缓存
    (转)git常见错误
    iOS本地通知
    (转)iOS获取设备型号
    (转)iOS平台UDID方案比较
  • 原文地址:https://www.cnblogs.com/blfshiye/p/5076988.html
Copyright © 2011-2022 走看看