Go Back   TechArena Community > Software > Software Development
Become a Member!
Forgot your username/password?
Tags Active Topics RSS Search Mark Forums Read

Reply
 
Thread Tools Search this Thread
  #1  
Old 27-02-2009
Member
 
Join Date: Feb 2009
Posts: 10
What are the Constructors and Destructors?

Hi,

I want to know the concept of Constructors and Destructors. Anyone can explain me this in simple words?
Reply With Quote
  #2  
Old 27-02-2009
Member
 
Join Date: May 2008
Posts: 35
Re: What are the Constructors and Destructors?

Constructor and Destructor functions are Member Functions of a class having some special property.

Constructor function gets invoked when an object of a class is constructed (declared) and destructor function gets invoked when the object is destructed (goes out of scope).

Use of Constructor and Destructor function of a class
  • Constructor function is used to initialize member variables to pre-defined values as soon as an object of a class is declared.
  • Constructor function having parameters is used to initialize the data members to the values passed values, upon declaration.
  • Generally, the destructor function is needed only when constructor has allocated dynamic memory.

Defining Constructor and Destructor functions

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

Code:
  class myclass
  {
   private:
   int number;

   public:
   myclass()//constructor
   {
    number=10;
   }

   ~myclass()//destructor
   {
  //nothing needed
   }
  };
A few points to note:
  • Both of the functions have the same name as that of the class, destructor function having (~) before its name.
  • Both constructor and destructor functions should not be preceded by any data type (not even void).
  • These functions do not (and cannot) return any values.
  • We can have only the constructor function in a class without destructor function or vice-versa.
  • Constructor function can take arguments but destructors cannot.
  • Constructor function can be overloaded as usual functions.

Example 1: Using constructor function to initialize data members to pre-defined values

Code:
  //Example Program in C++
  #include<iostream.h>

  class myclass
  {
   private:
   int a;
   int b;

   public:
   myclass()
   {
   //here constructor function is used to
   //initialize data members to pre-def
   //values
   a=10;
   b=10;
   }

   int add(void)
   {
    return a+b;
   }
  };

  void main(void)
  {
   myclass a;

   cout<<a.add();
  }
Example 2: Using constructor function to initialize data members to values passed as arguments

Code:
  //Example Program in C++
  #include<iostream.h>

  class myclass
  {
   private:
   int a;
   int b;

   public:
   myclass(int i, int j)
   {
    a=i;
    b=j;
   }

   int add(void)
   {
    return a+b;
   }
  };

  void main(void)
  {
   //notice how the object of the class
   //has been declared
   //it can be thought as
   // myclass a;
   // a=myclass(10,20)
   myclass a(10,20);

   cout<<a.add();
  }
Notice that there is no destructor function in both the examples, just because we don?t need them.
Reply With Quote
  #3  
Old 27-02-2009
Member
 
Join Date: May 2008
Posts: 35
Re: What are the Constructors and Destructors?

When does the destructor function gets invoked

The examples below illustrates when the destructor function gets invoked:

Example 1:

Code:
  //Example Program in C++
  #include<iostream.h>

  class myclass
  {
   public:

   ~myclass()
     {
      cout<<"destructed\n";
     }
  };

  void main(void)
  {
   myclass obj;
   cout<<"inside main\n";
  }
OUTPUT:

inside main
destructed
Press any key to continue

As I said in the other article, destructors get invoked when the object of a class goes out of scope. In this case, the object goes out of scope as the program terminates. So the destructor gets invoked just before the program?s termination.

Example 2:

Code:
  //Example Program in C++
  #include<iostream.h>

  void myfunc(void);

  class myclass
  {
   public:

   ~myclass()
     {
      cout<<"destructed\n";
     }
  };

  void main(void)
  {
   cout<<"inside main\n";
   myfunc();
   cout<<"again inside main\n";
  }

  void myfunc(void)
  {
   cout<<"inside myfunc\n";
   myclass obj;
   cout<<"still inside myfunc\n";
  }
OUTPUT:

inside main
inside myfunc
still inside myfunc
destructed
again inside main
Press any key to continue

In this case, destructor function is invoked just as the program?s execution returns from the function, but before executing any further instruction from where it was called (main).

Example 3: In the following example we are creating a dynamically allocated object of a class in the same way as we did with the variables.

Code:
  //Example Program in C++
  #include<iostream.h>

  class myclass
  {
   public:

   ~myclass()
     {
      cout<<"destructed\n";
     }
  };

  void main(void)
  {
   myclass *obj;
   obj=new myclass;
   cout<<"inside main\n";

   delete obj;

   cout<<"still inside main\n";
  }
OUTPUT:

inside main
destructed
still inside main
Press any key to continue

Here the programmer is explicitly destroying the object, hence the destructor function is called.

Now that you know some details about the destructor function, let me give you a practical example of the use of destructor function:

Code:
  //Example Program in C++
  #include<iostream.h>

  class myclass
  {
   int *number;

   public:
   myclass(int num)
     {
      number=new int[num];
     }

   ~myclass()
     {
      delete []number;
     }

   void input_num(int index, int num)
     {
      number[index]=num;
     }

   int output_num(int index)
     {
      return number[index];
     }
  };

  void main(void)
  {
   int size, num;
   cout<<"enter number of elments: ";
   cin>>size;

   myclass obj(size);

   for(int i=0;i<size;i++)
     {
      cout<<"enter element "<<i+1<<":";
      cin>>num;
      obj.input_num(i,num);
     }

   cout<<"\nelements have the following values\n\n";

   for(i=0;i<size;i++)
     {
      cout<<"element "<<i+1<<":";
      cout<<obj.output_num(i);
      cout<<"\n";
     }
  }
Good-Bye!
Reply With Quote
  #4  
Old 27-02-2009
Member
 
Join Date: Apr 2008
Posts: 1,948
Re: What are the Constructors and Destructors?

A constructor is a member function that has the same name as its class (and which has no return value). E.g., there are four constructors declared in Program. The purpose of a constructor is to initialize an object. In fact, C++ makes the following promise: If a class has a constructor, all objects of that class will be initialized.

Consider the following sequence of variable declarations:

Code:
Complex c;       // calls Complex ()
Complex d = 2.0; // calls Complex (double)
Complex i(0, 1); // calls Complex (double, double)
Complex j(i);    // calls Complex (Complex const&)
Each variable declaration implicitly causes the appropriate constructor to be invoked. In particular, the equal sign in the declaration of d denotes initialization not assignment and, therefore, is accomplished by calling the constructor.

details
Reply With Quote
  #5  
Old 27-02-2009
Member
 
Join Date: May 2008
Posts: 2,293
Re: What are the Constructors and Destructors?

Fore more details understanding go through this!

http://publib.boulder.ibm.com/infoce...c15cplr374.htm

Hope this helps you!
Reply With Quote
Reply

  TechArena Community > Software > Software Development
Tags: , ,



Thread Tools Search this Thread
Search this Thread:

Advanced Search


Similar Threads for: "What are the Constructors and Destructors?"
Thread Thread Starter Forum Replies Last Post
Constructors in Java help cloud101 Software Development 4 18-01-2012 05:02 PM
Do Virtual Destructors exist in C++ ? Satchel Operating Systems 6 20-01-2011 10:25 PM
what are constructors in C++? Asis Software Development 4 29-12-2010 08:38 AM
Constructors and Destructors in PHP garfield1 Software Development 4 02-03-2010 11:01 PM
Does anyone know about virtual constructors and destructors? Ram Bharose Software Development 5 08-02-2010 01:57 PM


All times are GMT +5.5. The time now is 01:56 PM.