OOAD Functions Q/A #5


Question:Discuss constructor and destructor with the help of example. Describe the importance of constructors and destructors.

Answer:

Constructor

A constructor can be defined as a special member function which is used to initialize the objects of the class with initial values. It is special member function as its name is the same as the class name. It enables an object to initialize itself during its creation. This is termed as automatic initialization of objects.

Destructor

Destructor are member functions which are invoked when an object is no more required. Destructors are complements to constructors. These are used to release objects when they are destroyed. A destructor is called when an object of the class goes out of scope or when the memory space used by it is deallocated with the help of delete operators.

Important points about constructors and destructors

  • Constructor and destructor functions are member functions of a class with predefined syntax.

  • Constructor function get invoked when an object of a class is constructed and destructor function get invoked when the object is destructed or it goes out of scope.

  • Destructor function name is same as that of a constructor but the name is proceeded by a tilde ('>') symbol.

  • Both constructor and destructor functions do not have any data type (not even void).

  • These function do not return any value.

  • Constructor functions can accept arguments but destructor can not.

Example

The example below illustrates how constructor and destructor functions are defined:

class myclass
{
   private
      int number;
   public:
      myclass ( ) // constructor 
      {
         number = 10:
      }
      ~myclass ( )  //destructor
      {
      }
};

Program example using constructor and destructor functions:

#include<iostream>
using namespace std;
class myclass
{
   private :
      int a;
      int b;
   public :
      myclass()
      {
         a= 10;
         b= 20;
      }
      ~myclass ( )
      {
         cout <<"Destructor is executed";
      }
      int add ( )
      {
         return (a+b);
      }
};
int main ( )
{
   myclass c;
   cout <<" Result ="<<c.add( )<<endl;
   return 1;
}

Output

Result =30
Destructor is executed.

Importance of constructors

  • Constructors are used to initialize the objects of the class with initial values.

  • Constructors are invoked automatically when the objects are created.

  • Constructors can have default parameters.

  • If constructor is not declared for a class , the C++ compiler generates a default constructor.

  • A constructor can be used explicitly to create new objects of its class type.

  • The constructor is executed automatically.

  • Constructor function can be overloaded.

Importance of destructors

  • Destructors are invoked automatically when the objects are destroyed.

  • Destructors can not have any parameter and will not return any value.

  • Destructors are declared in a program to release memory space for future utilization.

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements