INTRODUCTION OF DESTRUCTOR
Destructor functions are the inverse of constructor functions. These are called when objects are destroyed (de-allocated) and every destructor create with tilde sign (~)
DESTRUCTOR EXAMPLE
#include<iostream.h>
#include<conio.h>
class student
{
private:
int roll;
float marks;
public:
student()
{
roll=101;
marks=56.7;
}
void display()
{
cout<<"Roll number:"<<roll;
cout<<"\nStudent name:"<<marks;
}
// Define destructor
~student()
{
cout<<"\nObjects destroyed";
}
}; // Close class declaration
void main()
{
clrscr();
// Creating class object and calling default constructor
student ob;
ob.display(); // calling display function.
// At the end automatically calling destructor
getch();
}
0 Comments