Classes and Objects

Sort by

recency

|

418 Discussions

|

  • + 0 comments

    include

    include

    include

    include

    include

    using namespace std; class Student{ private: int scores[5]; public:

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

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

    int main() { int num; cin>>num; Student students[num]; int count = 0;

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

    count++;

    }
    
    cout<<count;
    

    }

  • + 0 comments

    class Student{ public : int score[5] , sum=0; void input(){ for(int i=0;i<5;i++){ cin>>score[i]; } } int calculateTotalScore(){ for(int i=0;i<5;i++){ sum+=score[i]; } return sum; } };

  • + 0 comments

    My C++ code

    class Student {
    
    public:
        void input() {
            for(int & score : scores) {
                cin >> score;;
            }
        }
    
        int calculateTotalScore() {
            int total = 0;
            for(int i = 0;i < 5;i++) {
                total += scores[i];
            }
            return total;
        }
    private:
        int scores[5] = {0};
    };
    
  • + 0 comments

    **For Those Wants To Execute This Code Without Using Vector **

    #include <iostream>
    using namespace std;
    
    // Define the Student class
    class Student {
    private:
        int scores[5]; // Array to hold scores (fixed size for 5 subjects)
    
    public:
        // Function to read scores from input
        void input() {
            for (int i = 0; i < 5; i++) {
                cin >> scores[i];
            }
        }
    
        // Function to calculate the total score
        int calculateTotalScore() {
            int totalScore = 0;
            for (int i = 0; i < 5; i++) {
                totalScore += scores[i];
            }
            return totalScore;
        }
    };
    
    int main() {
        int n; // Number of students
        cin >> n;
    
        Student* students = new Student[n]; // Dynamically allocate array of students
    
        // Input scores for each student
        for (int i = 0; i < n; i++) {
            students[i].input();
        }
    
        // Calculate Kristen's total score (first student)
        int kristenScore = students[0].calculateTotalScore();
    
        // Count how many students scored higher than Kristen
        int count = 0;
        for (int i = 1; i < n; i++) {
            if (students[i].calculateTotalScore() > kristenScore) {
                count++;
            }
        }
    
        // Output the result
        cout << count << endl;
    
        // Clean up dynamic memory
        delete[] students;
    		
    		
    		
    
        return 0;
    }
    
  • + 0 comments
    class Student
    {
    
    private: 
        vector<int> scores; 
    
    public: 
        void input()
        {
            int a, b, c, d, e; 
            cin >> a >> b >> c >> d >> e; 
            scores = { a, b, c, d ,e }; 
        };
        int calculateTotalScore()
        {
            return accumulate(scores.begin(), scores.end(), 0);
        }; 
    
    };