Click here to hide categories Click here to show left categories

User: Home          welcome : Guest          Log In / Register here     

Desctructor in c++

It is a special member function which is used to destroy the object that is it is used to take back the memory given to the object at the time of its creation. Since it destroys the object, it is called ‘Destructor’.

Certain characteristics of destructor are:

1. Its name is same as that of class name but is preceded by ‘tilde’ (~) symbol.
2. It is automatically executed when the object goes out of scope that is there is no need to call it as we call ordinary member function.
3. It does not take arguments and it has no return type that is it does not return any value.
4. If we do not use our own destructor it is automatically provided by the system.
5. We can not overload destructor.

For example (In case of C++):

class sample
sample( ) //Constructor
cout<<”Object Created”;
~sample( ) //Destructor
cout<<”Object Destroyed”;

void main( )
sample s1; //Constructor is called
clrscr( );
getch( );
} //Destructor is called since closing
brace of main ( ) is the end of scope
of object s1.

Share this article   |    Print    |    Article read by 2025 times
Rohit kakria
I am software developer, moderator of
Related Articles: Exception: System.IO.DirectoryNotFoundException: Could not find a part of the path 'G:\PleskVhosts\\\CommonFiles\words.txt'. at System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath) at System.IO.FileStream.Init(String path, FileMode mode, FileAccess access, Int32 rights, Boolean useRights, FileShare share, Int32 bufferSize, FileOptions options, SECURITY_ATTRIBUTES secAttrs, String msgPath, Boolean bFromProxy) at System.IO.FileStream..ctor(String path, FileMode mode, FileAccess access, FileShare share, Int32 bufferSize, FileOptions options) at System.IO.StreamReader..ctor(String path, Encoding encoding, Boolean detectEncodingFromByteOrderMarks, Int32 bufferSize) at System.IO.StreamReader..ctor(String path) at usercontrols_relatedInterviewQuestions.BindRelatedArticles() in g:\pleskvhosts\\\usercontrols\RelatedArticleInterviewQuest.ascx.cs:line 37
Related Interview Questions: