The destructor of a class, Purpose of the destructor, an example of destructor in OOP C++.
In this tutorial, we will learn about the followings;
- What is destructor of a class in OOP C+?
- What is the purpose of destructor in OOP C+?
- Example of destructor in OOP C+
What is the destructor of a class?
The destructor is a member function of the class. Destructor has the same name as the name of its class. The Tild sign ∼ is used before the name of the destructor.
- When the object of the class destroyed, destructor also destroyed automatically.
- One class can have only one destructor. However, one class can have many constructors.
- Destructor overloading is impossible.
- The Destructor can’t have any arguments(parameters).
- The destructor have no data type.
What is the purpose of the destructor of a class?
The main purpose of the destructor of a class is to free computer memory.
Program of a destructor of a class
using namespace std;
example(int n1, int n2)
cout<<"sum of 2 numbers is = "<<n1+n2<<endl;
cout<<"destructor completed its work to free the memory"<<endl;
sum of 2 numbers is 6
destructor completed its work to free the memory
|Test Your Understandings|
1.Multiple destructor for a class are possible? YES / NO
2. Destructor for a class is represented with…………..sign?
3. Destructor overloading is very helpful to free the computer memory?
Destructor of a class, Pupose of desctrucor, example of destructor in OOP C++.