Constructor vs destructor ??? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 8

Constructor vs destructor ???

What's the functional difference between constrctor and destructor in a program?

1st Mar 2018, 3:01 AM
Sankalp Dayal
Sankalp Dayal - avatar
8 Answers
+ 29
Sometimes it is required to initialize some part of an object before it can be utilized. For example, we are operating on the stack, before we perform any action, the top of the stack must always be set to zero. This feature of automatic initialization is performed through ‘Constructor’. Likely, if an object needs to execute some code before it is destroyed. For example, if an object needs to close a file that it had opened, before its destruction. It can be performed with the help of ‘Destructor’. Has same name as class but have ~ before it
1st Mar 2018, 4:04 AM
Shams Awais
Shams Awais - avatar
+ 5
class Boy{ //constructor Boy(){ } //destructor ~Boy(){} } when an object is initialized constructor is called Boy brains=new Boy(); destructor are automatically called from what I know
1st Mar 2018, 4:11 AM
᠌᠌Brains[Abidemi]
᠌᠌Brains[Abidemi] - avatar
+ 4
constructors and destructors work against each other. while constructor creates or initializes, destructors deletes or diminishes a class.
24th Jul 2018, 4:16 PM
Enock Acquah
Enock Acquah - avatar
+ 3
A constructor runs when the class is initiated and the deconstructor runs when the class is uninitiated
2nd Mar 2018, 4:10 AM
Ben Allen (Njinx)
Ben Allen (Njinx) - avatar
+ 3
Working: Constructor 1.Constructor is a special member function whose task is to initialise an object of it's class. 2.constructor can accept arguments. ~ Destructor 1.Destructor is a special member function whose task is to destroy the object created by constructor. 2. Destructor cannot accept arguments.
24th Apr 2018, 12:49 PM
Jayesh Patil
Jayesh Patil - avatar
+ 2
A constructor is used to initiate the class member and destructor is use to destroy the scope of object .
3rd Mar 2018, 2:01 PM
Sonu Sharma
Sonu Sharma - avatar
+ 1
Constructor is called when the object is created into the memory and destructor is called when object is deleted from the memory
18th Jun 2018, 5:36 AM
KIRTI GUPTA
KIRTI GUPTA - avatar
0
ok got it.but in actual code how constructor and disconstructor works?
2nd May 2018, 2:29 PM
Abhijeet Ghan
Abhijeet Ghan - avatar