Doubt with Classes

Could anybody tell me why at the end of the code, x and y are valued 12 and 32, since the member function "Lee" is void type and besides is executed in the scope of pareja?? Shouldn't they be their initial values??? Thanks a lot.

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
  #include <iostream> 
using namespace std;
 
class pareja {
   public:
      // Constructor
      pareja(int a2, int b2);
      // Funciones miembro de la clase "pareja"
      void Lee(int &a2, int &b2);
      void Guarda(int a2, int b2);
   private:
      // Datos miembro de la clase "pareja"
      int a, b; 
};

pareja::pareja(int a2, int b2) {
   a = a2;
   b = b2;
}

void pareja::Lee(int &a2, int &b2) {
   a2 = a;
   b2 = b;
}

void pareja::Guarda(int a2, int b2) {
   a = a2;
   b = b2;
}

int main() {
   pareja par1(12, 32);
   int x, y;
   
   par1.Lee(x, y);
   cout << "Valor de par1.a: " << x << endl;
   cout << "Valor de par1.b: " << y << endl;
   
   return 0;
}.
Because a2 and b2 are passed by reference which basically means that they are actually the variable that was passed, rather than being a copy.
Thanks Shadowmouse, I still have to check the basics.

Goodbye.
Topic archived. No new replies allowed.