Count students and scores (static)

Source: Internet
Author: User


Count students and scores

Problem description

Define a student class to record the student's student ID and C ++ course score. Static member variables and static member functions are required to calculate the total score and average score of the class C ++ course.


Enter the student ID and score, and each student's information occupies a row until the end of the file.

The output consists of two rows. The first row contains the total number of students and the total score, which are separated by spaces;
The average score of the second row.
Sample Input
101  30102  50103  90104  60105  70
Sample output
5 30060

Reference code:

#include <iostream>#include <stdio.h>#include <string.h>using namespace std;class Student{private:    int id, score;    static int count;    static int sum;public:    Student(int i, int s);    Student(const Student&);    ~Student();    static void show();};Student::Student(int i, int s){    id = i;    score = s;    count++;    sum += score;}Student::Student(const Student& c){    id =;    score = c.score;}Student::~Student(){}void Student::show(){    cout << count << " " << sum << endl;    cout << (double)(1.0 * sum / count) << endl;}int Student::count = 0;int Student::sum = 0;int main(){    //freopen("1.txt", "r", stdin);    int id, score;    while (cin >> id >> score)        Student x(id, score);    Student::show();    return 0;}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.