<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":4781,"date":"2022-08-18T12:31:05","date_gmt":"2022-08-18T12:31:05","guid":{"rendered":"https:\/\/www.wikitechy.com\/interview-questions\/?p=4781"},"modified":"2022-08-18T12:31:25","modified_gmt":"2022-08-18T12:31:25","slug":"what-is-a-virtual-destructor","status":"publish","type":"post","link":"https:\/\/www.wikitechy.com\/interview-questions\/cpp\/what-is-a-virtual-destructor\/","title":{"rendered":"What is a Virtual Destructor ?"},"content":{"rendered":"<ul>\n<li style=\"text-align: justify;\">In C++ destructor is a member function of a class used to free the space occupied by or delete an object of the class that goes out of scope.<\/li>\n<li style=\"text-align: justify;\">In class destructor has the same name as the name of the constructor function, but the destructor uses a tilde\u00a0<strong>(~)<\/strong>sign before its function name.<\/li>\n<li style=\"text-align: justify;\">It is used to free up the memory space allocated by derived class object or instance while deleting instances of the derived class using a base class pointer object.<\/li>\n<li style=\"text-align: justify;\">A derived class or base class destructor use the\u00a0<strong>virtual <\/strong>keyword that ensures both classes destructor will be called at run time.<\/li>\n<li style=\"text-align: justify;\">First it calls the derived class and then base class to release the space occupied by both destructors.<\/li>\n<li style=\"text-align: justify;\">If an object in the class goes out of scope or the execution of the main() function is about to end, automatically a destructor is called into the program to free up the space occupied by the class\u2019s destructor function.<\/li>\n<li style=\"text-align: justify;\">If a pointer object of the base class is deleted that points to the derived class, only the parent class destructor is called due to the early bind by the compiler.<\/li>\n<li style=\"text-align: justify;\">In main function if the compiler compiles the code, it calls a pointer object that refers to the base class.<\/li>\n<li style=\"text-align: justify;\">It executes the base class\u2019s constructor() function and then moves to the derived class\u2019s constructor() function and deletes the pointer object occupied by the base class&#8217;s destructor and the derived class&#8217;s destructor.<\/li>\n<li style=\"text-align: justify;\">In program base class pointer only removes the base class&#8217;s destructor without calling the derived class&#8217;s destructor<\/li>\n<\/ul>\n<h2 id=\"sample-code\">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\">using namespace std;  <br\/>class Base  <br\/>{  <br\/>    public:  <br\/>    Base() \/\/ Constructor member function.    <br\/>{  <br\/>    cout &lt;&lt; &quot;\\n Constructor Base class&quot;;  \/\/ It prints first.  <br\/>}  <br\/> virtual ~Base() \/\/ Define the virtual destructor function to call the Destructor Derived function.  <br\/>{  <br\/>    cout &lt;&lt; &quot;\\n Destructor Base class&quot;;  \/  <br\/>}  <br\/>};  <br\/>\/\/ Inheritance concept  <br\/>class Derived: public Base   <br\/>{  <br\/>    public:  <br\/>    Derived() \/\/ Constructor function.  <br\/>{  <br\/>    cout &lt;&lt; &quot;\\n Constructor Derived class&quot; ; \/* After print the Constructor Base, now it will prints. *\/  <br\/>}  <br\/> ~Derived() \/\/ Destructor function   <br\/>{  <br\/>    cout &lt;&lt; &quot;\\n Destructor Derived class&quot;; \/* The virtual Base Class? Destructor calls it before calling the Base Class Destructor. *\/  <br\/>}         <br\/>};  <br\/>int main()  <br\/>{  <br\/>    Base *bptr = new Derived; \/\/ A pointer object reference the Base class.  <br\/>    delete bptr; \/\/ Delete the pointer object.  <br\/>}  <\/code><\/pre> <\/div>\n<h2 id=\"output\">Output<\/h2>\n<p><img fetchpriority=\"high\" decoding=\"async\" class=\"alignnone size-full wp-image-4782\" src=\"https:\/\/www.wikitechy.com\/interview-questions\/wp-content\/uploads\/2022\/08\/virtual-destructor.png\" alt=\"\" width=\"1394\" height=\"391\" srcset=\"https:\/\/www.wikitechy.com\/interview-questions\/wp-content\/uploads\/2022\/08\/virtual-destructor.png 1394w, https:\/\/www.wikitechy.com\/interview-questions\/wp-content\/uploads\/2022\/08\/virtual-destructor-300x84.png 300w, https:\/\/www.wikitechy.com\/interview-questions\/wp-content\/uploads\/2022\/08\/virtual-destructor-1024x287.png 1024w, https:\/\/www.wikitechy.com\/interview-questions\/wp-content\/uploads\/2022\/08\/virtual-destructor-768x215.png 768w, https:\/\/www.wikitechy.com\/interview-questions\/wp-content\/uploads\/2022\/08\/virtual-destructor-390x109.png 390w, https:\/\/www.wikitechy.com\/interview-questions\/wp-content\/uploads\/2022\/08\/virtual-destructor-820x230.png 820w, https:\/\/www.wikitechy.com\/interview-questions\/wp-content\/uploads\/2022\/08\/virtual-destructor-1180x331.png 1180w\" sizes=\"(max-width: 1394px) 100vw, 1394px\" \/><\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In C++ destructor is a member function of a class used to free the space occupied by or delete an object of the class that goes out of scope. In class destructor has the same name as the name of the constructor function, but the destructor uses a tilde\u00a0(~)sign before its function name. It is [&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":[18386,18391,18384,18393,18383,18390,18392,2417,18388,18389,18385,18387],"class_list":["post-4781","post","type-post","status-publish","format-standard","hentry","category-cpp","tag-c-virtual-destructor","tag-can-we-have-virtual-destructor","tag-pure-virtual-destructor-in-c","tag-use-of-virtual-destructor-in-c","tag-virtual-destructor","tag-virtual-destructor-c","tag-virtual-destructor-default","tag-virtual-destructor-in-c","tag-what-is-virtual-destructor","tag-what-is-virtual-destructor-in-c","tag-when-to-use-virtual-destructors","tag-why-do-we-need-virtual-destructors"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v22.6 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>What is a Virtual Destructor ? - Virtual Destructor in C++ - Wikitechy<\/title>\n<meta name=\"description\" content=\"What is a Virtual Destructor ? - In C++ destructor is a member function of a class used to free the space occupied by or delete an object of the class that goes out of scope.\" \/>\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-a-virtual-destructor\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"What is a Virtual Destructor ? - Virtual Destructor in C++ - Wikitechy\" \/>\n<meta property=\"og:description\" content=\"What is a Virtual Destructor ? - In C++ destructor is a member function of a class used to free the space occupied by or delete an object of the class that goes out of scope.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.wikitechy.com\/interview-questions\/cpp\/what-is-a-virtual-destructor\/\" \/>\n<meta property=\"og:site_name\" content=\"Wikitechy\" \/>\n<meta property=\"article:published_time\" content=\"2022-08-18T12:31:05+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2022-08-18T12:31:25+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/www.wikitechy.com\/interview-questions\/wp-content\/uploads\/2022\/08\/virtual-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-a-virtual-destructor\/\",\"url\":\"https:\/\/www.wikitechy.com\/interview-questions\/cpp\/what-is-a-virtual-destructor\/\",\"name\":\"What is a Virtual Destructor ? - Virtual Destructor in C++ - Wikitechy\",\"isPartOf\":{\"@id\":\"https:\/\/www.wikitechy.com\/interview-questions\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/www.wikitechy.com\/interview-questions\/cpp\/what-is-a-virtual-destructor\/#primaryimage\"},\"image\":{\"@id\":\"https:\/\/www.wikitechy.com\/interview-questions\/cpp\/what-is-a-virtual-destructor\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/www.wikitechy.com\/interview-questions\/wp-content\/uploads\/2022\/08\/virtual-destructor.png\",\"datePublished\":\"2022-08-18T12:31:05+00:00\",\"dateModified\":\"2022-08-18T12:31:25+00:00\",\"author\":{\"@id\":\"https:\/\/www.wikitechy.com\/interview-questions\/#\/schema\/person\/f785ba3ecc599133e65ab6138042a3e4\"},\"description\":\"What is a Virtual Destructor ? - In C++ destructor is a member function of a class used to free the space occupied by or delete an object of the class that goes out of scope.\",\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.wikitechy.com\/interview-questions\/cpp\/what-is-a-virtual-destructor\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.wikitechy.com\/interview-questions\/cpp\/what-is-a-virtual-destructor\/#primaryimage\",\"url\":\"https:\/\/www.wikitechy.com\/interview-questions\/wp-content\/uploads\/2022\/08\/virtual-destructor.png\",\"contentUrl\":\"https:\/\/www.wikitechy.com\/interview-questions\/wp-content\/uploads\/2022\/08\/virtual-destructor.png\",\"width\":1394,\"height\":391},{\"@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 a Virtual Destructor ? - Virtual Destructor in C++ - Wikitechy","description":"What is a Virtual Destructor ? - In C++ destructor is a member function of a class used to free the space occupied by or delete an object of the class that goes out of scope.","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-a-virtual-destructor\/","og_locale":"en_US","og_type":"article","og_title":"What is a Virtual Destructor ? - Virtual Destructor in C++ - Wikitechy","og_description":"What is a Virtual Destructor ? - In C++ destructor is a member function of a class used to free the space occupied by or delete an object of the class that goes out of scope.","og_url":"https:\/\/www.wikitechy.com\/interview-questions\/cpp\/what-is-a-virtual-destructor\/","og_site_name":"Wikitechy","article_published_time":"2022-08-18T12:31:05+00:00","article_modified_time":"2022-08-18T12:31:25+00:00","og_image":[{"url":"https:\/\/www.wikitechy.com\/interview-questions\/wp-content\/uploads\/2022\/08\/virtual-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-a-virtual-destructor\/","url":"https:\/\/www.wikitechy.com\/interview-questions\/cpp\/what-is-a-virtual-destructor\/","name":"What is a Virtual Destructor ? - Virtual Destructor in C++ - Wikitechy","isPartOf":{"@id":"https:\/\/www.wikitechy.com\/interview-questions\/#website"},"primaryImageOfPage":{"@id":"https:\/\/www.wikitechy.com\/interview-questions\/cpp\/what-is-a-virtual-destructor\/#primaryimage"},"image":{"@id":"https:\/\/www.wikitechy.com\/interview-questions\/cpp\/what-is-a-virtual-destructor\/#primaryimage"},"thumbnailUrl":"https:\/\/www.wikitechy.com\/interview-questions\/wp-content\/uploads\/2022\/08\/virtual-destructor.png","datePublished":"2022-08-18T12:31:05+00:00","dateModified":"2022-08-18T12:31:25+00:00","author":{"@id":"https:\/\/www.wikitechy.com\/interview-questions\/#\/schema\/person\/f785ba3ecc599133e65ab6138042a3e4"},"description":"What is a Virtual Destructor ? - In C++ destructor is a member function of a class used to free the space occupied by or delete an object of the class that goes out of scope.","inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.wikitechy.com\/interview-questions\/cpp\/what-is-a-virtual-destructor\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.wikitechy.com\/interview-questions\/cpp\/what-is-a-virtual-destructor\/#primaryimage","url":"https:\/\/www.wikitechy.com\/interview-questions\/wp-content\/uploads\/2022\/08\/virtual-destructor.png","contentUrl":"https:\/\/www.wikitechy.com\/interview-questions\/wp-content\/uploads\/2022\/08\/virtual-destructor.png","width":1394,"height":391},{"@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\/4781","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=4781"}],"version-history":[{"count":2,"href":"https:\/\/www.wikitechy.com\/interview-questions\/wp-json\/wp\/v2\/posts\/4781\/revisions"}],"predecessor-version":[{"id":4784,"href":"https:\/\/www.wikitechy.com\/interview-questions\/wp-json\/wp\/v2\/posts\/4781\/revisions\/4784"}],"wp:attachment":[{"href":"https:\/\/www.wikitechy.com\/interview-questions\/wp-json\/wp\/v2\/media?parent=4781"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wikitechy.com\/interview-questions\/wp-json\/wp\/v2\/categories?post=4781"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wikitechy.com\/interview-questions\/wp-json\/wp\/v2\/tags?post=4781"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}