Simple Destructor Scope Measurement Example Program In C++
Definition
A destructor is a special member function which is called automatically when the object goes out of scope.
Syntax
Class class_name {
public:
`class_name() //Destructor
{
}
}
Example 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 ()
{
cout<<"Simple Destructor Scope Measurement Example Program \n\n";
{
// Object 1 Declaration for BaseClass
cout<<"Object 1 : Creation In Braces\n\n";
BaseClass des1;
// Object 1 des1 : End of Scope
}
cout<<"Object 2 : Creation in Main Part\n\n";
BaseClass des2;
// Wait For Output Screen
getch();
//Main Function return Statement
return 0;
}
Sample Output
Simple Destructor Scope Measurement Example Program
Object 1 : Creation In Braces
Constructor of the BaseClass : Object Created
Destructor of the BaseClass : Object Destroyed
Object 2 : Creation in Main Part
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 Program for Function Overloading Using C++ Programming
- Simple Example Program For Copy Constructor In C++
- Simple Program for Single Inheritance Using C++ Programming
- Simple Program for Inline Function without Class Using C++ Programming
- Factorial Using Function Example Program In C++
- Simple Example Program For Constructor Overloading In C++
- Simple Example Program For Constructor In C++
- Simple Addition ( Add Two Integers ) Example Program
- Simple Example Program for Inline Function Using C++ Programming
- Simple Program for Read user Input Using cin
- Factorial Using Loop Example Program In C++
- Simple Stack Program in C++ Programming
- Simple Program for Friend Function Using C++ Programming
- Simple Program for Static Data and Member Function Using C++ Programming
- Simple Program for Unary Operator Overloading Using C++ Programming
- Simple Program for Multiple Inheritance Using C++ Programming
- Do While Loop Example Program In C++
- Simple Copy Constructor Example Program For Find Factorial In C++
- Simple Program for Function Template Using C++ Programming