C ++ function overloading

C ++ function overloading


 


The purpose topics


Function overloading students to understand the advantages of this lesson, master and cooked with function overloading.

Topics introduced


Introduced by the example function overloading

1) a data type function overloading

example

Write function evaluation (int type, float type, double-type) of the absolute values ​​of different data types

Before the solution is sequentially int type, float type, double-type play the corresponding function

 int abs1(int x) { if (x<0) x=-x ; return x ;}
 float abs2(float x) { if (x<0) x=-x ; return x ;}
 double abs3(double x) { if (x<0) x=-x ; return x ;}
 int main()
 {
    cout<<abs1(-1)<<abs2(-1.0f)<<abs3(-1.0);
    return 0;
 }

Observation above three functions, in addition to data types found, completely identical, but requires three functions to perform its function.

Discussion: What are some disadvantages to do so

A: Write up trouble, not to mention function close by the data type of interference, too much trouble >>>>> function names readable too low

If you use the function overloading , then this example should be written:

 int abs(int x) { if (x<0) x=-x ; return x ;}
 float abs(float x) { if (x<0) x=-x ; return x ;}
 double abs(double x) { if (x<0) x=-x ; return x ;}
 int main()
 {
    cout<<abs(-1)<<abs(-1.0f)<<abs(-1.0);
    return 0;
 }

As seen above, this reduces the number of function names, to avoid the contamination of the name space, for readability a great advantage.

Number of elements 2) function overloading

Read the following code, especially at the turn function overloading:

void f()
{
    cout<<"1"<<endl;
}
 
void f(int x)
{
    cout<<"2"<<endl;
}
 
void f(int x,int y)
{
    cout<<"3"<<endl;
}
 int main()
{
    f();
    f(1);
    f(3,4);return 0;
}

As seen above, the number of the plurality of parameters may be implemented using an overloaded function type, so that the function of a plurality of functional elements to better meet.

 

In summary:

Function overloading features

1) number of different parameters of the function.
2) Different types of different parameters of the function or the parameter type sequence.
 
Function overloading advantage
1) to facilitate dynamic call function overloading late. SUMMARY facilitate the expansion, write less duplicate code.
2) The function also allows overloaded functions polymorphic, so that the function similar to the function may be implemented in a plurality of various parameters.
 
Use function overloading
The need for various situations function declared with the same function name when the function overloading.
The following code
#include<iostream>
using namespace std;
int add();//分别对函数进行声明
int add(int a);
int add(double a);
double add(int a,double b);
int main()
{
    add();
    add(5);
    add(2.0);
    add(1,2.0);
    return 0;
}
int add()
{
    return 1+2;
}
int add(int a)
{
    return a+2;
}
int add(double a)
{
    return a+2;
}
double add(int a,double b)
{
    return a+b;
}

Experiment topics


A write function, the maximum value can be the sum of two or three integers or real numbers. 

Problem-solving ideas: the four kinds of two integers, three integers, real numbers two, three real number of possible functions to the function for which the maximum value max by overloads, but may be resolved when the maximum value acquired three parameters using two parameters are a function of selecting the maximum value written
. 1 #include <the iostream>
 2  the using  namespace STD;
 . 3  int max ( int I, int J); // overloads 
. 4  int max ( int I, int J, int K); // a different number of parameters 
. 5  Double max ( Double I, Double J); // different parameter types 
. 6  Double max ( Double I, Double J, Double K);
 . 7  int max ( int I,int j)
 . 8  {
 . 9      return i> i j:? j; // If i is greater than the return j i, otherwise j; 
10  }
 . 11   
12 is  int max ( int i, int j, int K)
 13 is  {
 14      return max ( max (i, J), K); // lines and comparing i j, the last comparator K 
15  }
 16  
. 17  Double max ( Double i, Double J)
 18 is  {
 . 19      return i> J i: J;? // if i It is greater than the return j i, otherwise j; 
20 is  }
 21 is 
22 double max(double i, double j, double k)
23 {
24     return max( max(i, j), k); //线比较i和j,最后比较k
25 }
26 
27 int main()
28 {
29     cout<<max(1,5) <<"  "<<max(1.5,0.9)<<"  "<<max(5,3,7)<<"  "    
30    <<max(5.1,3.1,7.5)<<endl;
31     return 0;
32 }

 

 

 

 

Guess you like

Origin www.cnblogs.com/LGboy/p/11530701.html