C++ Destructor1 min read

As the name indicates, a C++ destructor destroys the object of a class as soon as the scope of an object ends. It is opposite to a constructor. When an object goes out of the scope, the compiler automatically calls the destructor function.

Also, the destructor has the exact same name as the class, the difference is, it is prefixed with a tilde sign (~). And the destructor cannot have parameters nor modifiers.

The syntax for C++ destructor:

let us see an example of a destructor in C++.


C++ example for the Constructor and Destructor call

Output:

Constructor Invoked.
Constructor Invoked.
Destructor Invoked.
Destructor Invoked.


MORE

Keith Number Program in C

In this tutorial, we will write a Keith number in C. It is one of the most asked questions in an interview. Before that, you …
Read More

Keith Number Program in C++

In this tutorial, we will write a Keith number in C++. It is one of the most asked questions in an interview. Before that, you …
Read More

Singly linked list Program C++

In this tutorial, we will write a C++ program to implement singly linked list. In this program, we will create a separate function for each …
Read More

Fascinating Number Program in C

In this tutorial, we will write a C program to check whether the given number is a Fascinating number or not. You may go through …
Read More

Peterson Number in C

In this tutorial, we will learn about the Peterson number and check if the number is Peterson or not in C. What is Peterson Number? …
Read More

Peterson Number in C++

In this tutorial, we will learn about the Peterson number and write a C++ program to check if the given number is a Peterson number …
Read More