What is the difference between constructor and destructor in C++?

+4 votes
16,760 views asked Jun 10, 2010 by anonymous
edited Dec 15, 2010 by pa

4 Answers

0 votes
 
Best answer

Constructor:

  • Constructor is Used to Initialize the Object.
  • Constructor can takes arguments.
  • Constructor overloading can be possible means more than one constructors can be defined insame class.
  • Constructor has same name as class name.
  • Syntax of constructor:

                class class_name
                  {
                     clas_sname(){}
                     class_name(argulist){}
                 } ;

 

  • Constructor are of following:

               1)Default Constructor.
               2)Parameterized Constructor.
               3)Copy Constructor.
 

  • Constructors can be used to dynamically initialize the memory.
  • Constructor indirectly use the New operator to initialize the object.


Destructor:

  • Destructor is used to destroy the object that are created in memory previously.
  • Destructor can not take any arguments.
  • Destructor overloading can not be possible.
  • Destructor has same name as class name with tiled operator.
  • Syntax of Destructor:

                 class class_name
                   {

                       ~class-name(void){}
                   };
 

  • Destructor has no any types.
  • Destructor can be used to deallocate the memory.
  • Destructor indirectly use the Delete operator to destroy the object initialize by constructor.
     
answered Nov 17, 2011 by pa Sr Member (3,640 points)
Thanks for such a nice explanation..
thanks...cover entire differences ..hope this will come in paper 2morow
nice one
i like it..!
thanku so much....
thanx a lott... it really helped me...
thank you  for this to me .
awesome answer ...thnks a lot...
thnxs very muchh.... for simple understandable format ..
thnqq......!!
it really help me and nice
thank u :)
0 votes
the main difference is that constructor constructs the values of an object and aA destructor destructs the values created by the constructor for the object.
answered Oct 25, 2010 by anonymous
0 votes
* A constructor is called when you want to create a new instance of a class. A destructor is called when you want to free up the memory of an object (when you delete it).

 * A constructor constructs the value of an object . A destructor destructs the value created by the constructor for the object.

    Another differentiation is their syntax's :

constructor :

<Class_name>(Arguments)

{

//body of constructor.

}



destructor :

~<class_name>()

{

}
answered May 19, 2011 by anonymous
** constructor  IS CALLED IN THE BEGINNING OF THE PROGRAMM AND **DESTRUCTOR IS CALLED AT THE ENDING OF THE PROGRAM.

**CONSTRUCTER CAN BE OVERLOADED
**DESTROCTER CN NOT BE OVERLOADED
hey i found this great read on this link....... i was searching it for my college exam... :) hope this helps.
 http://freefeast.info/general-it-articles/difference-between-constructor-and-destructor-constructor-vs-destructor/

superb effort by the user
0 votes

hey i found this great read.....i was searching it for my college exam... smiley hope this helps.

http://freefeast.info/general-it-articles/difference-between-constructor-and-destructor-constructor-vs-destructor/

superb effort by the admin or writer. Thanks to them.

answered Aug 9, 2012 by anonymous

Related questions

+1 vote
2 answers 394 views
0 votes
1 answer 295 views
0 votes
1 answer 18 views
18 views asked Jun 9, 2010 by anonymous
+1 vote
1 answer 221 views
0 votes
1 answer 122 views
...