Course Content
C++ Introduction
0/1
C++ Variables & Constants
0/1
C++Scope of Variable
0/1
C++ Keywords & Identifiers
0/1
C++ Data Types
0/1
C++ Basic I/O
0/1
C++ Type Conversion
0/1
C++ Operators
0/1
C++ Comments
0/1
C++ If-else
0/1
C++ Ternary Operator
0/1
C++ for Loop
0/1
C++ Ranged for Loop
0/1
C++ while/do-while Loop
0/1
C++ break Statement
0/1
C++ Continue Statement
0/1
C++ switch Statement
0/1
C++ goto Statement
0/1
C++ Functions
0/1
C++ User-defined Functions
0/1
C++ Default Arguments
0/1
C++ Storage Class
0/1
C++ Recursion
0/1
C++ Return by Reference
0/1
C++ Arrays
0/1
C++ Multi-dimentional Arrays
0/1
C++ Arrays & Function
0/1
C++ String
0/1
C++ Structure
0/1
C++ Structure & Functions
0/1
C++ Pointers to Structure
0/1
C++ Pointers
0/1
C++ Void Pointers
0/1
C++ Pointers & Arrays
0/1
C++ Pointers & Functions
0/1
C++ Dynamic Memory Allocation
0/1
C++ OOPs Concepts
0/1
C++ Objects and Class
0/1
C++ Constructors
0/1
C++ Destructors
0/1
C++ Constructor Overloading
0/1
C++ Objects & Function
0/1
C++ Enumeration
0/1
C++ Inheritance
0/1
C++ Inheritance Access Control
0/1
C++ Inheritance Types
0/1
C++ Polymorphism
0/1
C++ Function Overloading
0/1
C++ Function Overriding
0/1
C++ Operator Overloading
0/1
C++ Friend Function
0/1
C++ Virtual Function
0/1
C++ Abstract Class & Pure Virtual Function
0/1
C++ Encapsulation
0/1
C++ Abstraction
0/1
C++ Templates
0/1
C++ Exception Handling
0/1
C++ Multithreading
0/1
C++ Standard Library
0/1
C++ Programming Tutorials
About Lesson

C++ Destructors

In this tutorial, we will learn about the C++ destructor, Virtual and Pure virtual destructor with the help examples and when does destructor called and its rules.


Destructor

Destructor is a member function which deletes an object. A destructor is called automatically by the compiler when the object goes out of scope. A destructor is a special member function that works just opposite to constructor, unlike constructors that are used for initializing an object, destructors destroy (or delete) the object.

A destructor works opposite to constructor and defined like constructor; it destructs the objects of classes. It can be defined only once in a class and must have same name as class. Like constructors, it is invoked automatically. But it is prefixed with a tilde sign (~).

Syntax:

~className{

    // Some code
};

Note: C++ destructor cannot have parameters. Moreover, modifiers can’t be applied on destructors.


Properties of Destructor

A constructor is different from normal functions in following ways: 

  • Destructor function is automatically invoked when the objects are destroyed.
  • It cannot be declared static or const.
  • The destructor does not have arguments.
  • It has no return type not even void.
  • An object of a class with a Destructor cannot become a member of the union.
  • A destructor should be declared in the public section of the class.
  • The programmer cannot access the address of destructor.

When does the destructor get called?

A destructor function is called automatically when the object goes out of scope: 
(1) When the function ends. 
(2) When the program ends. 
(3) When a scope (the { } parenthesis) containing local variable ends. 
(4) When a delete operator is called.  


How destructors are different from a normal member function?

A destructor is different from normal functions in following ways: 

  • Destructors have same name as the class but is preceded by a tilde (~).
  • Destructors have no argument and no return value.

Example 1: C++ program to demonstrate the use of Destructor

#include <iostream>
using namespace std;

// declare a class
class hello {

   public:
    // Constructor
    hello() {
        cout << "Constructor is Called." << endl;
    }
    // Destructor
    ~hello() {
        cout << "Destructor is Called." << endl;
    }
    // Member Function
    int display(){
        cout << "Hello, Programmer." << endl;

    }
};

int main() {

    // create an object
    hello obj1;
    
    // Member function called
    obj.display();
    
    return 0;
}

Output

Constructor is Called.
Hello, Programmer.
Destructor is Called.

Virtual Destructors in C++

Deleting a derived class object using a pointer of base class type that has a non-virtual destructor results in undefined behavior. To correct this situation, the base class should be defined with a virtual destructor.

Destructors in the Base class can be Virtual. Whenever Upcasting is done, Destructors of the Base class must be made virtual for proper destruction of the object when the program exits.

NOTE: Constructors never be Virtual, only Destructors can be Virtual.

For Example:-


Example 2: C++ program for Upcasting without virtual destroctor

To print the content of a void pointer, we use the static_cast operator. It converts the pointer from void* type to the respective data type of the address the pointer is storing:

#include <iostream>
using namespace std;

// declare a class
class base {

   public:
    // Destructor
    ~base() {
        cout << "Destructor base." << endl;
    }
};
class derived : publicbase {
    public:
    // Destructor
    ~derived() {
        cout << "Destructor derived." << endl;
    }
}

int main() {

    base* b = new derived; // Upcasting
    delete b;

    return 0;
}

Output

Destructor base.

In the above example, delete b will only call the Base class destructor, which is undesirable because, then the object of Derived class remains undestructed, because its destructor is never called. Which results in memory leak.


Example 3: C++ program for Upcasting with virtual destructor

#include <iostream>
using namespace std;

// declare a class
class base {

   public:
    virtual ~base() {
        cout << "Destructor base." << endl;
    }
};
class derived : publicbase {
    public:
    ~derived() {
        cout << "Destructor derived." << endl;
    }
}

int main() {

    base* b = new derived; // Upcasting
    delete b;

    return 0;
}

Output

Destructor base.
Destructor derived.

When we have Virtual destructor inside the base class, then first Derived class’s destructor is called and then Base class’s destructor is called, which is the desired behaviour.


Pure Virtual Destructor

When a destructor is initialized with value 0. It is called virtual destructor, it is declared in base class. Like virtual destructor, pure virtual destructor is also possible. But it should be defined in base class.

  • The only difference between Virtual and Pure Virtual Destructor is, that pure virtual destructor will make its Base class Abstract, hence you cannot create object of that class.
  • There is no requirement of implementing pure virtual destructors in the derived classes.

Example 4: C++ program to understand Pure virtual destructor

#include <iostream>
using namespace std;

// declare a class
class base {
   public:
    virtual ~base() = 0; // Pure Virtual Destructor
};
// Definition of Pure Virtual Destructor
base :: ~base() {

    cout << "Pure virtual destructor is called.\n";
}
class derived : publicbase {
    public:
    ~derived() {
        cout << "Derived Destructor is called." << endl;
    }
}
int main() {

    base* b = new derived;
    delete b;

    return 0;
}

Output

Pure virtual destructor is called.
Derived Destructor is called.

Rules for defining a Destructor

1) Name should begin with tilde sign(~) and must match class name.
2) There cannot be more than one destructor in a class.
3) Unlike constructors that can have parameters, destructors do not allow any parameter.
4) They do not have any return type, just like constructors.
5) When you do not specify any destructor in a class, compiler generates a default destructor and inserts it into your code.


Important Question / Answers

 

1. Is there be more than one destructor in a class?

Ans 🙂  No, there can only one destructor in a class with className preceded by ’tilde sign’ ~, no parameters and no return type.


2. When do we need to write a user-defined destructor?

Ans 🙂  If we do not write our own destructor in class, compiler creates a default destructor for us. The default destructor works fine unless we have dynamically allocated memory or pointer in class. When a class contains a pointer to memory allocated in class, we should write a destructor to release memory before the class instance is destroyed. This must be done to avoid memory leak.

Exercise Files
No Attachment Found
No Attachment Found
error: Content is protected !!