Destructors in C++ with example



A destructor is a function that automatically executes when an object is destroyed. The prime usage of the destructor function is to release space on the heap. A destructor function may be invoked explicitly.

Example:
#include<iostream.h>
#include<conio.h>
class student
{
int id;
char name[20];
public:
void get();
void put();
~ student()                                        
               {
               cout<<”Object Destroyed”;
               }
};
void student::get()
{
               cout<<”Enter Student Info = ”;
               cin>>id>>name;
}
void student::put()
{
               cout<<”Student Info = ”;
               cout<<id<<name;
}
void main()
{
clrscr();
               student st;
               st.get();
               st.put();
               getch();
}

No comments:

Post a Comment