walk me through this please?

hey guys, i need a little bit of help in classes (section 1).
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
// example on constructors and destructors
#include <iostream>
using namespace std;

class CRectangle {
    int *width, *height;
  public:
    CRectangle (int,int);
    ~CRectangle ();
    int area () {return (*width * *height);}
};

CRectangle::CRectangle (int a, int b) {
  width = new int;
  height = new int;
  *width = a;
  *height = b;
}

CRectangle::~CRectangle () {
  delete width;
  delete height;
}

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


anyone care to tell me whats happening as we do CRectangle rect (3,4)? having all them pointers and "new int" etc put in has thrown me off a little.

thanks!
new is a c++ keyword that allocates memory for you and returns a pointer to it. So, what the Crectangle() does is allocate an integer for width and an integer for height + asociates the pointer with those memory locations. Later, when we do *width and *height, * makes us acess the memory pointed by the variable, we ve're acessing an int.

I guess they used pointers to show you another example of pointer use; this could be done just as eazily by

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
// example on constructors and destructors
#include <iostream>
using namespace std;

class CRectangle {
    int width, height;
  public:
    CRectangle (int,int);
    ~CRectangle ();
    int area () {return (width * height);}
};

CRectangle::CRectangle (int a, int b) {
  width = a;
  height = b;
}

CRectangle::~CRectangle () {
}

int main () {
  CRectangle rect (3,4), rectb (5,6);
  cout << "rect area: " << rect.area() << endl;
  cout << "rectb area: " << rectb.area() << endl;
  return 0;
ooo i see. i looked back over it, read the chapter on pointers again, and it all makes sense. helps keep the memory clear and improves the efficiency huh? guess ill never see much happen in my small "Area of a rectangle" program huh, but one day, one day! lol.

anyway, i understand now. one thing im wondering is when is when is an object destroyed? an example maybe? (using the example above)
I'm a beginner but my understanding that an object is destroyed (or its memory is deallocated) either when it goes out of scope or when you call delete on it. When this happens, if it has a destructor, such as yours above: CRectangle::~CRectangle(); then it that is called and its memory is deallocated. For example:

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

class CRectangle {
public:
    CRectangle(int, int);
    ~CRectangle();
    int area() { return *width * *height; }
private:
    int *width, *height;
};

CRectangle::CRectangle(int w, int h) {
    width = new int(w);
    height = new int(h);
}

CRectangle::~CRectangle() {
    delete width;
    delete height;
}

int main() {
    CRectangle cr1(5, 4);
    CRectangle cr2(3, 4);
    delete &cr1; // destructor called and memory
                 // deallocated
    
    return 0;
} // cr2 gone out of scope, destructor
  // called and memory deallocated 
ooo thanks buddy. lastly, and sort of unrelated...:
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
// pointer to classes example
#include <iostream>
using namespace std;

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

void CRectangle::set_values (int a, int b) {
  width = a;
  height = b;
}

int main () {
  CRectangle a, *b, *c;
  CRectangle * d = new CRectangle[2];
  b= new CRectangle;
  c= &a;
  a.set_values (1,2);
  b->set_values (3,4);
  d->set_values (5,6);
  d[1].set_values (7,8);
  cout << "a area: " << a.area() << endl;
  cout << "*b area: " << b->area() << endl;
  cout << "*c area: " << c->area() << endl;
  cout << "d[0] area: " << d[0].area() << endl;
  cout << "d[1] area: " << d[1].area() << endl;
  delete[] d;
  delete b;
  return 0;
}


in the main, look at b specifically. b is a pointer, which points to a CRectangle object? where is the object? for example, if we do CRectangle a i know that the "a" variable 'holds' this new object, right? by doing b= new CRectangle; are we simply creating a new CRectangle object in the memory, and accessing it by a pointer? (just like we created the new int into the memory in the example you posted above this?).

i can see how it all works and i can get it to work..i just want to understand how its all working thats all. :)
Last edited on
when you do CRectangle a, the compiler allocates space, when you make a pointer, however, the compiler creates a pointer object (4B on x84, same as sizeof(int)) which holds an adress. Than, you allocate memory via new operator, and you get an adress. Think of it as buying land and later placing a building on it, but when you let the compiler allocate it for you you just get the building.
oh ok.

so then why does deleteing the pointer to the bit of memory that now holds our object also removes the object itself from memory? are they somehow attached to each other when we create a new object in this way? because from what i understand, unless we have another pointer and give it the same address as this object, the object becomes inaccessible. if we had

1
2
3
4
CRectangle *a,*b;

a = new RCectangle();
b = &a;


we can use both a and b to access the same object, in the same way, right? (a->area(); and b->area(); would return the same thing). if we then did:

delete a;

would the object a and b were both pointing at (only b now) still exist? and if so, i conclude that this sort of object is only every removed from memory if all pointers to it are deleted. (would i be correct to conclude that?)
Last edited on
The object a and b are pointing to are the same (see it as a reference if you may)
So:
delete a;
would be the same as
delete b;

And the objects would no longer exist hence both a and b would be pointing to memory out of bounds.
Last edited on
I think you wanted to do this:
1
2
3
4
CRectangle *a,*b;

a = new RCectangle();
b = a;


Just imagine pointers to be normal ints. operator & gets the address of a variable, and since a is a pointer you dont need to know where it is, but where is the object it points to - its value.

when you deleted a you didnt delete the land, but you demolished the building - the same building that was on land b. Its wierd with lands here, so imagine an address of an building instead. One building had two adresses, and you ordered a demolition company to delete a building on adress a. There is nothing left on that adress now, and since b was used for that building too, it has nothing to point to now. Note that a still contains the address where the building once used to be, but that doesnt mean that you can build another one there, because you dont controll where the building company (new) builds, they only tell you where they've build the object you requested.

So, if you were to

1
2
3
4
5
6
CRectangle *a,*b;

a = new CRectangle();
b = a;
delete a;
a = new CRectangle();


B would still point to the old adress and would be useless. You'd have to do
b = a;

again.
Topic archived. No new replies allowed.