<?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>Coding Friends &#187; cpp</title>
	<atom:link href="http://www.codingfriends.com/index.php/tag/cpp/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.codingfriends.com</link>
	<description>Coding Friends, place for developers.</description>
	<lastBuildDate>Sun, 04 Dec 2011 21:11:08 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>Polymorphism</title>
		<link>http://www.codingfriends.com/index.php/2010/02/02/polymorphism/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=polymorphism</link>
		<comments>http://www.codingfriends.com/index.php/2010/02/02/polymorphism/#comments</comments>
		<pubDate>Tue, 02 Feb 2010 10:12:48 +0000</pubDate>
		<dc:creator>genux</dc:creator>
				<category><![CDATA[C / C++]]></category>
		<category><![CDATA[cpp]]></category>
		<category><![CDATA[interface]]></category>
		<category><![CDATA[polymorphism]]></category>

		<guid isPermaLink="false">http://www.codingfriends.com/?p=633</guid>
		<description><![CDATA[Polymorphism means that you are deriving from a base class to create a new class, and the polymorphism is when you derive from the base class and implement a interface (virtual function in c++ speak as such) functions. In C++ a virtual function would be virtual void printName&#40;&#41; = 0; the = 0 means to [...]]]></description>
			<content:encoded><![CDATA[<p>Polymorphism means that you are deriving from a base class to create a new class, and the polymorphism is when you derive from the base class and implement a interface (virtual function in c++ speak as such) functions.</p>
<p>In C++ a virtual function would be</p>

<div class="wp_syntax"><div class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #0000ff;">virtual</span> <span style="color: #0000ff;">void</span> printName<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span> <span style="color: #000080;">=</span> <span style="color: #0000dd;">0</span><span style="color: #008080;">;</span></pre></div></div>

<p>the = 0 means to set the virtual function to point to nothing (pure virtual function).</p>
<p>You could put some code within {} and still call it a virtual function, but that is taking away from the interface idea. (Java,C#  and PHP use the interface structures)</p>
<p>In the code below, I have a const string (constant string) and as we know a constant string cannot be altered so to define this string when the constructor is called you place the variable name after the constructor and set it up to the constant value, e.g..</p>

<div class="wp_syntax"><div class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #0000ff;">class</span> Shape
<span style="color: #008000;">&#123;</span>
     <span style="color: #0000ff;">const</span> string name<span style="color: #008080;">;</span>
      <span style="color: #666666;">// constructor</span>
     Shape<span style="color: #008000;">&#40;</span><span style="color: #0000ff;">const</span> string<span style="color: #000040;">&amp;</span> constructorName<span style="color: #008000;">&#41;</span> <span style="color: #008080;">:</span> name<span style="color: #008000;">&#40;</span>constructorName<span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span><span style="color: #008000;">&#125;</span><span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span></pre></div></div>

<p>This will setup the constant string when the Shape class has been created on the heap (memory allocation).</p>
<p>To call a parent class, Shape in this instance, constructor you just do something very similar to the setting of the constant string, you place it after the constructor in the subclass and put the parent class that you want to call its constructor after the &#8220;:&#8221; as</p>

<div class="wp_syntax"><div class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #0000ff;">class</span> Rectangle 
<span style="color: #008000;">&#123;</span>
      Rectangle<span style="color: #008000;">&#40;</span><span style="color: #0000ff;">const</span> string<span style="color: #000040;">&amp;</span> constructorName<span style="color: #008000;">&#41;</span> <span style="color: #008080;">:</span> Shape<span style="color: #008000;">&#40;</span>constructorName<span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span><span style="color: #008000;">&#125;</span>
<span style="color: #008000;">&#125;</span></pre></div></div>

<p>you can place more code into the {} for the Rectangle constructor still, but the &#8220;Shape(constructorName) is calling the parent class &#8220;Shape&#8221; constructor.</p>
<p>If you do not define the virtual pure function within the derived class, when you try to compile the error would be something similar to</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">poly.cpp:<span style="color: #000000;">46</span>: error: cannot allocate an object of abstract <span style="color: #7a0874; font-weight: bold;">type</span> ‘Circle’
poly.cpp:<span style="color: #000000;">21</span>: note:   because the following virtual functions are pure within ‘Circle’:
poly.cpp:<span style="color: #000000;">11</span>: note:      virtual void Shape::printName<span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #7a0874; font-weight: bold;">&#41;</span></pre></div></div>

<p>Because the Circle was not implementing what the parent class has defined as a pure virtual function.</p>
<p>In the code below I have placed some error values if you do not put &#8220;public&#8221; in the same places.  These are typical errors that come from access to the objects internals that you are trying to access.</p>

<div class="wp_syntax"><div class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #339900;">#include &lt;iostream&gt;</span>
<span style="color: #339900;">#include &lt;string&gt;</span>
&nbsp;
<span style="color: #0000ff;">using</span> <span style="color: #0000ff;">namespace</span> std<span style="color: #008080;">;</span>
&nbsp;
<span style="color: #0000ff;">class</span> Shape 
<span style="color: #008000;">&#123;</span>
  <span style="color: #0000ff;">public</span><span style="color: #008080;">:</span>
      Shape<span style="color: #008000;">&#40;</span><span style="color: #0000ff;">const</span> string<span style="color: #000040;">&amp;</span> conName<span style="color: #008000;">&#41;</span> <span style="color: #008080;">:</span> name<span style="color: #008000;">&#40;</span>conName<span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span><span style="color: #008000;">&#125;</span><span style="color: #008080;">;</span> 
      <span style="color: #666666;">// create a virtual function so that it is implemented in the subclass</span>
      <span style="color: #0000ff;">virtual</span> <span style="color: #0000ff;">void</span> printName<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span> <span style="color: #000080;">=</span> <span style="color: #0000dd;">0</span><span style="color: #008080;">;</span>
&nbsp;
  <span style="color: #666666;">// protected, so it does not allow direct access, but subclasses can access it.</span>
  <span style="color: #0000ff;">protected</span><span style="color: #008080;">:</span>
      <span style="color: #0000ff;">const</span> string name<span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span><span style="color: #008080;">;</span>
&nbsp;
<span style="color: #666666;">// need to be &quot;public Shape&quot; otherwise</span>
<span style="color: #666666;">// error: ‘Shape’ is an inaccessible base of ‘Circle’</span>
<span style="color: #666666;">// it will not allow access to the Shape class.</span>
<span style="color: #0000ff;">class</span> Circle <span style="color: #008080;">:</span> <span style="color: #0000ff;">public</span> Shape
<span style="color: #008000;">&#123;</span>
  <span style="color: #666666;">// you need to specify public for Cat to access Animal string name because that is protected</span>
  <span style="color: #0000ff;">public</span> <span style="color: #008080;">:</span>
      Circle <span style="color: #008000;">&#40;</span><span style="color: #0000ff;">const</span> string<span style="color: #000040;">&amp;</span> conName<span style="color: #008000;">&#41;</span> <span style="color: #008080;">:</span> Shape<span style="color: #008000;">&#40;</span>conName<span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span><span style="color: #008000;">&#125;</span><span style="color: #008080;">;</span>
      <span style="color: #0000ff;">void</span> printName<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>
      <span style="color: #008000;">&#123;</span>
	  <span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> name <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot; class&quot;</span> <span style="color: #000080;">&lt;&lt;</span> endl<span style="color: #008080;">;</span>
      <span style="color: #008000;">&#125;</span>
<span style="color: #008000;">&#125;</span><span style="color: #008080;">;</span>
&nbsp;
<span style="color: #0000ff;">class</span> Rectangle<span style="color: #008080;">:</span> <span style="color: #0000ff;">public</span> Shape
<span style="color: #008000;">&#123;</span>
  <span style="color: #666666;">// you need to specify public for Dog to access Animal string name because that is protected</span>
  <span style="color: #666666;">// error: ‘Rectangle::Rectangle(const std::string&amp;)’ is private</span>
  <span style="color: #0000ff;">public</span><span style="color: #008080;">:</span> 
      Rectangle<span style="color: #008000;">&#40;</span><span style="color: #0000ff;">const</span> string<span style="color: #000040;">&amp;</span> conName<span style="color: #008000;">&#41;</span> <span style="color: #008080;">:</span> Shape<span style="color: #008000;">&#40;</span>conName<span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span><span style="color: #008000;">&#125;</span><span style="color: #008080;">;</span>
      <span style="color: #0000ff;">void</span> printName<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>
      <span style="color: #008000;">&#123;</span>
	  <span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> name <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot; class&quot;</span> <span style="color: #000080;">&lt;&lt;</span> endl<span style="color: #008080;">;</span>
      <span style="color: #008000;">&#125;</span>
<span style="color: #008000;">&#125;</span><span style="color: #008080;">;</span>
&nbsp;
<span style="color: #0000ff;">int</span> main<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
    Shape<span style="color: #000040;">*</span> shapes<span style="color: #008000;">&#91;</span><span style="color: #0000dd;">2</span><span style="color: #008000;">&#93;</span><span style="color: #008080;">;</span>
    shapes<span style="color: #008000;">&#91;</span><span style="color: #0000dd;">0</span><span style="color: #008000;">&#93;</span> <span style="color: #000080;">=</span> <span style="color: #0000dd;">new</span> Circle<span style="color: #008000;">&#40;</span><span style="color: #FF0000;">&quot;Cirlce&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
    shapes<span style="color: #008000;">&#91;</span><span style="color: #0000dd;">1</span><span style="color: #008000;">&#93;</span> <span style="color: #000080;">=</span> <span style="color: #0000dd;">new</span> Rectangle<span style="color: #008000;">&#40;</span><span style="color: #FF0000;">&quot;Rectangle&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
&nbsp;
    <span style="color: #0000ff;">for</span> <span style="color: #008000;">&#40;</span><span style="color: #0000ff;">int</span> i <span style="color: #000080;">=</span><span style="color: #0000dd;">0</span><span style="color: #008080;">;</span> i <span style="color: #000080;">&lt;</span> <span style="color: #0000dd;">2</span> <span style="color: #008080;">;</span> i<span style="color: #000040;">++</span><span style="color: #008000;">&#41;</span>
    <span style="color: #008000;">&#123;</span>
      shapes<span style="color: #008000;">&#91;</span>i<span style="color: #008000;">&#93;</span><span style="color: #000040;">-</span><span style="color: #000080;">&gt;</span>printName<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
      <span style="color: #666666;">// clean up the memory space.</span>
      <span style="color: #0000dd;">delete</span><span style="color: #008000;">&#40;</span>shapes<span style="color: #008000;">&#91;</span>i<span style="color: #008000;">&#93;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
    <span style="color: #008000;">&#125;</span>
    <span style="color: #0000ff;">return</span> <span style="color: #0000dd;">0</span><span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span></pre></div></div>

<p>and here is the output</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">Cirlce class
Rectangle class</pre></div></div>

<p>So in the example code above, the polymorphism is when for example the Rectangle class is defining the &#8220;interface&#8221; code for printName method.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.codingfriends.com/index.php/2010/02/02/polymorphism/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

