<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>use of virtual destructor in c++ - Wikitechy</title>
	<atom:link href="https://www.wikitechy.com/interview-questions/tag/use-of-virtual-destructor-in-c/feed/" rel="self" type="application/rss+xml" />
	<link>https://www.wikitechy.com/interview-questions/tag/use-of-virtual-destructor-in-c/</link>
	<description>Interview Questions</description>
	<lastBuildDate>Thu, 18 Aug 2022 12:31:25 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.9.4</generator>

<image>
	<url>https://www.wikitechy.com/interview-questions/wp-content/uploads/2025/10/cropped-wikitechy-icon-32x32.png</url>
	<title>use of virtual destructor in c++ - Wikitechy</title>
	<link>https://www.wikitechy.com/interview-questions/tag/use-of-virtual-destructor-in-c/</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>What is a Virtual Destructor ?</title>
		<link>https://www.wikitechy.com/interview-questions/cpp/what-is-a-virtual-destructor/</link>
					<comments>https://www.wikitechy.com/interview-questions/cpp/what-is-a-virtual-destructor/#respond</comments>
		
		<dc:creator><![CDATA[webmaster]]></dc:creator>
		<pubDate>Thu, 18 Aug 2022 12:31:05 +0000</pubDate>
				<category><![CDATA[C++]]></category>
		<category><![CDATA[c++ virtual destructor]]></category>
		<category><![CDATA[can we have virtual destructor]]></category>
		<category><![CDATA[pure virtual destructor in c++]]></category>
		<category><![CDATA[use of virtual destructor in c++]]></category>
		<category><![CDATA[virtual destructor]]></category>
		<category><![CDATA[virtual destructor c++]]></category>
		<category><![CDATA[virtual destructor default]]></category>
		<category><![CDATA[virtual destructor in c++]]></category>
		<category><![CDATA[what is virtual destructor]]></category>
		<category><![CDATA[what is virtual destructor in c++]]></category>
		<category><![CDATA[when to use virtual destructors]]></category>
		<category><![CDATA[why do we need virtual destructors]]></category>
		<guid isPermaLink="false">https://www.wikitechy.com/interview-questions/?p=4781</guid>

					<description><![CDATA[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 (~)sign before its function name. It is [&#8230;]]]></description>
										<content:encoded><![CDATA[<ul>
<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>
<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 <strong>(~)</strong>sign before its function name.</li>
<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>
<li style="text-align: justify;">A derived class or base class destructor use the <strong>virtual </strong>keyword that ensures both classes destructor will be called at run time.</li>
<li style="text-align: justify;">First it calls the derived class and then base class to release the space occupied by both destructors.</li>
<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’s destructor function.</li>
<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>
<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>
<li style="text-align: justify;">It executes the base class’s constructor() function and then moves to the derived class’s constructor() function and deletes the pointer object occupied by the base class&#8217;s destructor and the derived class&#8217;s destructor.</li>
<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>
</ul>
<h2 id="sample-code">Sample Code</h2>
<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>
<h2 id="output">Output</h2>
<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>
<p>&nbsp;</p>
]]></content:encoded>
					
					<wfw:commentRss>https://www.wikitechy.com/interview-questions/cpp/what-is-a-virtual-destructor/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>
