Constructors and Destructors

Constructors: Constructors are methods which are used to initialize an object at its definition time. We extend our class Point such that it initializes a point to coordinates (0, 0):

  class Point {
    int _x, _y;
  public:
    Point() {
      _x = _y = 0;
    }
    void setX(const int val);
    void setY(const int val);
    int getX() { return _x; }
    int getY() { return _y; }
  };
Constructors have the same name of the class (thus they are identified to be constructors). They have no return value. As other methods, they can take arguments. For example, we may want to initialize a point to other coordinates than (0, 0). We therefore define a second constructor taking two integer arguments within the class:


  class Point {
    int _x, _y;
    
  public:
    Point() {
      _x = _y = 0;
    } 
    Point(const int x, const int y) {
      _x = x;
      _y = y;
    }
    
    void setX(const int val);
    void setY(const int val);
    int getX() { return _x; }
    int getY() { return _y; }
  };

Constructors are implicitly called when we define objects of their classes:

  Point apoint;           // Point::Point()
  Point bpoint(12, 34);   // Point::Point(const int, const int)

Constructor having no arguments is reffered as default constructor.


If we want to create a point from another point, hence, copying the properties of one object to a newly created one, we sometimes have to take care of the copy process. For example, consider the class List which allocates dynamically memory for its elements. If we want to create a second list which is a copy of the first, we must allocate memory and copy the individual elements. In our class Point we therefore add a third constructor which takes care of correctly copying values from one object to the newly created one:

 
  class Point { 
    int _x, _y; 
     
  public: 
    Point() {
      _x = _y = 0; 
    }  
    Point(const int x, const int y) {
      _x = x;
      _y = y;
    }
    Point(const Point &from) {
      _x = from._x;
      _y = from._y;
    }
     
    void setX(const int val);
    void setY(const int val); 
    int getX() { return _x; }
    int getY() { return _y; } 
  };

The third constructor takes a constant reference to an object of class Point as an argument and assigns _x and _y the corresponding values of the provided object.

This type of constructor is called  copy constructor. It is highly recommended that you provide for each of your classes such a constructor, even if it is as simple as in our example. The copy constructor is called in the following cases:

  Point apoint;            // Point::Point()
  Point bpoint(apoint);    // Point::Point(const Point &)
  Point cpoint = apoint;   // Point::Point(const Point &)


Destructors: Destructors are also special member functions used in C++ programming language. Destructors have the opposite function of a constructor. The main use of destructors is to release dynamic allocated memory. Destructors are used to free memory, release resources and to perform other clean up. Destructors are automatically named when an object is destroyed. Like constructors, destructors also take the same name as that of the class name.

General Syntax of Destructors

~classname();

The above is the general syntax of a destructor. In the above, the symbol tilda ~ represents a destructor which precedes the name of the class.

Some important points about destructors:

   * Destructors take the same name as the class name.
   * Like the constructor, the destructor must also be defined in the public. The destructor must be a public member.
    * The Destructor does not take any argument which means that destructors cannot be overloaded.
    * No return type is specified for destructors.

Example: Sample Code


      class Exforsys{
          private:
              ...
          public:
             Exforsys(){ }
             ....
             ~Exforsys(){ }
 
      }



No comments:

Post a Comment