×

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++

  1. Default constructor
  2. Dynamic constructor
  3. Parameterized constructor
  4. 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:

CharacteristicsConstructor in C++Destructor in C++
MotiveIn 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.
ArgumentsIt could include or exclude arguments.It is unable to accommodate the arguments.
CallingIt is invoked automatically whenever a class object is generated.When the application ends, it automatically calls this function.
RAMThe function Object () { [native code] } uses memory.Memory is released by the Destructor.
Returning TypesIt has different return types.There is no return type for it.
Special SymbolThe 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 UseIn our program, we can utilize many constructors.The program cannot have more than one destructor.
Way of DeclarationTo 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();  { …………         };

Related Topics

Pointer to Object in C++

What is a pointer? A pointer in C++ is used to point the variable by storing the address of the variable. In C++, to print the address of the variable, we...

4 minutes read.

INT_MAX and INT_MIN in C/C++

In competitive programming, assigning a variable that maximum or minimum value a data type can carry is frequently necessary. Still, it might be challenging to recall such a significant, exact...

3 minutes read.

Converting string into integer in C++

When programming in C++, we'll frequently need to change one data type to another. When we use C++ to create apps, we must transform data from one type to another. When...

7 minutes read.

Armstrong number using for loop in C++

What is For Loop? A for loop is a repetitive control structure that allows you to create a loop to execute a specific number of times efficiently. The syntax that can be...

4 minutes read.

Reverse String Word-Wise in C++

What is a reversed String? Reversing the words of a sentence is called reversed string by words. The difference between the reverse a string and the reverse a string word-wise is...

4 minutes read.

Decltype type Specifier in C++

The primary use of C++ decltype is to inspect the declaration type of an entity in an expression. The auto keyword can declare a particular type of variable, whereas the...

4 minutes read.

Initialize Vector in C++

Initialize Vector in C++  The following comparison operators are defined for vector and those are given below. ==, <, <=, !=, >,>=  This allows you to access the element of a vector using...

3 minutes read.

C++ Identifier

In a program, C++ identifiers relate to the names of variables, functions, arrays, and other user-defined data types that the programmer has developed. They are a prerequisite for learning any...

4 minutes read.

C++ Overriding

C++ Function Overriding When the base and derive class both contain the same function name and calling the function through derived object invokes derived class function called function overriding. Function overloading is...

1 minute read.

Data Hiding in C++

C++ : High-performance apps can be made using the cross-platform language C++. Bjarne Stroustrup created C++ as an addition to the C language. Programmers have extensive control over memory and system...

3 minutes read.

C++ Tricks for Competitive Programming

If you are interested in Computer science or Information technology, you must have heard about competitive programming. Competitive programming is a way to improve your problem-solving skills. There are various...

7 minutes read.

Continue in C++ While loop

Continue statement: Inside the loop, the continue statement is used to control the loop. C++ utilizes the continue keyword to implement the continue statement, which transfers the program's flow at the...

4 minutes read.

Armstrong Number using Do-While Loop in C++

What is Do-While Loop? An iterative loop that checks the condition at the end.The Do-While loop can be used whenever a test condition is specific, as the control enters the loop...

4 minutes read.

Use of Inheritance in C++

What is inheritance in c++? Inheritance is fundamental in object-oriented programming (OOP) languages like C++. It allows a programmer to create a new class (called a derived class) that inherits the...

4 minutes read.

Destructor

Let’s discover the C++ destructor, virtual and pure virtual destructors, and when destructors are invoked and their rules in this lesson. Destructors : A member function that destroys an object is known as...

5 minutes read.

Dynamic Binding in C++

The notion of dynamic binding solved the challenges associated with static binding. Static binding refers to bindings that can be resolved by the compiler at runtime. All storage, stationary, and...

3 minutes read.

wcscpy(), wcslen(), wcscmp() Functions in C++

There are many built-in functions in C++ programming language which differentiate it from C programming language in most hardware-coded languages. We will now closely look into the applications of three...

4 minutes read.

Containership in C++

In C++, it is possible to create an object in one class into another class, and that object is a member of another class. This relationship is known as a...

4 minutes read.

Memset in C++

Memset () is a function in the C++ programming language that fills memory blocks. The value of "ch" is first converted to an unsigned character. In this case, "ch" denotes the...

3 minutes read.

Advanced C++ with Boost Library

The goal of the Boost Libraries is to be widely applicable and used in a variety of applications. For instance, they can in handy when working with huge numbers whose...

4 minutes read.