<?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>constructor and destructor in c++ - Wikitechy</title>
	<atom:link href="https://www.wikitechy.com/interview-questions/tag/constructor-and-destructor-in-c/feed/" rel="self" type="application/rss+xml" />
	<link>https://www.wikitechy.com/interview-questions/tag/constructor-and-destructor-in-c/</link>
	<description>Interview Questions</description>
	<lastBuildDate>Thu, 18 Aug 2022 10:51:24 +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>constructor and destructor in c++ - Wikitechy</title>
	<link>https://www.wikitechy.com/interview-questions/tag/constructor-and-destructor-in-c/</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>What is destructor in C++ ?</title>
		<link>https://www.wikitechy.com/interview-questions/cpp/what-is-destructor-in-cpp/</link>
					<comments>https://www.wikitechy.com/interview-questions/cpp/what-is-destructor-in-cpp/#respond</comments>
		
		<dc:creator><![CDATA[webmaster]]></dc:creator>
		<pubDate>Thu, 18 Aug 2022 10:51:24 +0000</pubDate>
				<category><![CDATA[C++]]></category>
		<category><![CDATA[c++ destructor]]></category>
		<category><![CDATA[c++ destructors]]></category>
		<category><![CDATA[characteristics of destructors in c++]]></category>
		<category><![CDATA[constructor and destructor in c++]]></category>
		<category><![CDATA[destructor in c++]]></category>
		<category><![CDATA[destructor in c++ example program with output]]></category>
		<category><![CDATA[destructors in c++]]></category>
		<category><![CDATA[how to call destructor C++]]></category>
		<category><![CDATA[types of destructor in c++]]></category>
		<category><![CDATA[use of destructor in c++]]></category>
		<category><![CDATA[what is destructor in c++]]></category>
		<guid isPermaLink="false">https://www.wikitechy.com/interview-questions/?p=4774</guid>

					<description><![CDATA[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 [&#8230;]]]></description>
										<content:encoded><![CDATA[<ul>
<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>
<li style="text-align: justify;">A destructor meaning is the last function that is going to be called before an object is destroyed.</li>
<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>
<li style="text-align: justify;">When the objects are destroyed, destructor function is automatically invoked.</li>
<li style="text-align: justify;">Destructor does not have arguments and it cannot be declared as constant or static.</li>
<li style="text-align: justify;">Destructor cannot become a member of the union while with an object of a class.</li>
<li style="text-align: justify;">The programmer cannot access the address of destructor and in public section of class destructor should be declared.</li>
<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>
<li style="text-align: justify;">Destructors have same name as the class preceded by a tilde (~) it doesn’t take any argument and return anything.</li>
</ul>
<h2 id="sample-code" style="text-align: justify;">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">#include&lt;iostream&gt;<br/>               using namespace std;<br/>              class Demo {<br/>                   private:<br/>                   int num1, num2;<br/>                   public:<br/>                   	Demo(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>
<h2 id="output" style="text-align: justify;">Output</h2>
<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>
]]></content:encoded>
					
					<wfw:commentRss>https://www.wikitechy.com/interview-questions/cpp/what-is-destructor-in-cpp/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>What is Constructor in C++ ?</title>
		<link>https://www.wikitechy.com/interview-questions/cpp/what-is-constructor-in-cpp/</link>
					<comments>https://www.wikitechy.com/interview-questions/cpp/what-is-constructor-in-cpp/#respond</comments>
		
		<dc:creator><![CDATA[webmaster]]></dc:creator>
		<pubDate>Thu, 18 Aug 2022 10:41:19 +0000</pubDate>
				<category><![CDATA[C++]]></category>
		<category><![CDATA[c++ class constructor and destructor]]></category>
		<category><![CDATA[c++ constructors]]></category>
		<category><![CDATA[constructor and destructor in c++]]></category>
		<category><![CDATA[Constructor in c++ and its types]]></category>
		<category><![CDATA[constructor overloading in c++]]></category>
		<category><![CDATA[constructors in c++]]></category>
		<category><![CDATA[types of constructor in c++]]></category>
		<category><![CDATA[types of constructor in c++ with example]]></category>
		<category><![CDATA[use of constructor in c++]]></category>
		<category><![CDATA[what is copy constructor in c++]]></category>
		<category><![CDATA[what is default constructor in c++]]></category>
		<category><![CDATA[what is parameterized constructor in c++]]></category>
		<guid isPermaLink="false">https://www.wikitechy.com/interview-questions/?p=4768</guid>

					<description><![CDATA[In C++ constructor is a special type of member function of a class which initializes objects of a class. Constructor is automatically called when object is created in C++. It does not have any return type because it is special member function of the class. In public section of class and it has same name [&#8230;]]]></description>
										<content:encoded><![CDATA[<ul>
<li style="text-align: justify;">In C++ constructor is a special type of member function of a class which initializes objects of a class.</li>
<li style="text-align: justify;">Constructor is automatically called when object is created in C++.</li>
<li style="text-align: justify;">It does not have any return type because it is special member function of the class.</li>
<li style="text-align: justify;">In public section of class and it has same name as the class itself.</li>
<li style="text-align: justify;">Copy and parameterized constructors have input arguments, by default constructors don’t have input argument however.</li>
<li style="text-align: justify;">C++ compiler generates a default constructor for object, if we do not specify a constructor.</li>
<li style="text-align: justify;">In constructor there are three types, they are <strong>default constructor, parameterized constructor, copy constructor.</strong></li>
</ul>
<p><img decoding="async" class="alignnone size-full wp-image-4772 aligncenter" src="https://www.wikitechy.com/interview-questions/wp-content/uploads/2022/08/constructor-in-c.png" alt="" width="1759" height="709" srcset="https://www.wikitechy.com/interview-questions/wp-content/uploads/2022/08/constructor-in-c.png 1759w, https://www.wikitechy.com/interview-questions/wp-content/uploads/2022/08/constructor-in-c-300x121.png 300w, https://www.wikitechy.com/interview-questions/wp-content/uploads/2022/08/constructor-in-c-1024x413.png 1024w, https://www.wikitechy.com/interview-questions/wp-content/uploads/2022/08/constructor-in-c-768x310.png 768w, https://www.wikitechy.com/interview-questions/wp-content/uploads/2022/08/constructor-in-c-1536x619.png 1536w, https://www.wikitechy.com/interview-questions/wp-content/uploads/2022/08/constructor-in-c-390x157.png 390w, https://www.wikitechy.com/interview-questions/wp-content/uploads/2022/08/constructor-in-c-820x331.png 820w, https://www.wikitechy.com/interview-questions/wp-content/uploads/2022/08/constructor-in-c-1180x476.png 1180w" sizes="(max-width: 1759px) 100vw, 1759px" /></p>
<h2 id="default-constructor">Default Constructor</h2>
<ul>
<li>It is the constructor which has no parameters and doesn’t take any argument.</li>
</ul>
<h3 id="sample-code"><strong>Sample Code</strong></h3>
<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/><br/>class construct<br/><br/>{<br/><br/>public:<br/><br/>    int a, b;<br/><br/><br/><br/><br/>    // Default Constructor<br/><br/>    construct()<br/><br/>    {<br/><br/>        a = 30;<br/><br/>        b = 40;<br/><br/>    }<br/><br/>};<br/><br/><br/><br/><br/>int main()<br/><br/>{<br/><br/>    // Default constructor called automatically<br/><br/>    // when the object is created<br/><br/>    construct c;<br/><br/>    cout &lt;&lt; &quot;a: &quot; &lt;&lt; c.a &lt;&lt; endl<br/><br/>         &lt;&lt; &quot;b: &quot; &lt;&lt; c.b;<br/><br/>    return 1;<br/><br/>}</code></pre> </div>
<h3 id="output"><strong>Output</strong></h3>
<p><img decoding="async" class="alignnone size-full wp-image-4769" src="https://www.wikitechy.com/interview-questions/wp-content/uploads/2022/08/default-constructor.png" alt="" width="969" height="178" srcset="https://www.wikitechy.com/interview-questions/wp-content/uploads/2022/08/default-constructor.png 969w, https://www.wikitechy.com/interview-questions/wp-content/uploads/2022/08/default-constructor-300x55.png 300w, https://www.wikitechy.com/interview-questions/wp-content/uploads/2022/08/default-constructor-768x141.png 768w, https://www.wikitechy.com/interview-questions/wp-content/uploads/2022/08/default-constructor-390x72.png 390w, https://www.wikitechy.com/interview-questions/wp-content/uploads/2022/08/default-constructor-820x151.png 820w" sizes="(max-width: 969px) 100vw, 969px" /></p>
<h2 id="parameterized-constructor">Parameterized Constructor</h2>
<ul>
<li>It is possible to pass arguments to constructors and these arguments help initialize an object when it is created typically.</li>
</ul>
<h3 id="sample-code-2"><strong>Sample Code</strong></h3>
<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/><br/>class Point<br/><br/>{<br/><br/>private:<br/><br/>    int x, y;<br/><br/><br/><br/><br/>public:<br/><br/>    // Parameterized Constructor<br/><br/>    Point(int x1, int y1)<br/><br/>    {<br/><br/>        x = x1;<br/><br/>        y = y1;<br/><br/>    }<br/><br/><br/><br/><br/>    int getX()<br/><br/>    {<br/><br/>        return x;<br/><br/>    }<br/><br/>    int getY()<br/><br/>    {<br/><br/>        return y;<br/><br/>    }<br/><br/>};<br/><br/><br/><br/><br/>int main()<br/><br/>{<br/><br/>    // Constructor called<br/><br/>    Point p1(10, 15);<br/><br/><br/><br/><br/>    // Access values assigned by constructor<br/><br/>    cout &lt;&lt; &quot;p1.x = &quot; &lt;&lt; p1.getX() &lt;&lt; &quot;, p1.y = &quot; &lt;&lt; p1.getY();<br/><br/><br/><br/><br/>    return 0;<br/><br/>}</code></pre> </div>
<h3 id="output-2"><strong> O</strong><strong>utput</strong></h3>
<p><img loading="lazy" decoding="async" class="alignnone size-full wp-image-4770" src="https://www.wikitechy.com/interview-questions/wp-content/uploads/2022/08/parameterized-constructor.png" alt="" width="934" height="181" srcset="https://www.wikitechy.com/interview-questions/wp-content/uploads/2022/08/parameterized-constructor.png 934w, https://www.wikitechy.com/interview-questions/wp-content/uploads/2022/08/parameterized-constructor-300x58.png 300w, https://www.wikitechy.com/interview-questions/wp-content/uploads/2022/08/parameterized-constructor-768x149.png 768w, https://www.wikitechy.com/interview-questions/wp-content/uploads/2022/08/parameterized-constructor-390x76.png 390w, https://www.wikitechy.com/interview-questions/wp-content/uploads/2022/08/parameterized-constructor-820x159.png 820w" sizes="(max-width: 934px) 100vw, 934px" /></p>
<h2 id="copy-constructor">Copy Constructor</h2>
<ul>
<li>It is a member function which initializes an object using another object of the same class.</li>
</ul>
<h3 id="sample-code-3"><strong>Sample Code</strong></h3>
<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/><br/>class point<br/><br/>{<br/><br/>private:<br/><br/>  double x, y;<br/><br/><br/><br/><br/>public:<br/><br/>   <br/><br/>  // Non-default Constructor &amp;<br/><br/>  // default Constructor<br/><br/>  point (double px, double py)<br/><br/>  {<br/><br/>    x = px, y = py;<br/><br/>  }<br/><br/>};<br/><br/><br/><br/><br/>int main(void)<br/><br/>{<br/><br/><br/><br/><br/>  // Define an array of size<br/><br/>  // 10 &amp; of type point<br/><br/>  // This line will cause error<br/><br/>  point a[10];<br/><br/><br/><br/><br/>  // Remove above line and program<br/><br/>  // will compile without error<br/><br/>  point b = point(5, 6);<br/><br/>}</code></pre> </div>
<h3 id="output-3"><strong>Output</strong></h3>
<p><img loading="lazy" decoding="async" class="alignnone size-full wp-image-4771" src="https://www.wikitechy.com/interview-questions/wp-content/uploads/2022/08/copy-constructor.png" alt="" width="1053" height="189" srcset="https://www.wikitechy.com/interview-questions/wp-content/uploads/2022/08/copy-constructor.png 1053w, https://www.wikitechy.com/interview-questions/wp-content/uploads/2022/08/copy-constructor-300x54.png 300w, https://www.wikitechy.com/interview-questions/wp-content/uploads/2022/08/copy-constructor-1024x184.png 1024w, https://www.wikitechy.com/interview-questions/wp-content/uploads/2022/08/copy-constructor-768x138.png 768w, https://www.wikitechy.com/interview-questions/wp-content/uploads/2022/08/copy-constructor-390x70.png 390w, https://www.wikitechy.com/interview-questions/wp-content/uploads/2022/08/copy-constructor-820x147.png 820w" sizes="(max-width: 1053px) 100vw, 1053px" /></p>
]]></content:encoded>
					
					<wfw:commentRss>https://www.wikitechy.com/interview-questions/cpp/what-is-constructor-in-cpp/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>
