Destructor | Concept and Declaration – Object Oriented Programming (OOP)

Download our Android App from Google Play Store and start reading Reference Notes Offline.

DestructorDestructor | Concept and Declaration
Object Oriented Programming (OOP)
Third Semester | Second year
BSc.CSIT | Tribhuvan University (TU)

Destructors 
Detructors are functins that are complimentary to constructors. A destructor, as the name implies, is used to destroy the objects that have been created by using constructor. They de-initialize objects when they are destroyed. A destructor is invoked when an object of the class goes out of scope, or when the memory occupied by it is de allocateed using the delete operator. A desutructor is a function that has the same name as that of the class but is prefixed with a ~(tilde).

For example the destructor for the class student will bear the name ~student(). Destructor takes no argument or specifies a return value, or explicitly returns a value not even void. It is called automatically by the compiler when an object is destroyed. A destructor cleans up the memory that is no longer required or accessible. To understand the syntax of destructor let us take the following example.

Declaraton of Destructor
Let us take the following C++ program to illustrate the declaration of destructor
#include<iostream.h>
#include<conio.h>
class Student
{
public:
~student()
{
cout<<”This is the example of destructor”<<endl;
}
};
main()
{
Student s1;
getch();
}

Here in the above program ~student() function has been used for destroying the use of memory which was allocated to constructor class by the operating system.

(Visited 188 times, 1 visits today)

Posted By : Digvijay | Comment RSS | Category : Third Semester
Tag : ,

Post a Comment

Your email is never published nor shared. Required fields are marked *

*
*

Wordpress DMCA
Community | Toolbar | Android App | Founder/Developer : Hari Prasad Chaudhary | CSIT Portal Manager : Digvijay Chaudhary