Classes and Objects

Sort by

recency

|

417 Discussions

|

  • + 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);
        }; 
    
    };
    
  • + 0 comments
    1. include

      using namespace std; class Student{ private: int q1,q2,q3,q4,q5; public: void input(){ cin >> q1 >> q2 >> q3 >> q4 >> q5; } int calculateTotalScore(){ int score; score += q1+q2+q3+q4+q5; return score; }}; int main() { int n; // number of students cin >> n; Student *s = new Student[n]; for(int i = 0; i < n; i++){ s[i].input(); } int kristen_score = s[0].calculateTotalScore(); int count = 0; for(int i = 1; i < n; i++){ int total = s[i].calculateTotalScore(); if(total > kristen_score){ count++; } } cout << count; return 0; }