Classes and Objects

Sort by

recency

|

423 Discussions

|

  • + 0 comments
    class Student {
    private:
        int scores[5];
        int totalScore = 0;
        
    public:
        void input()
        {
            for (int i = 0; i < 5; i++)
            {
                std::cin >> scores[i];
                totalScore += scores[i];
            }
        }
        
        int calculateTotalScore()
        {
            return totalScore;
        }
    };
    
  • + 0 comments
    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& score : scores) {
                    sum += score;
                }
                
                return sum;
            }
    };
    
  • + 0 comments

    class Student{ private: int scores[5]; public: void input(){ for(int i=0;i<5;i++){ cin>> scores[i]; } } int calculateTotalScore(){ int result = 0; for(int i=0;i<5;i++){ result += scores[i]; } return result; } }; /

  • + 0 comments
    #include <cmath>
    #include <cstdio>
    #include <iostream>
    using namespace std;
    
    class Student{
        private:
            int scores[5]{};
        public:
            void input(){
                for (int i{}; i<5; i++) {
                    std::cin >> scores[i];
                }
            }
            int calculateTotalScore(){
                int total{};
                for (int i{}; i<5; i++) {
                    total += scores[i];
                }
                return total;
            }
    };
    
    int main() {
        unsigned int n;
        std::cin >> n;
        Student st[n];
        int n_g{};
        for (size_t i{}; i<n; i++) {
            st[i].input();
            (st[i].calculateTotalScore()>st[0].calculateTotalScore()) ? n_g++ : false;     
        }
        std::cout << n_g;
        return 0;
    }
    
  • + 0 comments

    Here is Classes and Objects problem solution in C++ - https://programmingoneonone.com/hackerrank-classes-and-objects-solution-in-cpp.html