Simple Destructor Scope Measurement Example Program In C++
On this page (6sections)
About this program
This is an example program in c constructor example programs. Read the concept first: Constructors and Destructors in C++ Classes, then study the code and output below.
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
Related Pages
Learn the concept first, then study the code:
- C++ Programs — Browse all C++ Programs.
- Constructors and Destructors in C++ Classes — Tutorial — constructors, destructors and overloading.
- Simple Example Program For Constructor In C++ — More in c constructor example programs.
- Simple Parameterized Constructor For Find Prime Number Example Program In C++ — More in c constructor example programs.