Declaring objects of the class

Hello!
Please, we declared the objects in main:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
  #include <iostream>
using namespace std;

class Rectangle {
    int width, height;
  public:
    void set_values (int,int);
    int area () {return width*height;}
};

void Rectangle::set_values (int x, int y) {
  width = x;
  height = y;
}

int main () {
  Rectangle rect, rectb;
  rect.set_values (3,4);
  rectb.set_values (5,6);
  cout << "rect area: " << rect.area() << endl;
  cout << "rectb area: " << rectb.area() << endl;
  return 0;
}


Can we do it and/or in line 10:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <iostream>
using namespace std;

class Rectangle {
    int width, height;
  public:
    void set_values (int,int);
    int area () {return width*height;}
}rect, rectb;

void Rectangle::set_values (int x, int y) {
  width = x;
  height = y;
}

int main () {
 // ? Rectangle rect, rectb;
  rect.set_values (3,4);
  rectb.set_values (5,6);
  cout << "rect area: " << rect.area() << endl;
  cout << "rectb area: " << rectb.area() << endl;
  return 0;
}


Many thanks!!!
Both programs works. The difference is that in the second code rect and rectb are global variables and in the first code they are local variables in main().
Hello!
If they are global in first case, so as uninitialised, they are put into bss memory, then when they get initialised, they are in data memory?
What happens with their & and values in any case?

Many thanks!
I'm not sure about the exact implementation details but the C++ standard guarantees that in the second code the memory of rect and rectb are zero initialized.
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 Rectangle {
    int width, height;
  public:
    void set_values (int,int);
    int area () {return width*height;}
}rect;

void Rectangle::set_values (int x, int y) {
  width = x;
  height = y;
  cout<<&rect<<" a"<<endl;
}

int main () {
  Rectangle rect;
  cout<<&rect<<" b"<<endl;
  rect.set_values (3,4);
  //rectb.set_values (5,6);
  cout << "rect area: " << rect.area() << endl;
  //cout << "rectb area: " << rectb.area() << endl;
  return 0;
}



Output:

1
2
3

	

0xbf559364 b
0x8050b38 a
rect area: 12


So, first and second &rect are obviously not at the same part of the memory???
Hello!
This was made by codepad (online compiler).
I suppose, first (uninitialised) rect.widht and rect.height are in bss pat of memory,

and second (initialised) rect.width and rect.height are in data.

It means, we acutally have 2 variables with the same name, (rect.width first and second) but each one has different adress and different value.

Am I right?

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
58
59
#include <iostream>
using namespace std;

class Rectangle {
  public:
    int width, height;
  public:
    void set_values (int,int);
    int area () {return width*height;}
}rect/*, rectb*/;

void Rectangle::set_values (int x, int y) {
  width = x;
  height = y;
  cout<<&rect<<" a"<<endl;
}

int main () {
  cout<<endl<<endl;
  Rectangle rect/*,rectb*/;
  cout<<&rect<<" b"<<endl<<rect.width<<endl<<rect.height<<endl;
  rect.set_values (3,4);
  cout<<endl<<endl<<endl<<"After being initialised: "<<rect.width<<","<<rect.height<<endl;
  //rectb.set_values (5,6);
  cout << "rect area: " << rect.area() << endl;
  //cout << "rectb area: " << rectb.area() << endl;
  return 0;
}



Output:

1
2
3
4
5
6
7
8
9
10
11

	



0xbf8d837c b
134539157
134539008
0x8050bf8 a



After being initialised: 3,4
rect area: 12
Topic archived. No new replies allowed.