<br />
<b>Warning</b>:  Undefined array key "global_protection_id" in <b>/home/wikitechy/public_html/interview-questions/wp-content/plugins/content-protector/inc/class-ps-rest-handler.php</b> on line <b>51</b><br />
{"id":4774,"date":"2022-08-18T10:51:24","date_gmt":"2022-08-18T10:51:24","guid":{"rendered":"https:\/\/www.wikitechy.com\/interview-questions\/?p=4774"},"modified":"2022-08-18T10:51:24","modified_gmt":"2022-08-18T10:51:24","slug":"what-is-destructor-in-cpp","status":"publish","type":"post","link":"https:\/\/www.wikitechy.com\/interview-questions\/cpp\/what-is-destructor-in-cpp\/","title":{"rendered":"What is destructor in C++ ?"},"content":{"rendered":"<ul>\n<li style=\"text-align: justify;\">In C++ destructor is an instance member function which is invoked automatically whenever an object is going to be destroyed.<\/li>\n<li style=\"text-align: justify;\">A destructor meaning is the last function that is going to be called before an object is destroyed.<\/li>\n<li style=\"text-align: justify;\">If the object is created by using new or the constructor uses new to allocate memory which resides in the heap memory or the free store, the destructor should use delete to free the memory, the thing is to be noted here.<\/li>\n<li style=\"text-align: justify;\">When the objects are destroyed, destructor function is automatically invoked.<\/li>\n<li style=\"text-align: justify;\">Destructor does not have arguments and it cannot be declared as constant or static.<\/li>\n<li style=\"text-align: justify;\">Destructor cannot become a member of the union while with an object of a class.<\/li>\n<li style=\"text-align: justify;\">The programmer cannot access the address of destructor and in public section of class destructor should be declared.<\/li>\n<li style=\"text-align: justify;\">If the object goes out of scope, destructor function is called automatically were the function ends, the program ends, a block containing local variables ends, a delete operator is called.<\/li>\n<li style=\"text-align: justify;\">Destructors have same name as the class preceded by a tilde (~) it doesn\u2019t take any argument and return anything.<\/li>\n<\/ul>\n<h2 id=\"sample-code\" style=\"text-align: justify;\">Sample Code<\/h2>\n<div class=\"code-embed-wrapper\"> <div class=\"code-embed-infos\"> <\/div> <pre class=\"language-cpp code-embed-pre line-numbers\"  data-start=\"1\" data-line-offset=\"0\"><code class=\"language-cpp code-embed-code\">#include&lt;iostream&gt;<br\/>               using namespace std;<br\/>              class Demo {<br\/>                   private:<br\/>                   int num1, num2;<br\/>                   public:<br\/>                   \tDemo(int n1, int n2) {<br\/>                                   cout&lt;&lt;&quot;Inside Constructor&quot;&lt;&lt;endl;<br\/>                                   num1 = n1;<br\/>                                   num2 = n2;<br\/>                              }<br\/>                             void display() {<br\/>                                   cout&lt;&lt;&quot;num1 = &quot;&lt;&lt; num1 &lt;&lt;endl;<br\/>                                   cout&lt;&lt;&quot;num2 = &quot;&lt;&lt; num2 &lt;&lt;endl;<br\/>                             }<br\/>                         ~Demo() {<br\/>                                  cout&lt;&lt;&quot;Inside Destructor&quot;;<br\/>                            }<br\/>                       };<br\/>                      int main() {<br\/>                      Demo obj1(30, 50);<br\/>                      obj1.display();<br\/>                      return 0;<br\/>                      }<\/code><\/pre> <\/div>\n<h2 id=\"output\" style=\"text-align: justify;\">Output<\/h2>\n<p style=\"text-align: justify;\"><img fetchpriority=\"high\" decoding=\"async\" class=\"alignnone size-full wp-image-4775\" src=\"https:\/\/www.wikitechy.com\/interview-questions\/wp-content\/uploads\/2022\/08\/destructor.png\" alt=\"\" width=\"1137\" height=\"311\" srcset=\"https:\/\/www.wikitechy.com\/interview-questions\/wp-content\/uploads\/2022\/08\/destructor.png 1137w, https:\/\/www.wikitechy.com\/interview-questions\/wp-content\/uploads\/2022\/08\/destructor-300x82.png 300w, https:\/\/www.wikitechy.com\/interview-questions\/wp-content\/uploads\/2022\/08\/destructor-1024x280.png 1024w, https:\/\/www.wikitechy.com\/interview-questions\/wp-content\/uploads\/2022\/08\/destructor-768x210.png 768w, https:\/\/www.wikitechy.com\/interview-questions\/wp-content\/uploads\/2022\/08\/destructor-390x107.png 390w, https:\/\/www.wikitechy.com\/interview-questions\/wp-content\/uploads\/2022\/08\/destructor-820x224.png 820w\" sizes=\"(max-width: 1137px) 100vw, 1137px\" \/><\/p>\n","protected":false},"excerpt":{"rendered":"<p>In C++ destructor is an instance member function which is invoked automatically whenever an object is going to be destroyed. A destructor meaning is the last function that is going to be called before an object is destroyed. If the object is created by using new or the constructor uses new to allocate memory which [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"passster_activate_protection":false,"passster_protect_child_pages":"","passster_protection_type":"password","passster_password":"","passster_activate_overwrite_defaults":"","passster_headline":"","passster_instruction":"","passster_placeholder":"","passster_button":"","passster_id":"","passster_activate_misc_settings":"","passster_redirect_url":"","passster_hide":"no","passster_area_shortcode":"","gtb_hide_title":false,"gtb_wrap_title":false,"gtb_class_title":"","gtb_remove_headerfooter":false,"footnotes":""},"categories":[2080],"tags":[18365,18363,18372,18362,18368,18369,18364,18367,18366,18371,18370],"class_list":["post-4774","post","type-post","status-publish","format-standard","hentry","category-cpp","tag-c-destructor","tag-c-destructors","tag-characteristics-of-destructors-in-c","tag-constructor-and-destructor-in-c","tag-destructor-in-c","tag-destructor-in-c-example-program-with-output","tag-destructors-in-c","tag-how-to-call-destructor-c","tag-types-of-destructor-in-c","tag-use-of-destructor-in-c","tag-what-is-destructor-in-c"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v22.6 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>What is destructor in C++ - Destructors in C++ - C++ Destructor<\/title>\n<meta name=\"description\" content=\"What is destructor in C++ - In C++ destructor is an instance member function which is invoked automatically whenever an object is going to be destroyed.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.wikitechy.com\/interview-questions\/cpp\/what-is-destructor-in-cpp\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"What is destructor in C++ - Destructors in C++ - C++ Destructor\" \/>\n<meta property=\"og:description\" content=\"What is destructor in C++ - In C++ destructor is an instance member function which is invoked automatically whenever an object is going to be destroyed.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.wikitechy.com\/interview-questions\/cpp\/what-is-destructor-in-cpp\/\" \/>\n<meta property=\"og:site_name\" content=\"Wikitechy\" \/>\n<meta property=\"article:published_time\" content=\"2022-08-18T10:51:24+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/www.wikitechy.com\/interview-questions\/wp-content\/uploads\/2022\/08\/destructor.png\" \/>\n<meta name=\"author\" content=\"webmaster\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"webmaster\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"2 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.wikitechy.com\/interview-questions\/cpp\/what-is-destructor-in-cpp\/\",\"url\":\"https:\/\/www.wikitechy.com\/interview-questions\/cpp\/what-is-destructor-in-cpp\/\",\"name\":\"What is destructor in C++ - Destructors in C++ - C++ Destructor\",\"isPartOf\":{\"@id\":\"https:\/\/www.wikitechy.com\/interview-questions\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/www.wikitechy.com\/interview-questions\/cpp\/what-is-destructor-in-cpp\/#primaryimage\"},\"image\":{\"@id\":\"https:\/\/www.wikitechy.com\/interview-questions\/cpp\/what-is-destructor-in-cpp\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/www.wikitechy.com\/interview-questions\/wp-content\/uploads\/2022\/08\/destructor.png\",\"datePublished\":\"2022-08-18T10:51:24+00:00\",\"dateModified\":\"2022-08-18T10:51:24+00:00\",\"author\":{\"@id\":\"https:\/\/www.wikitechy.com\/interview-questions\/#\/schema\/person\/f785ba3ecc599133e65ab6138042a3e4\"},\"description\":\"What is destructor in C++ - In C++ destructor is an instance member function which is invoked automatically whenever an object is going to be destroyed.\",\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.wikitechy.com\/interview-questions\/cpp\/what-is-destructor-in-cpp\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.wikitechy.com\/interview-questions\/cpp\/what-is-destructor-in-cpp\/#primaryimage\",\"url\":\"https:\/\/www.wikitechy.com\/interview-questions\/wp-content\/uploads\/2022\/08\/destructor.png\",\"contentUrl\":\"https:\/\/www.wikitechy.com\/interview-questions\/wp-content\/uploads\/2022\/08\/destructor.png\",\"width\":1137,\"height\":311},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.wikitechy.com\/interview-questions\/#website\",\"url\":\"https:\/\/www.wikitechy.com\/interview-questions\/\",\"name\":\"Wikitechy\",\"description\":\"Interview Questions\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/www.wikitechy.com\/interview-questions\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"en-US\"},{\"@type\":\"Person\",\"@id\":\"https:\/\/www.wikitechy.com\/interview-questions\/#\/schema\/person\/f785ba3ecc599133e65ab6138042a3e4\",\"name\":\"webmaster\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.wikitechy.com\/interview-questions\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/155b77fd8cdda3d0913fcb7e7ee63543b0c345d2d8f6dcebda5b0583ab61f967?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/155b77fd8cdda3d0913fcb7e7ee63543b0c345d2d8f6dcebda5b0583ab61f967?s=96&d=mm&r=g\",\"caption\":\"webmaster\"},\"sameAs\":[\"https:\/\/www.wikitechy.com\/interview-questions\"],\"url\":\"https:\/\/www.wikitechy.com\/interview-questions\/author\/webmaster\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"What is destructor in C++ - Destructors in C++ - C++ Destructor","description":"What is destructor in C++ - In C++ destructor is an instance member function which is invoked automatically whenever an object is going to be destroyed.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.wikitechy.com\/interview-questions\/cpp\/what-is-destructor-in-cpp\/","og_locale":"en_US","og_type":"article","og_title":"What is destructor in C++ - Destructors in C++ - C++ Destructor","og_description":"What is destructor in C++ - In C++ destructor is an instance member function which is invoked automatically whenever an object is going to be destroyed.","og_url":"https:\/\/www.wikitechy.com\/interview-questions\/cpp\/what-is-destructor-in-cpp\/","og_site_name":"Wikitechy","article_published_time":"2022-08-18T10:51:24+00:00","og_image":[{"url":"https:\/\/www.wikitechy.com\/interview-questions\/wp-content\/uploads\/2022\/08\/destructor.png"}],"author":"webmaster","twitter_card":"summary_large_image","twitter_misc":{"Written by":"webmaster","Est. reading time":"2 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/www.wikitechy.com\/interview-questions\/cpp\/what-is-destructor-in-cpp\/","url":"https:\/\/www.wikitechy.com\/interview-questions\/cpp\/what-is-destructor-in-cpp\/","name":"What is destructor in C++ - Destructors in C++ - C++ Destructor","isPartOf":{"@id":"https:\/\/www.wikitechy.com\/interview-questions\/#website"},"primaryImageOfPage":{"@id":"https:\/\/www.wikitechy.com\/interview-questions\/cpp\/what-is-destructor-in-cpp\/#primaryimage"},"image":{"@id":"https:\/\/www.wikitechy.com\/interview-questions\/cpp\/what-is-destructor-in-cpp\/#primaryimage"},"thumbnailUrl":"https:\/\/www.wikitechy.com\/interview-questions\/wp-content\/uploads\/2022\/08\/destructor.png","datePublished":"2022-08-18T10:51:24+00:00","dateModified":"2022-08-18T10:51:24+00:00","author":{"@id":"https:\/\/www.wikitechy.com\/interview-questions\/#\/schema\/person\/f785ba3ecc599133e65ab6138042a3e4"},"description":"What is destructor in C++ - In C++ destructor is an instance member function which is invoked automatically whenever an object is going to be destroyed.","inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.wikitechy.com\/interview-questions\/cpp\/what-is-destructor-in-cpp\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.wikitechy.com\/interview-questions\/cpp\/what-is-destructor-in-cpp\/#primaryimage","url":"https:\/\/www.wikitechy.com\/interview-questions\/wp-content\/uploads\/2022\/08\/destructor.png","contentUrl":"https:\/\/www.wikitechy.com\/interview-questions\/wp-content\/uploads\/2022\/08\/destructor.png","width":1137,"height":311},{"@type":"WebSite","@id":"https:\/\/www.wikitechy.com\/interview-questions\/#website","url":"https:\/\/www.wikitechy.com\/interview-questions\/","name":"Wikitechy","description":"Interview Questions","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.wikitechy.com\/interview-questions\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"en-US"},{"@type":"Person","@id":"https:\/\/www.wikitechy.com\/interview-questions\/#\/schema\/person\/f785ba3ecc599133e65ab6138042a3e4","name":"webmaster","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.wikitechy.com\/interview-questions\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/155b77fd8cdda3d0913fcb7e7ee63543b0c345d2d8f6dcebda5b0583ab61f967?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/155b77fd8cdda3d0913fcb7e7ee63543b0c345d2d8f6dcebda5b0583ab61f967?s=96&d=mm&r=g","caption":"webmaster"},"sameAs":["https:\/\/www.wikitechy.com\/interview-questions"],"url":"https:\/\/www.wikitechy.com\/interview-questions\/author\/webmaster\/"}]}},"_links":{"self":[{"href":"https:\/\/www.wikitechy.com\/interview-questions\/wp-json\/wp\/v2\/posts\/4774","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.wikitechy.com\/interview-questions\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.wikitechy.com\/interview-questions\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.wikitechy.com\/interview-questions\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.wikitechy.com\/interview-questions\/wp-json\/wp\/v2\/comments?post=4774"}],"version-history":[{"count":1,"href":"https:\/\/www.wikitechy.com\/interview-questions\/wp-json\/wp\/v2\/posts\/4774\/revisions"}],"predecessor-version":[{"id":4776,"href":"https:\/\/www.wikitechy.com\/interview-questions\/wp-json\/wp\/v2\/posts\/4774\/revisions\/4776"}],"wp:attachment":[{"href":"https:\/\/www.wikitechy.com\/interview-questions\/wp-json\/wp\/v2\/media?parent=4774"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wikitechy.com\/interview-questions\/wp-json\/wp\/v2\/categories?post=4774"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wikitechy.com\/interview-questions\/wp-json\/wp\/v2\/tags?post=4774"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}