

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Difference Between Constructor and Destructor
In this post, we will understand the difference between constructor and destructor.
Constructor
It helps allocate memory to an object.
It can take arguments.
It is called automatically when an object is created.
It allows an object to initialize a value before it is used.
They are called in the successive order of their creation.
There can be multiple constructors in a single class.
The copy constructor allows the constructor to declare and initialize an object from another object.
It can be overloaded.
Example:
class_name( arguments if any ) { };
Destructor
It deallocates the memory of an object.
It doesn’t take any argument.
It is called automatically when the block is exited or when the program terminates.
They allow objects to execute code when it is being destroyed.
They are called in the reverse order of their creation.
There is a single destructor in a class.
Destructor can’t be overloaded.
Example:
~ class_name( no arguments ) { };
- Related Questions & Answers
- Difference between Static Constructor and Instance Constructor in C#
- Order of Constructor/ Destructor Call in C++
- Difference between constructor and method in Java
- attribute((constructor)) and attribute((destructor)) syntaxes in C in tutorials point ?
- What is the difference between a destructor and a free function in C++?
- Difference Between Constructor Injection and Setter Injection in Spring
- Difference Between Copy Constructor and Assignment Operator in C++
- What is the difference between new operator and object() constructor in JavaScript?
- What is the difference between getter/setter methods and constructor in Java?
- What is the difference between void, eval, and the Function constructor in JavaScript?
- What's the difference between assignment operator and copy constructor in C++?
- What are the differences between default constructor and parameterized constructor in Java?
- What is the main difference between objects created using object literal and constructor function?
- Virtual Destructor in C++
- Private Destructor in C++