From 7e0f021a9aec35fd8e6725e87e3313b101d26f5e Mon Sep 17 00:00:00 2001 From: Tobias Klauser Date: Sun, 27 Jan 2008 11:37:44 +0100 Subject: Initial import (2.0.2-6) --- reference/CPLUSPLUS/CONCEPT/funcoverload.html | 153 ++++++++++++++++++++++++++ 1 file changed, 153 insertions(+) create mode 100644 reference/CPLUSPLUS/CONCEPT/funcoverload.html (limited to 'reference/CPLUSPLUS/CONCEPT/funcoverload.html') 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 @@ + + +Function Overloading. + + + +
+

Function Overloading.

+
+

+Overloading is a technique that allows you to give a several +functions with the same name but different parameter lists. +

+Consider the following program. +

+

+ + + + +
+
+
+    #include <iostream.h>
+    
+    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;
+    }
+
+
+
+

+The program contains the Add 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 + cast to integers. +

+The second +program offers a solution by overloading the Add function. + +

+

+ + + + +
+
+
+    #include <iostream.h>
+    
+    void Add(int    Left, int    Right);
+    void Add(double Left, double 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;
+    }
+ 
+    // float version of Add.
+    
+    void Add(double Left, double Right)
+    {
+        cout << Left << " + " << Right << " = " << Left+Right << endl;
+    }
+
+
+
+
+ +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. + +

+ +Please note that the returned argument is not used when matching +overloaded functions. + + +


+

Examples:

+o +Example program. +

+ +


+

See Also:

+ +o +Default Parameter Values.. + + +
+
+ +

C References

+

+o +Function Basics. + + + +


+

+

+ + + + +
+Top + +Master Index + +Keywords + +Functions +
+
+

+


+ +
Martin Leslie + +

+ + + -- cgit v1.2.3-54-g00ecf