We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
  • Hackerrank Home
  • Prepare
    NEW
  • Certify
  • Compete
  • Career Fair
  • Hiring developers?
  1. Prepare
  2. C++
  3. Classes
  4. Classes and Objects
  5. Discussions

Classes and Objects

Problem
Submissions
Leaderboard
Discussions

Sort 317 Discussions, By:

votes

Please Login in order to post a comment

  • malrefai
    5 years ago+ 16 comments
    class Student {
      private:
        int scores[5];
        int sum;
      public:
        Student() : sum(0) {}
        int calculateTotalScore() {return sum;}
        void input() {
            for(int i=0; i<5; i++) {
                cin >> scores[i];
                sum+=scores[i];
            }
        }
    };
    
    74|
    Permalink
    View more Comments..
  • tigerleapgorge
    5 years ago+ 6 comments
    class Student{
        int scores[5];
        public:
        void input(){
            for(int i=0; i<5; i++){
                cin >> scores[i];
            }
        }
        int calculateTotalScore(){
            int total = 0;
            for(int i=0; i<5; i++){
                total += scores[i];
            }
            return total;
        }
    };
    
    38|
    Permalink
    View more Comments..
  • runcy
    4 years ago+ 3 comments

    The given template is prone to memory leaks as it's not using "delete" after the object is created with "new"

    28|
    Permalink
  • NiceBuddy
    4 years ago+ 5 comments

    however in the main there is a mem leak:

    class Student
    {
    private:
        std::vector<int> m_scores;
        int m_sum;
    public:
        Student()
            : m_scores(5,0), m_sum(0)
              {}
    
        ~Student(){}
    
        inline void input()
        {
            for(auto &it:m_scores)
            {
                int temp;
                std::cin>> temp;
                it = temp;
                m_sum += temp;
            }
        }
        const int& calculateTotalScore()const { return m_sum;   }
    };
    
    12|
    Permalink
    View more Comments..
  • [deleted] 6 years ago+ 0 comments

    It is generally bad practice to name a function something like Input() because the name of pretty much anything except a class/struct should start with a lowercase latter. Thus the functions we had to write should be input() and calculateTotalScore().

    11|
    Permalink
Load more conversations

Need Help?


View top submissions
  • Blog
  • Scoring
  • Environment
  • FAQ
  • About Us
  • Support
  • Careers
  • Terms Of Service
  • Privacy Policy
  • Request a Feature