静态成员

#include <bits/stdc++.h>
using namespace std;

class Student {
public:
    Student(const string&amp; name, int score)
        : itsName(name), itsScore(score) {}

    static float aver() {
        return sum_score / float(count);
    }

    void sum() {
        sum_score += itsScore;
        count++;
    }

private:
    string itsName;
    int itsScore;

    static int sum_score;
    static int count;
};

int Student::sum_score = 0;
int Student::count = 0;

int main()
{
    Student stu[5] ={Student(&quot;Ma&quot;,89),Student(&quot;Hu&quot;,90),Student(&quot;LU&quot;,95),Student(&quot;Li&quot;,88),Student(&quot;Gao&quot;,75)
    };

    for(int i=0;i<5;i++)
        stu[i].sum();

    cout<<&quot;Average=&quot;<<Student::aver()<<endl;
}
全部评论

相关推荐

点赞 评论 收藏
分享
06-25 09:33
厦门大学 Java
球球别拷打俺了:现在日常估计没啥hc了,等到八月多估计就慢慢有了。双九✌🏻不用焦虑的
投递快手等公司10个岗位
点赞 评论 收藏
分享
牛客nb666号:看数据范围, -1e4~1e4, 用一个计数数组存一下, 再按个数让k减到0就行; 堆排不是O(n)的, 快速选择算法是O(n)但随机性较强
点赞 评论 收藏
分享
驼瑞驰_招募评论官版...:这是要去亚马逊雨林守夜吗?
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务