<?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++ Overloading and Operator Overloading</title>
	<atom:link href="http://www.codingunit.com/cplusplus-tutorial-overloading-and-operator-overloading/feed" rel="self" type="application/rss+xml" />
	<link>http://www.codingunit.com/cplusplus-tutorial-overloading-and-operator-overloading</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: Chaitanya</title>
		<link>http://www.codingunit.com/cplusplus-tutorial-overloading-and-operator-overloading/comment-page-1#comment-4340</link>
		<dc:creator>Chaitanya</dc:creator>
		<pubDate>Tue, 06 Dec 2011 17:49:19 +0000</pubDate>
		<guid isPermaLink="false">http://localhost/?p=528#comment-4340</guid>
		<description>What is the code for the below given operation using Operator Overloading..?
Class Abc{};
main()
{ Abc ob1,ob2(10,20),ob3(50,60);
Abc ob4=ob1+ob2-ob3;
}</description>
		<content:encoded><![CDATA[<p>What is the code for the below given operation using Operator Overloading..?<br />
Class Abc{};<br />
main()<br />
{ Abc ob1,ob2(10,20),ob3(50,60);<br />
Abc ob4=ob1+ob2-ob3;<br />
}</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: admin</title>
		<link>http://www.codingunit.com/cplusplus-tutorial-overloading-and-operator-overloading/comment-page-1#comment-1978</link>
		<dc:creator>admin</dc:creator>
		<pubDate>Thu, 09 Dec 2010 06:37:55 +0000</pubDate>
		<guid isPermaLink="false">http://localhost/?p=528#comment-1978</guid>
		<description>The ternary operator (?:) in c++ is cannot be overloaded. Most operators can be overloaded by a programmer. The exceptions are: . (dot)  ::  ?:  sizeof
On the Stroustrup: c++ style and technique FAQ website he says:
&lt;blockquote&gt;There is no fundamental reason to disallow overloading of ?:. I just didn&#039;t see the need to introduce the special case of overloading a ternary operator. Note that a function overloading expr1?expr2:expr3 would not be able to guarantee that only one of expr2 and expr3 was executed. &lt;/blockquote&gt;</description>
		<content:encoded><![CDATA[<p>The ternary operator (?:) in c++ is cannot be overloaded. Most operators can be overloaded by a programmer. The exceptions are: . (dot)  ::  ?:  sizeof<br />
On the Stroustrup: c++ style and technique FAQ website he says:</p>
<blockquote><p>There is no fundamental reason to disallow overloading of ?:. I just didn&#8217;t see the need to introduce the special case of overloading a ternary operator. Note that a function overloading expr1?expr2:expr3 would not be able to guarantee that only one of expr2 and expr3 was executed. </p></blockquote>
]]></content:encoded>
	</item>
	<item>
		<title>By: Praveen</title>
		<link>http://www.codingunit.com/cplusplus-tutorial-overloading-and-operator-overloading/comment-page-1#comment-1970</link>
		<dc:creator>Praveen</dc:creator>
		<pubDate>Tue, 07 Dec 2010 10:53:52 +0000</pubDate>
		<guid isPermaLink="false">http://localhost/?p=528#comment-1970</guid>
		<description>I want an example program for ternary operator overloading.</description>
		<content:encoded><![CDATA[<p>I want an example program for ternary operator overloading.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: shrihari</title>
		<link>http://www.codingunit.com/cplusplus-tutorial-overloading-and-operator-overloading/comment-page-1#comment-1763</link>
		<dc:creator>shrihari</dc:creator>
		<pubDate>Fri, 23 Jul 2010 08:13:48 +0000</pubDate>
		<guid isPermaLink="false">http://localhost/?p=528#comment-1763</guid>
		<description>thank u.</description>
		<content:encoded><![CDATA[<p>thank u.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: admin</title>
		<link>http://www.codingunit.com/cplusplus-tutorial-overloading-and-operator-overloading/comment-page-1#comment-1744</link>
		<dc:creator>admin</dc:creator>
		<pubDate>Thu, 22 Jul 2010 16:51:18 +0000</pubDate>
		<guid isPermaLink="false">http://localhost/?p=528#comment-1744</guid>
		<description>@ Shri hari
Besides all sorts of minor issues, such as using of cout instead of cin, conversion from float to int (possible loss of data), etc. 

The main problem is that you are trying to overload functions that only differ in return type. So int area(int) is the same as float area(int). You should get an error like: 
error C2556: &#039;float area(int)&#039; : overloaded function differs only by return type from &#039;int area(int)&#039; 
This is because each overloaded function must have a &lt;strong&gt;distinct&lt;/strong&gt; formal parameter list.

So the overloading problem we can easily fix by using a float area(float), this also fixes the problem of type conversion (and possible loss of data.) Then combat the problem of unwanted type conversion in float area(int,int,int);//area of triangle, we also change the type of the parameter list to float. Then fix the s=(x+y+z)/3; to s=(x+y+z)/2; and other minor changes. This example should compile without warnings or errors:

#include &lt;iostream&gt;
#include &lt;math.h&gt;
using namespace std;

int area(int);//area of square
int area(int,int);//area of rectangle
float area(float);//area of circle
float area(float,float,float);//area of triangle

//area of square
int area(int a) {
	return a*a;
}

//area of rectangle
int area(int l,int b) {
	return l*b;
}

//area of circle
float area(float r) {
	return float((3.14*r*r));
}

//area of triangle
float area(float x,float y,float z) {
	float s;
	s = (x + y + z) / 2; // Not three as you had!
	return(sqrt(s*(s-x)*(s-y)*(s-z)));
}



void main() {
	int a,b;
	float c,d,e;
	
	cout &lt;&lt; &quot;\nInput an integer for area of square:&quot;;
	cin &gt;&gt; a;
	cout &lt;&lt; &quot;\narea=&quot; &lt;&lt; area(a);

	cout &lt;&lt; &quot;\nInput two integers for area of rectangle:&quot;;
	cin &gt;&gt; a &gt;&gt; b;
	cout &lt;&lt; &quot;\narea=&quot; &lt;&lt; area(a,b);

	cout &lt;&lt; &quot;\nInput a float for area of circle:&quot;;
	cin &gt;&gt; c;
	cout &lt;&lt; &quot;\narea=&quot; &lt;&lt; area(c);

	cout &lt;&lt; &quot;\nInput three floats for area of triangle:&quot;;
	cin &gt;&gt; c &gt;&gt; d &gt;&gt; e;
	cout &lt;&lt; &quot;\narea=&quot; &lt;&lt; area(c,d,e);
}

Tip: always try to make your example programs as complete as possible (even if its only for a test) as we did by adding some comment lines and some extra screen output. This will help you in debugging.

We hope that this example helps you!!</description>
		<content:encoded><![CDATA[<p>@ Shri hari<br />
Besides all sorts of minor issues, such as using of cout instead of cin, conversion from float to int (possible loss of data), etc. </p>
<p>The main problem is that you are trying to overload functions that only differ in return type. So int area(int) is the same as float area(int). You should get an error like:<br />
error C2556: &#8216;float area(int)&#8217; : overloaded function differs only by return type from &#8216;int area(int)&#8217;<br />
This is because each overloaded function must have a <strong>distinct</strong> formal parameter list.</p>
<p>So the overloading problem we can easily fix by using a float area(float), this also fixes the problem of type conversion (and possible loss of data.) Then combat the problem of unwanted type conversion in float area(int,int,int);//area of triangle, we also change the type of the parameter list to float. Then fix the s=(x+y+z)/3; to s=(x+y+z)/2; and other minor changes. This example should compile without warnings or errors:</p>
<p>#include &lt;iostream&gt;<br />
#include &lt;math.h&gt;<br />
using namespace std;</p>
<p>int area(int);//area of square<br />
int area(int,int);//area of rectangle<br />
float area(float);//area of circle<br />
float area(float,float,float);//area of triangle</p>
<p>//area of square<br />
int area(int a) {<br />
	return a*a;<br />
}</p>
<p>//area of rectangle<br />
int area(int l,int b) {<br />
	return l*b;<br />
}</p>
<p>//area of circle<br />
float area(float r) {<br />
	return float((3.14*r*r));<br />
}</p>
<p>//area of triangle<br />
float area(float x,float y,float z) {<br />
	float s;<br />
	s = (x + y + z) / 2; // Not three as you had!<br />
	return(sqrt(s*(s-x)*(s-y)*(s-z)));<br />
}</p>
<p>void main() {<br />
	int a,b;<br />
	float c,d,e;</p>
<p>	cout &lt;&lt; &#8220;\nInput an integer for area of square:&#8221;;<br />
	cin &gt;&gt; a;<br />
	cout &lt;&lt; &#8220;\narea=&#8221; &lt;&lt; area(a);</p>
<p>	cout &lt;&lt; &#8220;\nInput two integers for area of rectangle:&#8221;;<br />
	cin &gt;&gt; a &gt;&gt; b;<br />
	cout &lt;&lt; &#8220;\narea=&#8221; &lt;&lt; area(a,b);</p>
<p>	cout &lt;&lt; &#8220;\nInput a float for area of circle:&#8221;;<br />
	cin &gt;&gt; c;<br />
	cout &lt;&lt; &#8220;\narea=&#8221; &lt;&lt; area(c);</p>
<p>	cout &lt;&lt; &#8220;\nInput three floats for area of triangle:&#8221;;<br />
	cin &gt;&gt; c &gt;&gt; d &gt;&gt; e;<br />
	cout &lt;&lt; &#8220;\narea=&#8221; &lt;&lt; area(c,d,e);<br />
}</p>
<p>Tip: always try to make your example programs as complete as possible (even if its only for a test) as we did by adding some comment lines and some extra screen output. This will help you in debugging.</p>
<p>We hope that this example helps you!!</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: shri hari</title>
		<link>http://www.codingunit.com/cplusplus-tutorial-overloading-and-operator-overloading/comment-page-1#comment-1737</link>
		<dc:creator>shri hari</dc:creator>
		<pubDate>Thu, 22 Jul 2010 10:21:20 +0000</pubDate>
		<guid isPermaLink="false">http://localhost/?p=528#comment-1737</guid>
		<description>i could not run the following code;

float area(int,int,int);//area of triangle
int area(int);//area of square
int area(int,int);//area of rectaangle
float area(int);//area of circle

float area(int x,int y,int z)
{
	float s;
	s=(x+y+z)/3;
	return(sqrt(s*(s-x)*(s-y)*(s-z)));
}

int area(int a)
{
	return a*a;
}

int area(int l,int b)
{
	return l*b;
}

float area(int r)
{
	return float((3.14*r*r));
}

void main()
{
	int x,y,z,a,l,b,r;
	float s;
	cout&gt;&gt;x&gt;&gt;y&gt;&gt;z;
	cout&lt;&lt;&quot;\narea=&quot;&lt;&lt;area(x,y,z);
	cout&gt;&gt;a;
	cout&lt;&lt;&quot;\narea=&quot;&lt;&lt;area(a);
	cout&gt;&gt;l&gt;&gt;b;
	cout&lt;&lt;&quot;\narea=&quot;&lt;&lt;area(l,b);
	cout&gt;&gt;r;
	cout&lt;&lt;&quot;\narea=&quot;&lt;&lt;area(r);
	getch();
}

All the above &quot;area&quot; functions are different, right? pls explain this for me.</description>
		<content:encoded><![CDATA[<p>i could not run the following code;</p>
<p>float area(int,int,int);//area of triangle<br />
int area(int);//area of square<br />
int area(int,int);//area of rectaangle<br />
float area(int);//area of circle</p>
<p>float area(int x,int y,int z)<br />
{<br />
	float s;<br />
	s=(x+y+z)/3;<br />
	return(sqrt(s*(s-x)*(s-y)*(s-z)));<br />
}</p>
<p>int area(int a)<br />
{<br />
	return a*a;<br />
}</p>
<p>int area(int l,int b)<br />
{<br />
	return l*b;<br />
}</p>
<p>float area(int r)<br />
{<br />
	return float((3.14*r*r));<br />
}</p>
<p>void main()<br />
{<br />
	int x,y,z,a,l,b,r;<br />
	float s;<br />
	cout&gt;&gt;x&gt;&gt;y&gt;&gt;z;<br />
	cout&lt;&lt;&quot;\narea=&quot;&lt;&lt;area(x,y,z);<br />
	cout&gt;&gt;a;<br />
	cout&lt;&lt;&quot;\narea=&quot;&lt;&lt;area(a);<br />
	cout&gt;&gt;l&gt;&gt;b;<br />
	cout&lt;&lt;&quot;\narea=&quot;&lt;&lt;area(l,b);<br />
	cout&gt;&gt;r;<br />
	cout&lt;&lt;&quot;\narea=&quot;&lt;&lt;area(r);<br />
	getch();<br />
}</p>
<p>All the above &quot;area&quot; functions are different, right? pls explain this for me.</p>
]]></content:encoded>
	</item>
</channel>
</rss>

