<?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>Knowledge Sharing &#187; C++</title>
	<atom:link href="http://blog.danigunawan.com/tag/c-plus-plus/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.danigunawan.com</link>
	<description>"Say it... although a word..."</description>
	<lastBuildDate>Mon, 04 Jan 2010 10:17:12 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Method Overloading</title>
		<link>http://blog.danigunawan.com/programming/method-overloading/</link>
		<comments>http://blog.danigunawan.com/programming/method-overloading/#comments</comments>
		<pubDate>Thu, 15 Nov 2007 01:58:00 +0000</pubDate>
		<dc:creator>Dagu</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[C++]]></category>
		<category><![CDATA[Method]]></category>

		<guid isPermaLink="false">http://blog.danigunawan.com/2007/11/15/method-overloading/</guid>
		<description><![CDATA[Method overloading is used to make a few methods (functions) with the SAME NAME and do the SIMILAR TASK. It helps programmer to remember methods which have similar task. For example is multiplication method. The first method needs two arguments and another needs three arguments (similar task, that is multiply two arguments, the different is [...]


No related posts.]]></description>
			<content:encoded><![CDATA[<div style="text-align: justify;font-family:georgia;">Method overloading is used to make a few methods (functions) with the SAME NAME and do the SIMILAR TASK. It helps programmer to remember methods which have similar task. For example is multiplication method. The first method needs two arguments and another needs three arguments (similar task, that is multiply two arguments, the different is amount of the arguments only). If we make that two methods using two different names, it will make redundant method names. For examples, the method name for two arguments multiplication, the name is <strong>multiply1(number1, number2)</strong>. For three arguments multiplication, the name is <strong>multiply2(number1, number2, number3)</strong>. That&#8217;s a trouble! How if there are ten types of multiplication? But, compare if we make the method name <strong>multiply(number1, number2) </strong>and <strong>multiply(number1, number2, number3)</strong>. We only need to remember the amount of arguments from method multiply.</p>
<p>So, how does C++ compiler differentiate those two methods? C++ compiler observes the amount, data type and argument sequence. It&#8217;s clear that the compiler use parameters to differentiate methods which have the same name. Not differentiate them with their return type.</p></div>
<div style="text-align: justify;font-family:georgia;">
<p><span id="more-90"></span></p>
<p>To make method overloading, we need at least one of these three conditions below;</p>
<ul>
<li>have different argument amount</li>
<li>have different argument data type</li>
<li>have different argument sequence</li>
</ul>
<div style="text-align: justify;font-family:georgia;"><strong>Example #1 (different argument amount):</strong></div>
<p>[sourcecode language='cpp']// method multiply with two arguments (number1 dan number2)<br />
int multiply(int number1, int number2) {<br />
   return number1*number2<br />
}[/sourcecode]</p>
<pre>// method multiply with three arguments (number1, number2 dan number3)
int multiply(int number1, int number2, int number3) {
   return number1*number2*number3;
}</pre>
<p>Method multiply above are <strong>VALID </strong>because they have <strong>different argument amount</strong>.</p>
<p><span style="font-weight: bold;">Example #2 (different argument data type):</span></p>
<pre>// method showGrade has a char data type argument
void showGrade(char grade) {
   cout &lt;&lt; "Grade (in letter): " &lt;&lt; grade;
}</pre>
<pre>// method showGrade has a int data type argument
void showGrade(int grade) {
   cout &lt;&lt; "Grade (in number): " &lt;&lt; grade;
}</pre>
<p>Method showGrade above are <strong>VALID </strong>because they have <strong>different data type argument </strong>although they have the same argument amount.</p>
<p><span style="font-weight: bold;">Example #3 (different argument sequence):</span></p>
<pre>// method multiply with data type number1 is int
// and number2 is double
double multiply(int number1, double number2) {
   return number1*number2;
}</pre>
<pre>// method multiply with data type number1 is double
// and number2 is int
double multiply(double number1, int number2) {
   return number1*number2;
}</pre>
<p>Those method above are <strong>VALID </strong>because they have <strong>different argument sequence</strong>. In the first multiply method, number1 has int data type then number2 has double data type. In the second method, number1 has double data type and number2 has int data type.</p>
<p><span style="font-weight: bold;">REMEMBER!!!</span><br />
Frequently mistake done by the programmer is assigning the same amount or sequence argument with the different return type.  It will return <span style="font-weight: bold;">SYNTAX ERROR</span> while compiling it.</p>
<p>Wrong example:</p>
<pre>// return type: int
int countAge(int age) {
   return age + 2;
}</pre>
<pre>// return type: void
void countAge(int age) {
   cout&lt;&lt;"Student age after two years = "&lt;&lt;age+2;
}</pre>
<p><span style="font-weight: bold;">SYNTAX ERROR</span>. Method can&#8217;t be <span style="font-style: italic;">overloaded</span> because they have same argument amount with the same data type.</div>


<p>No related posts.</p>]]></content:encoded>
			<wfw:commentRss>http://blog.danigunawan.com/programming/method-overloading/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
