summaryrefslogtreecommitdiff
path: root/reference/CPLUSPLUS/CONCEPT/funcoverload.html
diff options
context:
space:
mode:
authorTobias Klauser <tklauser@distanz.ch>2008-01-27 11:37:44 +0100
committerTobias Klauser <tklauser@xenon.tklauser.home>2008-01-27 11:37:44 +0100
commit7e0f021a9aec35fd8e6725e87e3313b101d26f5e (patch)
treeb1cacc4b24393f517aeb4610e9e1021f954307a8 /reference/CPLUSPLUS/CONCEPT/funcoverload.html
Initial import (2.0.2-6)2.0.2-6
Diffstat (limited to 'reference/CPLUSPLUS/CONCEPT/funcoverload.html')
-rw-r--r--reference/CPLUSPLUS/CONCEPT/funcoverload.html153
1 files changed, 153 insertions, 0 deletions
diff --git a/reference/CPLUSPLUS/CONCEPT/funcoverload.html b/reference/CPLUSPLUS/CONCEPT/funcoverload.html
new file mode 100644
index 0000000..a245e32
--- /dev/null
+++ b/reference/CPLUSPLUS/CONCEPT/funcoverload.html
@@ -0,0 +1,153 @@
+<html>
+<head>
+<title>Function Overloading.</title>
+</head>
+<body bgcolor="#dddddd">
+<font color=brown>
+<hr>
+<center><h1>Function Overloading.</h1></center>
+<hr>
+<p>
+Overloading is a technique that allows you to give a several
+functions with the same name but different parameter lists.
+<p>
+Consider the following program.
+<p>
+<center>
+<table border=1 width="80%" bgcolor="ivory">
+<tr>
+<td>
+<pre>
+
+ #include &lt;iostream.h&gt;
+
+ void Add(int Left, int Right);
+
+ main ()
+ {
+ Add(5, 9); // Integer Add.
+ Add(3.2, 7.1); // Floating point Add.
+ }
+
+ // Integer version of Add.
+
+ void Add(int Left, int Right)
+ {
+ cout << Left << " + " << Right << " = " << Left+Right << endl;
+ }
+</pre>
+</td>
+</tr>
+</table>
+</center>
+<p>
+The program contains the <b>Add</b> function that adds integers.
+But the main calls it twice, once passing integers and the second
+time passing floating point numbers. The program will compile and run
+but you will get incorrect results because the floats will be
+<a href=../../C/CONCEPT/cast.html> cast</a> to integers.
+<p>
+The second
+program offers a solution by <b>overloading</b> the <b>Add</b> function.
+
+<p>
+<center>
+<table border=1 width="80%" bgcolor="ivory">
+<tr>
+<td>
+<pre>
+
+ #include &lt;iostream.h&gt;
+
+ void Add(int Left, int Right);
+ <b>void Add(double Left, double Right);</b>
+
+ main ()
+ {
+ Add(5, 9); // Integer Add.
+ Add(3.2, 7.1); // Floating point Add.
+ }
+
+ // Integer version of Add.
+
+ void Add(int Left, int Right)
+ {
+ cout << Left << " + " << Right << " = " << Left+Right << endl;
+ }
+ <b>
+ // float version of Add.
+
+ void Add(double Left, double Right)
+ {
+ cout << Left << " + " << Right << " = " << Left+Right << endl;
+ }
+</b>
+</pre>
+</td>
+</tr>
+</table>
+</center>
+
+The compiler can now look at the call list and match it with
+a function with the correct parameter list and the floating
+point addition is performed correctly.
+
+<p>
+
+Please note that the returned argument is not used when matching
+overloaded functions.
+
+
+<hr>
+<h2>Examples:</h2>
+<img src="../../GRAPHICS/computer.gif" alt="o">
+<a href="../EXAMPLES/overload.cc">Example program.</a>
+<p>
+
+<hr>
+<h2>See Also:</h2>
+
+<img src="../../GRAPHICS/whiteball.gif" alt="o">
+<a href="fundefault.html">Default Parameter Values.</a>.
+
+
+</font>
+<hr>
+<font color=black>
+<h2>C References</h2>
+<p>
+<img src="../../GRAPHICS/whiteball.gif" alt="o">
+<a href="../../C/SYNTAX/functions.html">Function Basics</a>.
+
+</font>
+
+<hr>
+<p>
+<center>
+<table border=2 width="80%" bgcolor="ivory">
+<tr align=center>
+<td width="25%">
+<a href="../cref.html">Top</a>
+</td><td width="25%">
+<a href="../../C/master_index.html">Master Index</a>
+</td><td width="25%">
+<a href="../SYNTAX/keywords.html">Keywords</a>
+</td><td width="25%">
+<a href="../../C/FUNCTIONS/funcref.htm">Functions</a>
+</td>
+</tr>
+</table>
+</center>
+<p>
+<hr>
+<font color=brown>
+<address>Martin Leslie
+<script language="JavaScript">
+<!-- //
+document.write(document.lastModified);
+// -->
+</script>
+</address><p>
+</font>
+</body>
+</html>