Simple Example Program For Destructor In C++
Destructor Overview
Definition
In C++, The destructor is a special member function which is called automatically when the object goes out of scope.
Destructor looks like a normal function and is called automatically when the program ends or an object is deleted.
Rules Of Destructor
- Should start with a tilde(~) and same name of the class.
- Destructors do not have parameters and return type.
- Destructors are called automatically and cannot be called from a program manually.
Destructor Usage
- Releasing memory of the objects.
- Releasing memory of the pointer variables.
- Closing files and resources.
Destructor Syntax
Class class_name {
public:
~class_name() //Destructor
{
}
}
Simple Destructor Example Program For Program
// Header Files
#include<iostream>
#include<conio.h>
//Standard Namespace Declaration
using namespace std;
class BaseClass // Class Name
{
public:
//Constructor of the BaseClass
BaseClass() {
cout << "Constructor of the BaseClass : Object Created"<<endl;
}
//Destructor of the BaseClass
~BaseClass() {
cout << "Destructor of the BaseClass : Object Destroyed"<<endl;
}
};
int main ()
{
// Object Declaration for BaseClass
BaseClass des;
// Wait For Output Screen
getch();
//Main Function return Statement
return 0;
}
Sample Output
Constructor of the BaseClass : Object Created
Destructor of the BaseClass : Object Destroyed
C++ Constructor Example Programs
- Simple Example Program For Constructor In C++
- Define Constructor in Outside Class Example Program In C++
- Simple Example Program For Parameterized Constructor In C++
- Simple Parameterized Constructor For Find Prime Number Example Program In C++
- Simple Example Program For Constructor Overloading In C++
- Simple Example Program For Copy Constructor In C++
- Simple Copy Constructor Example Program For Find Factorial In C++
- Simple Example Program For Destructor In C++
- Simple Destructor Scope Measurement Example Program In C++
Read More Articles
- Simple Merge Sort Program in C++
- Scope Resolution Operator In C++
- Simple Program for Virtual Functions Using C++ Programming
- Simple Class Example Program For Find Prime Number In C++
- Simple Example Program For Parameterized Constructor In C++
- Define Constructor in Outside Class Example Program In C++
- Simple Example Program For Copy Constructor In C++
- Simple Program for Function Overloading Using C++ Programming
- Simple Program for Single Inheritance Using C++ Programming
- Simple Program for Inline Function without Class Using C++ Programming
- Simple Example Program For Constructor Overloading In C++
- Factorial Using Function Example Program In C++
- Simple Example Program for Inline Function Using C++ Programming
- Simple Addition ( Add Two Integers ) Example Program
- Simple Example Program For Constructor In C++
- Simple Program for Read user Input Using cin
- Simple Stack Program in C++ Programming
- Factorial Using Loop Example Program In C++
- Simple Program for Friend Function Using C++ Programming
- Simple Program for Static Data and Member Function Using C++ Programming
- Simple Program for Multiple Inheritance Using C++ Programming
- Simple Program for Unary Operator Overloading Using C++ Programming
- Simple Copy Constructor Example Program For Find Factorial In C++
- Do While Loop Example Program In C++
- Simple Program for Virtual Base Class Using C++ Programming