<?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>what is copy constructor in c++ - Wikitechy</title>
	<atom:link href="https://www.wikitechy.com/interview-questions/tag/what-is-copy-constructor-in-c/feed/" rel="self" type="application/rss+xml" />
	<link>https://www.wikitechy.com/interview-questions/tag/what-is-copy-constructor-in-c/</link>
	<description>Interview Questions</description>
	<lastBuildDate>Thu, 18 Aug 2022 10:41:50 +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>what is copy constructor in c++ - Wikitechy</title>
	<link>https://www.wikitechy.com/interview-questions/tag/what-is-copy-constructor-in-c/</link>
	<width>32</width>
	<height>32</height>
</image> 
	<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 fetchpriority="high" 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 << &quot;a: &quot; << c.a << endl<br/><br/>         << &quot;b: &quot; << 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 << &quot;p1.x = &quot; << p1.getX() << &quot;, p1.y = &quot; << 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 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 &<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 & 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>
