Class Member's Pass

Hello! I found this example online and am trying to play around with it.

I know it is possible to get the sum of the array in the PutData() member function, but how can you pass this to the main () 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.

I have been successful at calling the members in main and outputting the sum, but how can I assign it to a new variable to use in main () using best practices?

Thank you in advance

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
#include<iostream>

using namespace std;


       class Employee
       {
              int Id;
              char Name[25];
              int Age;
              long Salary;

              public:
              void GetData()      
              {
                    cout<<"\n\tEnter Employee Id : ";
                    cin>>Id;

                    cout<<"\n\tEnter Employee Name : ";
                    cin>>Name;

                    cout<<"\n\tEnter Employee Age : ";
                    cin>>Age;

                    cout<<"\n\tEnter Employee Salary : ";
                    cin>>Salary;
              }

              void PutData()           
              {
                    sum = id + age;
                    cout<<"\n"<<Id<<"\t"<<Name<<"\t"<<Age<<"\t"<<Salary;
                    cout << sum;
              }

       };


       int main()
       {

              int i;

              Employee E[3];           

              for(i=0;i<3;i++)
              {
                    cout<<"\nEnter details of "<<i+1<<" Employee";
                    E[i].GetData();
              }

              cout<<"\nDetails of Employees";
              for(i=0;i<3;i++)
              E[i].PutData();

       }



Last edited on
First, your program has syntax errors:
 In member function 'void Employee::PutData()':
31:21: error: 'sum' was not declared in this scope
31:27: error: 'id' was not declared in this scope
31:32: error: 'age' was not declared in this scope


That aside,
Look at the first example in http://www.cplusplus.com/doc/tutorial/functions/
Q. What does that function do?
A. One thing: computes a value. The caller receives that value.


Q. What does your PutData do?
A. Two things:
1. Computes a value
2. Displays something

The function could return a value.
Registered users can post here. Sign in or register to post.