Default parameters and function overloading

Source: Internet
Author: User

First, the default parameters
In C + +, you can specify a default value for the parameter. When a function call does not specify an argument that corresponds to a formal parameter, the default parameter is automatically used.

The default parameter syntax is used with:
(1) When a function is declared or defined, the parameter is directly assigned a value. This is the default parameter;
(2) Omit some or all of the arguments when the function is called. You can use default parameters instead.

Attention:
(1) The default parameter can only be set once in the function declaration. It can be set in the function definition only when there is no function declaration.
(2) The default parameters are defined in the order from right to left. That is, if a parameter is set to a default value, the parameter to its left must have a default value.
such as: int mal (int a, int b=3, int c=6, int d=8) are correct, set default values in right-to-left order.
int mal (int a=6, int b=3, int c, int d=5) error, default value is not set from right to left. C Sets the default value, and the D on the right has no default value.
(3) When the default parameter is called, the parameter invocation order is followed, which is called from left to right, that is, when the left parameter call has a default value, the right argument is the default. This point should be clearly divided with paragraph (2) and not be confused.
such as: void Mal (int a, int b=3, int c=5); Default parameters
Mal (3, 8, 9); When called with a specified argument, no default parameter is used
Mal (3, 5); Only two parameters are specified on invocation, in order from left to right, which is equivalent to Mal (3,5,5);
Mal (3); Only 1 parameters are specified on invocation, in order from left to right, which is equivalent to Mal (5,3,5);
Mal (); Error because a does not have a default value
Mal (3,, 9)//error, should be called one by one in order from left to right


Again such as: void Mal (int a=8, int b=3, int c=5); Default parameters
Mal (); Correct, call all default parameters, equivalent to Mal (8,3,5);

(4) The default value can be a global variable, a global constant, or even a function. But it cannot be a local variable. Because the invocation of the default parameter is determined at compile time, the location of the local variable and the default value cannot be determined at compile time.

Second, function overloading
In the same declaration field, the function name is the same, and the parameter table is different. A special function usage that uniquely identifies and distinguishes a function through the parameter table of a function.

The different performance of the parameter table is:
1, the parameter type is different;
2, the number of parameters is different;

Special Note: Different return types cannot be identified as function overloads.

Cases:

#include <iostream>
using namespace Std;
int test (int a,int b);
Float test (float a,float b);
void Main ()
{
cout << test << endl << Test (2.1f,3.14f) << Endl;
Cin.get ();
}

int test (int a,int b)
{
return a+b;
}

Float test (float a,float b)
{
return a+b;
}

In the above program, two functions named Test are used to describe the type of int and operation and float type and operation to facilitate the management of the same or similar function functions!
So, how does the computer determine the same name function? Does the operation cause a selection error?
The answer is no, internal C + + uses a kind of wit called name shredding to internally rename a function of the same name, the above example may be testii and TESTFF after the calculation is renamed, they are internally renamed by the type or number of arguments.

1. Examples of different parameter types:
(1)
#include <iostream.h>
void Add (char X,char y)
{

cout<< "string is:";
cout<<x<<y<<endl;
}


void Add (int x,int y)
{

cout<< "Two number of the and is:";
cout<<x+y<<endl;
}


void Main ()
{
ADD (' O ', ' K ');
ADD (65,100);
}

The

(2) Overloads the function abs () to find the absolute value of the int, float, and double type data.
   #include <iostream.h> 
   int ABS (int x)  //To find the absolute value of the INT type data  
   { 
         if (x<0) x=-x; 
          return x; 
  } 
   //to find the absolute value of the float type data  
  Float ABS (float x)  
  { 
           if (x<0) x=-x; 
          return x; 
   } 
 //The absolute value of the double type data  
 //imitate the above function to write

Main function
void Main ()
{
int a=-357;
float b=63.85;
Double c=-6974.26;
Cout<<abs (a) << '/t ' <<abs (b) << '/t ' <<abs (c) <<endl;

2, the number of different parameters of the example: to find the maximum number of integers, according to different numbers of parameters call different Max () function
#include <iostream.h>
int max (int x,int y)
{
if (x>y) return x;
else return y;
}

int max (int x,int y,int z)
{
int A=max (x, y);
Return Max (A,Z);
}


int max (int a,int b,int c,int D)
{
Self-compiling this part of the code
}


Main ()
{
Cout<<max (<<endl;)
Cout<<max (<<endl;)
Cout<<max (1,2,3,4) <<endl;
}

Considerations for function overloading

1, the parameters of the function must be different, or the number is different, or the type is different, can not only rely on the function of the return value type different or the shape parametric name different to implement function overloading.
2, do not define functions of different functions as overloaded functions, so as to avoid misunderstanding of the result of the call. Such as:
int add (int x,int y) {return x+y;}
Float Add (float x,float y) {return x-y;}

The two semantic issues that result from overlapping of overloaded functions with default parameters:
func (int); Overloaded function 1, only 1 parameters, no default parameters
func (int, int =4); Overloaded function 2, with 2 parameters, 1 default parameters
Func (int a=3, int b=4, int c=6); Overloaded function 3, with 3 parameters, 3 default parameters
FUCN (float a=3.0, float b=4.0 float c=5.0); Overloaded function 4, with 3 parameters, 3 default parameters
FUCN (float a=3.0, float b=4.0 float c=5.0 float d=7.9); Overloaded function 5, with 4 parameters, 4 default parameters

Func (2); The first 3 functions can be called, and a ambiguity appears.
Func (2.0); can call after 2 functions, appear ambiguity

So when overloaded functions are used in conjunction with default parameters, there are two semantic issues to be aware of.

Default parameters and function overloading

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.