Destructor In C++ | Destructor Explained With Real Life Examples

 Destructor In C++


What is a Destructor ?

As you previously understood about Constructor. You know how constructor works so destructor is opposite of that - 
What are opposite in destroctor - 

- Destructor call the end of a class always
- Destructor takes no argument
- Destructor won't return anything
- To Modify a constructor you should use ~className

Other than that Destructor is same as constructor.


#include <iostream>
using namespace std;

class myClass{
    
    public:
    ~ myClass()
    {

        cout<<"I AM A DESRUCTOR"<<endl;
    }
    void get()
    {
        cout<<"I am a simple method"<<endl;
    }
    
};
int main()
{
    myClass m1;
    m1.get();
    return 0;
}

Output - 
I am a simple method
I AM A DESRUCTOR

Here you can see we define destructor at starting in class but the destructor executed at last always.



Comments

Popular posts from this blog

Constructor In C++ | Constructor Explained With Real Life Example

Array Of Objects In C++ | Array Of Objects Explained With Real Life Example