Constructors and Destructors

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:
// example: class constructor
#include <iostream.h>


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

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

int main () {
  CRectangle rect (3,4);
  CRectangle rectb (5,6);
  cout << "rect area: " << rect.area() << endl;
  cout << "rectb area: " << rectb.area() << endl;
  return 0;
}
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.



Destructor: 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. 

// example on constructors and destructors
#include <iostream.h>


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;
}

Comments

Popular posts from this blog

Program for calculating Simple Interest using the concept of Default Arguments

Program: Maximum number in three numbers in visual basic

Program for Checking whether the given number is Palindrome or not