title: Reading a Text File and storing the data in Private member variables of a Class - C++
body: <p>I have a <code>myFile.txt</code> file and it contains the First Name, Last Name and an ID of a student in the following format,</p>
  
  <p><code>First-Name  Last-Name   ID</code> <strong>&lt;-- This line isn't included in the file</strong></p>
  
  <p><code>Steve        Smith    12345</code> <strong>&lt;-- This line is included in the file</strong></p>
  
  <p>I also have a class <code>Student</code> that looks like</p>
  
  <pre><code>class Student
  {
  private:
      std::string firstName;
      std::string lastName;
      int id;
  public:
      Student();
      Student(std::string fname, std::string lname, int i);
      ~Student();
  
      void setFirstName(std::string fnam);
      std::string getFirstName();
  
      void setLastName(std::string lnam);
      std::string getLastName();
  
      void SetID(int i);
      int getID();
  };
  </code></pre>
  
  <p>I want to read the <code>myFile.txt</code> file and store the information in the respective <strong>private member variables</strong>  of the student class.</p>
  
  <p>I know i have to overload the <code>&gt;&gt;</code> operator but i can't figure out how to store the data from the file in to respective variables when the variables are <strong>private</strong>.</p>
  
  <p>To write in to the file, i overloaded the <code>&lt;&lt;</code> operator as follows.</p>
  
  <pre><code>std::ostream&amp; operator &lt;&lt; (std::ostream &amp;out, Student &amp;stu)
  {
      out &lt;&lt; stu.getFirstName() &lt;&lt; " " &lt;&lt; stu.getLastName() &lt;&lt; " " &lt;&lt; stu.getID() &lt;&lt; std::endl;
      return out;
  }
  </code></pre>
  
  <p><strong>Question:</strong> How do i overload the <code>&gt;&gt;</code> operator so that data from the text file is stored in the respective variables  when the variables are <strong>private</strong> and can only be accessed using <code>setter</code> and <code>getter</code> functions ?</p>
  
  <pre><code>std::istream&amp; operator &gt;&gt; (std::istream &amp;in, Student &amp;stu)
  {
  
      in &gt;&gt; //what do i write here ? do i have to call the setter functions ? if so, how ?
      return in;
  }
  </code></pre>