title: C++ Pass Member to Main () as variable
body: <p>I found this example online. I know it is possible to get the sum of the array in the <code>PutData()</code> member function, but how can you pass this to the <code>main()</code> to use as a variable properly? I understand it is probably best to have a member function for sum, but for simplicity, I think you should understand what I am trying to do.</p>
  
  <p>I have been successfully at calling the members in main, but how can I assign it to a new variable to use in <code>main()</code>? </p>
  
  <p>Thank you in advance </p>
  
  <pre><code>#include&lt;iostream&gt;
  
  
  class Employee
  {
      int Id;
      char Name[25];
      int Age;
      long Salary;
  
      public:
      void GetData()      
      {
          cout&lt;&lt;"\n\tEnter Employee Id : ";
          cin&gt;&gt;Id;
  
          cout&lt;&lt;"\n\tEnter Employee Name : ";
          cin&gt;&gt;Name;
  
          cout&lt;&lt;"\n\tEnter Employee Age : ";
          cin&gt;&gt;Age;
  
          cout&lt;&lt;"\n\tEnter Employee Salary : ";
          cin&gt;&gt;Salary;
      }
  
      void PutData()           
      {
          sum = id + age;
          cout&lt;&lt;"\n"&lt;&lt;Id&lt;&lt;"\t"&lt;&lt;Name&lt;&lt;"\t"&lt;&lt;Age&lt;&lt;"\t"&lt;&lt;Salary;
          cout &lt;&lt; sum;
      }    
  };
  
  
  int main()
  {
      int i;
  
      Employee E[3];           
  
      for(i=0;i&lt;3;i++)
      {
          cout&lt;&lt;"\nEnter details of "&lt;&lt;i+1&lt;&lt;" Employee";
          E[i].GetData();
      }
  
      cout&lt;&lt;"\nDetails of Employees";
      for(i=0;i&lt;3;i++)
      E[i].PutData();
  }
  </code></pre>
  
  <p>EDIT: Thank you everyone for the help!
  I was able to get the sum of every employee individually with help from the comments.</p>
  
  <p>I created a different member </p>
  
  <pre><code>int GetSum()
  {
  return Age + Id;
  }
  </code></pre>
  
  <p>and my main is:</p>
  
  <pre><code>int sum1 = P[0].GetSum();
  cout &lt;&lt; "\nSUM OF TEAM 1:  " &lt;&lt; sum1;
  
  int sum2 = P[1].GetSum();
  cout &lt;&lt; "\nSUM OF TEAM 2:  " &lt;&lt; sum2;
  </code></pre>