DEV Community

Sana Khan
Sana Khan

Posted on

💻 Understanding Arrays in C++ Classes: A Student Score Program

In this blog, I’ll walk you through a simple yet effective C++ program that uses classes and objects to manage student scores and determine how many students scored more than the first student.

Let’s break it down together! 🧩

🧠 Problem Statement

We have a group of students. Each student gives five test scores. We want to calculate how many students scored more than the first student.

Simple, right? Now let's dive into how this is achieved using classes in C++.

🧑‍🎓 Creating the Student Class

class Student {
private:
    int scores[5]; // Each student has 5 scores
public:
    void input();
    int calculateTotalScore();
};

Enter fullscreen mode Exit fullscreen mode

We define a class Student to encapsulate the scores and operations.

scores[5] is a fixed-size array holding 5 test scores.

input() allows entering the scores.

calculateTotalScore() adds up the scores.

🖊️ Input Function

void Student::input() {
    for (int i = 0; i < 5; i++) {
        cin >> scores[i];
    }
}

Enter fullscreen mode Exit fullscreen mode

This method allows each student object to take 5 scores as input from the user.

➕ Score Calculation

int Student::calculateTotalScore() {
    int sum = 0;
    for (int i = 0; i < 5; i++) {
        sum += scores[i];
    }
    return sum;
}

Enter fullscreen mode Exit fullscreen mode

This method calculates the total score for each student by summing up their individual scores.

🧮 Main Function Logic

int main() {
    int n;
    cin >> n;
    vector<Student> Students(n);
    int num = 0;

    for (int i = 0; i < n; i++) {
        Students[i].input();
        if (Students[0].calculateTotalScore() < Students[i].calculateTotalScore()) {
            num++;
        }
    }

    cout << num;
}

Enter fullscreen mode Exit fullscreen mode
  • First, we take the number of students n.
  • We create a vector of Student objects.
  • For each student:
  1. We take input for their scores.
  2. We compare their total score with that of the first student.
  3. if it’s greater, we increment num.
  • Finally, we print how many students scored more than the first student.

Sample Input/ output

Input:

4
100 50 40 80 70
30 40 100 90 80
60 60 60 60 60
70 70 70 70 70

Output:
2
Enter fullscreen mode Exit fullscreen mode

Explanation: First student’s total score = 340.
Student 2 = 340 → not greater.
Student 3 = 300 → less.
Student 4 = 350 → greater.

Only Student 4 scored more → output is 1.

🎯 Learning Outcomes

Writing this program helped me understand some key concepts in C++ that were initially confusing but became clearer as I practiced more. Here’s what I learned:

✅ How to use arrays inside a class: Initially, I wasn’t sure how to declare and access an array as a class member, but implementing scores[5] inside the Student class made it clear.

✅ Objects can be stored in arrays or vectors: I learned that we can create an array or vector of objects like vector Students(n);. This was new and slightly confusing at first because we often only work with single objects, not collections of them.

✅ How vectors work with classes: Using a vector to manage multiple objects made the program more flexible and dynamic. It felt tricky at first, but I now understand how powerful it is when dealing with multiple instances of a class.

✅ Logical comparisons become easier with OOP: Initially, I thought I’d have to compare every student with every other student. But thankfully, the problem only asked me to compare each one with the first student — which simplified the logic a lot and helped me focus more on the object-oriented part.

This task gave me a real confidence boost in using OOP with arrays and vectors, and it made me realize how clean and manageable code becomes once you start thinking in terms of objects rather than just variables.

Top comments (0)