## Tuesday, October 11, 2011

### C++ Tutorial : Constructors and Destructors

Objects generally need to initialize variables or assign dynamic memory during their process of creation to become operative and to avoid returning unexpected values during their execution. For example, what would happen if in the previous example we called the member function area() before having called function set_values()? Probably we would have gotten an undetermined result since the members x and y would have never been assigned a value.
In order to avoid that, a class can include a special function called constructor, which is automatically called whenever a new object of this class is created. This constructor function must have the same name as the class, and cannot have any return type; not even void.
We are going to implement CRectangle including a constructor:
  1: // example: class constructor
2: #include <iostream>
3: using namespace std;
4:
5: class CRectangle {
6:     int width, height;
7:   public:
8:     CRectangle (int,int);
9:     int area () {return (width*height);}
10: };
11:
12: CRectangle::CRectangle (int a, int b) {
13:   width = a;
14:   height = b;
15: }
16:
17: int main () {
18:   CRectangle rect (3,4);
19:   CRectangle rectb (5,6);
20:   cout << "rect area: " << rect.area() << endl;
21:   cout << "rectb area: " << rectb.area() << endl;
22:   return 0;
23: }


As you can see, the result of this example is identical to the previous one. But now we have removed the member function set_values(), and have included instead a constructor that performs a similar action: it initializes the values of width and height with the parameters that are passed to it.
Notice how these arguments are passed to the constructor at the moment at which the objects of this class are created:

CRectangle rect (3,4);
CRectangle rectb (5,6);

Constructors cannot be called explicitly as if they were regular member functions. They are only executed when a new object of that class is created.
You can also see how neither the constructor prototype declaration (within the class) nor the latter constructor definition include a return value; not even void.
The destructor fulfills the opposite functionality. It is automatically called when an object is destroyed, either because its scope of existence has finished (for example, if it was defined as a local object within a function and the function ends) or because it is an object dynamically assigned and it is released using the operator delete.
The destructor must have the same name as the class, but preceded with a tilde sign (~) and it must also return no value.
The use of destructors is especially suitable when an object assigns dynamic memory during its lifetime and at the moment of being destroyed we want to release the memory that the object was allocated.
  1: // example on constructors and destructors
2: #include <iostream>
3: using namespace std;
4:
5: class CRectangle {
6:     int *width, *height;
7:   public:
8:     CRectangle (int,int);
9:     ~CRectangle ();
10:     int area () {return (*width * *height);}
11: };
12:
13: CRectangle::CRectangle (int a, int b) {
14:   width = new int;
15:   height = new int;
16:   *width = a;
17:   *height = b;
18: }
19:
20: CRectangle::~CRectangle () {
21:   delete width;
22:   delete height;
23: }
24:
25: int main () {
26:   CRectangle rect (3,4), rectb (5,6);
27:   cout << "rect area: " << rect.area() << endl;
28:   cout << "rectb area: " << rectb.area() << endl;
29:   return 0;
30: }