What is defference between constructor and destructor

Questions by anil_mca

Showing Answers 1 - 9 of 9 Answers

sumanjit

  • Sep 15th, 2005
 

Constructor is called when a object is createdand Destructor is called when object goes out of scopeConstructor. is used to initilize the valuesDestructor. is uised to free the memory --

  Was this answer useful?  Yes

priya

  • Mar 5th, 2006
 

constructor: it is used to construct the object of the class.it is called when object of the first class is instansiated.destructor: it is used while destroying the object.it is called automatically when close braces is encontered.

  Was this answer useful?  Yes

constructor is the memeber function of the class which has the same name as that of class and it is invoked whenever the class object is instantiated.

destructor is also the member function  of same class name and has ~ operator when ever declared in the function and it is used to destruct the object which has been constructed ,whenever we want to destroy it..

jyotsna

  • Jul 7th, 2007
 

Constructor is a member of the class. Constructor is cerated automatically when class is called. Using construtor we can allocate memory.

Destructor is used for deallocate(release) the memory.

YaelG

  • Aug 3rd, 2008
 

Both Constructor and Destructor are member functions of the class. Both are made by the language defaultly, and can be override by the programmer. The constructor is being called when a new object of the class is created- it can be used to initiate members, to call functions and what ever is needed when an instance of the class is being made. The destructor is a function that is responsible for avoiding memory leak when deleteting an object. It's important to put a virtual befor constructors of base classes in order to avoid memory leak.

  Was this answer useful?  Yes

vsvraju

  • Aug 8th, 2008
 

Constructor builds the object from the scratch, where as destrcutor give the object its last rights (like, for getting back the allocated memory). Constructor gets called when the object was created and destructor gets called automatically when the object goes out of scope.

  Was this answer useful?  Yes

rlee2008

  • Jan 17th, 2009
 

A constructor of a class is mainly responsibly for turning the raw memory allotted to an object into a usable object.
DEFAULT CONSTRUCTOR:
The default constructor is a constructor that takes no argument.
Part::Part()
String() and String(const char* str) can be combined into a single constructor that has a default argument: String(const char* str=0)

for every class there can be only one default constructor.

COPY CONSTRUCTOR:
A copy constructor is a special constructor that can be called to copy an object. The copy constructor for class Part has the form
Part::Part(const Part&). 

DESTRUCTOR:
Whenever an object goes out of scope it is destroyed and the memory used by that particular object will be reclaimed. 
Part::~Part().

  Was this answer useful?  Yes

constructor is the member of a class which is called implicitly when the instance of class is created. It allocates the memory to data.
Same as destrutor is also a member of a class. It is called implicitly at the end of execution of class. It deallocates the memory.

  Was this answer useful?  Yes

Like constructor, the destructor is a member function whose name is the same as the class name but is preceded by a tilde. For example the destructor of a class integer can be define as :-


~integer(){}

A destructor never takes any argument nor does it return any value. It will invoked implicitly by the compiler upon exit from the program to clean up storage that is no longer accessible.

  Was this answer useful?  Yes

Give your answer:

If you think the above answer is not correct, Please select a reason and add your answer below.

 

Related Answered Questions

 

Related Open Questions