Constructor Vs Destructor in C++
C++ Constructor
A function Object () { [native code] } is a member function that shares the same name as the class. It is called automatically whenever a class object is generated.
class Name // syntax
{
……….
public
Name ([parameter list])
{
……………….
}
};
Explanation:
The constructor's name is class name, the public specifier is an access specifier, and the parameter list is optional in the above syntax.
Example of a program showing the use of constructor in C++:
#include <iostream.h>
#include <bits/stdc++.h>
#include <stdlib>
#include <conio.h>
using namespace std;
class First_hello { // The class
public: // Access specifier
first_hello () { // Constructor
cout << "Hello World! Program in C++ showing the use of Constructor";
}
void display() {
cout <<"Hello World!" <<endl;
}
};
int main() {
hello myObje; /
return 0;
}
OUTPUT:
Hello World!
Types of Constructor available in C++
- Default constructor
- Dynamic constructor
- Parameterized constructor
- Copy constructor
Default Constructor
A default function Object () { [native code] } is a function Object() { [native code] } that does not accept any parameters. If a class doesn't have a function Object () { [native code] }, the compiler constructs one for it implicitly.
class name { //syntax
private:
………..
………..
public:
name ()
{
…….
}
}
Explanation:
There are no arguments or parameter lists in this sort of function Object () { [native code] }. The compiler constructs the class's default function Object () { [native code] } if no function Object() { [native code] } is defined in the class.
Parameterized Constructor
A parameterized function Object() { [native code] } is a type of function Object() { [native code] } that can accept parameters. It's used to provide objects a separate set of values when they're created.
Class Name //syntax
{
…………;
…………;
Public:
Class Name (parameter list)
{
………….;
}
};
Explanation:
The constructor's parameter list can be defined here.
Copy Constructor
A function Object () { [native code] } that is used to create a new object from an existing one. The copy function Object () { [native code] } is used to copy data from another object of the same type.
Class (Name, &object) //syntax
{
………….;
………….;
}
Explanation:
The object in the above syntax refers to an item that is utilized to initialize another object.
Dynamic Constructor
This function Object () { [native code] } can be used to allocate memory for objects while they are being created. Dynamic initialization is the process of initializing the data members of an object after it has been created.
C++ Destructor
The same class name prefixed by the () tilde symbol is used for destructors. It removes and destroys the object's memory, which was allocated by the function Object () { [native code] } during its creation.
class Name //syntax
{
…………….;
…………….;
public:
abc(); //constructor
~abc(); //destructor
};
Explanation:
In C++ programming, the tilde symbol is used to define the destructor. Because the Destructor takes no arguments and returns no value, it cannot be overloaded.
Example of a program showing the use of destructor in C++:
#include <iostream.h>
#include <bits/sdtc++.h>
#include <stdlib>
#include <conio.h>
using namespace std;
class Helloworld {
public:
//Constructor
Helloworld () {
cout<< "Constructor function is called" <<endl;
}
//Destructor
~Helloworld () {
cout << "Destructor function is called" <<endl;
}
//Member function
void display() {
cout <<"Hello World!" <<endl;
}
};
int main(){
//Object created
Helloworld obje;
//Member function called
obje.display();
return 0;
}
OUTPUT:
Hello World!
…………………….
Process exited with return value 0
Press any key to continue.
In C++ programming, there is a distinction between constructors and destructors as follows:
Characteristics | Constructor in C++ | Destructor in C++ |
Motive | In C++, we used a function Object () {[native code]} to allocate memory to the object. | In C++, we use the concept of destructor to de-allocate the memory that the function Object () { [native code] } allocated to an object for this purpose. |
Arguments | It could include or exclude arguments. | It is unable to accommodate the arguments. |
Calling | It is invoked automatically whenever a class object is generated. | When the application ends, it automatically calls this function. |
RAM | The function Object () { [native code] } uses memory. | Memory is released by the Destructor. |
Returning Types | It has different return types. | There is no return type for it. |
Special Symbol | The special symbol is not required when declaring constructors in the C++ programming language. | A specific sign, the tilde symbol, is necessary when defining a destructor in the C++ programming language. |
Number of Use | In our program, we can utilize many constructors. | The program cannot have more than one destructor. |
Way of Declaration | To create a function Object() { [native code] }, use the following declaration: class Name { ………. public Name ([parameter list]) { ………………. } }; | To create a destructor, use the following declaration: class Name { …………….; …………….; public: ~abc(); { ………… }; |