title: Virtual Functions C++ Hacker rank challenge
body: <p>I'm new to c++ and I was trying to develop my skills through solving challenges on Hacker Rank.</p>
  
  <p>This is the challenge I was working on 
  <a href="https://www.hackerrank.com/challenges/virtual-functions" rel="nofollow noreferrer">https://www.hackerrank.com/challenges/virtual-functions</a></p>
  
  <p>My solution for the challenge is</p>
  
  <pre><code>class Person {
  protected: 
      int age;
      string name;
  
  public:
      virtual void getdata() {
      }   
      virtual void putdata() {
      }       
  };
  
  class Student: public Person{
  protected: 
      int marks[6];
      int sum = 0;
      static int cur_id2;
  
  public:
      void getdata() {
          cin &gt;&gt; name &gt;&gt; age;
  
          for(int i = 0; i &lt; 6; i++) {
              cin &gt;&gt; marks[i] ;
              sum = sum + marks[i];
          } 
      }    
      void putdata() {
          cout &lt;&lt; name &lt;&lt;" "&lt;&lt; age&lt;&lt;" " &lt;&lt; sum &lt;&lt;" "&lt;&lt; cur_id2 &lt;&lt; endl;
      } 
      Student() {
          cur_id2++;
      }
  };
  
  int Student::cur_id2 = 0;
  
  class Professor: public Person {
  protected: 
      int publications;
      static int cur_id1; 
  
  public: 
      void getdata() {
          cin &gt;&gt; name &gt;&gt; age &gt;&gt; publications;
      }
      void putdata() {
          cout &lt;&lt; name &lt;&lt;" "&lt;&lt; age &lt;&lt;" "&lt;&lt; publications&lt;&lt;" " &lt;&lt; cur_id1 &lt;&lt; endl;
      }  
  
      Professor() {
          cur_id1++;  
      }
  };
  
  int Professor::cur_id1 = 0;
  </code></pre>
  
  <p>I got those results :</p>
  
  <pre><code>Your Output 
  
  Walter 56 99 2
  Jesse 18 403 2
  Pinkman 22 135 2
  White 58 87 2
  
  Expected Output
  
  Walter 56 99 1
  Jesse 18 403 1
  Pinkman 22 135 2
  White 58 87 2
  </code></pre>
  
  <p>I think the problem with the id is that in the main function it always gets data for all objects before it prints it this mean that the id variable will always get the last value after creating the last object so I think this way will not make sense I need another way to assign an id for each new instance of the class and keep it somewhere in order to use it when the putdata function is called . I thought of using an array to keep all ids but I don't think it's the right way to solve this problem please help me. 
  Thank you</p>
  
  <p>Edit:</p>
  
  <p>Thank you all your answers helped me solving the challenge I just modified small pieces of code</p>
  
  <pre><code> protected: 
     int publications;
     static int next_id1; 
  
    int cur_id1; 
  
  
  
  
    Professor(){
  
          cur_id1=++next_id1;  
      }
  
  };int Professor::next_id1=0;
  </code></pre>
  
  <p>And the same in the Student class.</p>