<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	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/"
		>
<channel>
	<title>Comments on: C++ Classes &#8211; Constructors and destructors</title>
	<atom:link href="http://www.codingunit.com/cplusplus-tutorial-classes-constructors-and-destructors/feed" rel="self" type="application/rss+xml" />
	<link>http://www.codingunit.com/cplusplus-tutorial-classes-constructors-and-destructors</link>
	<description>CodingUnit is your online resource for learning to program. Tutorials on C, C++, PHP, Python, MySQL, Java, JQuery, Opengl, DirectX and much more!</description>
	<lastBuildDate>Mon, 06 Feb 2012 06:08:16 +0000</lastBuildDate>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=</generator>
	<item>
		<title>By: mebrahten</title>
		<link>http://www.codingunit.com/cplusplus-tutorial-classes-constructors-and-destructors/comment-page-1#comment-4338</link>
		<dc:creator>mebrahten</dc:creator>
		<pubDate>Tue, 06 Dec 2011 12:57:49 +0000</pubDate>
		<guid isPermaLink="false">http://localhost/?p=519#comment-4338</guid>
		<description>it is nice note</description>
		<content:encoded><![CDATA[<p>it is nice note</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: admin</title>
		<link>http://www.codingunit.com/cplusplus-tutorial-classes-constructors-and-destructors/comment-page-1#comment-1804</link>
		<dc:creator>admin</dc:creator>
		<pubDate>Sat, 14 Aug 2010 21:36:47 +0000</pubDate>
		<guid isPermaLink="false">http://localhost/?p=519#comment-1804</guid>
		<description>@Crazy - We working on a tutorial to explain copy constructor and assignment operators. Give us a couple of days to come-up with some examples.</description>
		<content:encoded><![CDATA[<p>@Crazy &#8211; We working on a tutorial to explain copy constructor and assignment operators. Give us a couple of days to come-up with some examples.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Crazy</title>
		<link>http://www.codingunit.com/cplusplus-tutorial-classes-constructors-and-destructors/comment-page-1#comment-1803</link>
		<dc:creator>Crazy</dc:creator>
		<pubDate>Fri, 13 Aug 2010 12:51:05 +0000</pubDate>
		<guid isPermaLink="false">http://localhost/?p=519#comment-1803</guid>
		<description>Hi,
Can you please explain copy constructor and its usage
Thanks in advance</description>
		<content:encoded><![CDATA[<p>Hi,<br />
Can you please explain copy constructor and its usage<br />
Thanks in advance</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: admin</title>
		<link>http://www.codingunit.com/cplusplus-tutorial-classes-constructors-and-destructors/comment-page-1#comment-869</link>
		<dc:creator>admin</dc:creator>
		<pubDate>Sat, 06 Mar 2010 22:39:22 +0000</pubDate>
		<guid isPermaLink="false">http://localhost/?p=519#comment-869</guid>
		<description>@Rugnar, below you will find an example as requested:

#include &lt;iostream&gt;
using namespace std;

// A class CRectArea with a constructor and descructor
// to determine the area of a rectangle.
class CRectArea {
    int *width, *height;
  public:
    CRectArea (int,int);
    ~CRectArea ();
    int areaofrect () {
         return (*width * *height);
    }
};

// Constructor
CRectArea::CRectArea (int x, int y) {
  
  //Dynamically allocate some memory
  width = new int;
  height = new int;

  *width = x;
  *height = y;
}

// Destructor
CRectArea::~CRectArea () {
  //Delete the allocate memory
  delete width;
  delete height;
}

int main () {
  CRectArea myrectangle (2,2);
  cout &lt;&lt; &quot;The area of the rectangle is: &quot; &lt;&lt; myrectangle.areaofrect() &lt;&lt; endl;
  return 0;
}

In this example we want to determine the area of a rectangle. We use a constructor, where we dynamically allocate some memory for the width and height. In the destructor we delete the memory, we dynamically allocated in the constructor. Now, every time an instance of a class is created the constructor method is called (in this case: CRectArea myrectangle (2,2); The destructor is called near the end of the program. 

Where are destructors commonly used for? They are usually used to &quot;clean up&quot; when an object is no longer necessary, to &quot;clean up&quot; the mess you have create during a program.
Hope this helps, good luck!</description>
		<content:encoded><![CDATA[<p>@Rugnar, below you will find an example as requested:</p>
<p>#include &lt;iostream&gt;<br />
using namespace std;</p>
<p>// A class CRectArea with a constructor and descructor<br />
// to determine the area of a rectangle.<br />
class CRectArea {<br />
    int *width, *height;<br />
  public:<br />
    CRectArea (int,int);<br />
    ~CRectArea ();<br />
    int areaofrect () {<br />
         return (*width * *height);<br />
    }<br />
};</p>
<p>// Constructor<br />
CRectArea::CRectArea (int x, int y) {</p>
<p>  //Dynamically allocate some memory<br />
  width = new int;<br />
  height = new int;</p>
<p>  *width = x;<br />
  *height = y;<br />
}</p>
<p>// Destructor<br />
CRectArea::~CRectArea () {<br />
  //Delete the allocate memory<br />
  delete width;<br />
  delete height;<br />
}</p>
<p>int main () {<br />
  CRectArea myrectangle (2,2);<br />
  cout &lt;&lt; &#8220;The area of the rectangle is: &#8221; &lt;&lt; myrectangle.areaofrect() &lt;&lt; endl;<br />
  return 0;<br />
}</p>
<p>In this example we want to determine the area of a rectangle. We use a constructor, where we dynamically allocate some memory for the width and height. In the destructor we delete the memory, we dynamically allocated in the constructor. Now, every time an instance of a class is created the constructor method is called (in this case: CRectArea myrectangle (2,2); The destructor is called near the end of the program. </p>
<p>Where are destructors commonly used for? They are usually used to &#8220;clean up&#8221; when an object is no longer necessary, to &#8220;clean up&#8221; the mess you have create during a program.<br />
Hope this helps, good luck!</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Rugnar</title>
		<link>http://www.codingunit.com/cplusplus-tutorial-classes-constructors-and-destructors/comment-page-1#comment-868</link>
		<dc:creator>Rugnar</dc:creator>
		<pubDate>Sat, 06 Mar 2010 20:06:02 +0000</pubDate>
		<guid isPermaLink="false">http://localhost/?p=519#comment-868</guid>
		<description>Please, give example for usage of destructor.</description>
		<content:encoded><![CDATA[<p>Please, give example for usage of destructor.</p>
]]></content:encoded>
	</item>
</channel>
</rss>

