Parameter default values and dummy for C + +

Source: Internet
Author: User


I. Parameter of function supports dummy element            dummy is a parameter without parameter name, mainly for the difference between functions and forward compatibilitytwo. The parameters of the function can specify a default value, and when the call is not passed, the function call is made by default. three. A parameter with a default value must be right, and when declared and defined, the default value needs to be specified in the declaration, which is no longer specified in the definition.

Default parameter with dummy # include <iostream>using namespace std;/* 1 void print (int *a,int n) {for (int i = 0; i<n; i++) {    cout << A[i];  } cout << Endl;}    *///2. Each number of outputs separated by a space/*void print (int *a,int n) {for (int i = 0; i<n; i++) {cout << a[i] << "; } cout << Endl;} *///3. Want to separate it with asterisks?    /*void print (int *a,int n) {for (int i = 0; i<n; i++) {cout << a[i] << ' * '; } cout << Endl;} *///4. Feel free to separate each number of delimiters with a variety of separators the package function is to find the difference as the parameter/* *void print (int *a,int N,char ch) {for (int i = 0; i<n; i++) {cout    << a[i] << ch; } cout << Endl;}    * *///5. Output content in brackets/*void print (int *a,int N,char ch) {cout << ' [';    for (int i = 0; i<n; i++) {cout << a[i] << ch;    } cout << '; cout << Endl;} *///6. Sometimes you don't want the brackets sometimes?    /* *void print (int *a,int n,char Ch,bool flag) {if (flag) {cout << ' ['; } for (int i = 0; i<n;    i++) {cout << a[i] << ch;    } if (flag) {cout << '] '; } cout << Endl;}   * */#pragma if you want to print 10,000 times, but only 9,999 times the delimiter is a space and need the brackets, only one time delimiter is an asterisk * and without brackets, if only for this special, will write 9,999 times not special how to do? Just give the default parameter to//7.    Parameters in C + + can give default values void print (int *a,int n,char Ch,bool flag = True) {if (flag) {cout << ' [';    } for (int i = 0; i<n; i++) {cout << a[i] << ch;    } if (flag) {cout << '] '; } cout << Endl;} 8.    By default I want to use a space as a delimiter void print2 (int *a,int N,char ch = ", BOOL flag = TRUE) {if (flag) {cout << ' [';    } for (int i = 0; i<n; i++) {cout << a[i] << ch;    } if (flag) {cout << '] '; } cout << Endl;} #pragma note that the default value for C + + can only be given from right to left/This function I did not give the default value of the last parameter you will find an error this indicates that the default value can only be given from right to left//if fourth does not give default value to the third parameter default value, then when I pass in 3 parameters, The compiler doesn't know who to send me the 3rd one .... That is, there is no default value on the right to the left default value/*void print3 (int *a, int n, char ch = ", BooL flag) {//omit} *///9. Can I have any separators without a 8th function to be able to meet???    Unsatisfied//method overload/*void print2 (int *a,int n) {for (int i = 0; i<n; i++) {cout << a[i]; } cout << Endl;} */#pragma dummy parameter dummy parameter has no practical meaning, resolution conflict resolution version compatible (for example, there is a function in the original version has 2 parameters to the current version of this function requires only 1 parameters.) If the function is changed to 1 parameters, then it is not compatible with the original//10.    Dummy parameters arbitrarily give a type without variable name void print2 (int *a,int n,float) {for (int i = 0; i<n; i++) {cout << a[i]; } cout << Endl;}        Main function int main () {//Declaration array int a[] = {12,15,16,18,23,89,73,29,88,97};    Call function output traversal array print (A, ten, ' * ', false);        Print (A, ten, '/', true);        cout << "===== default = = =" << Endl;    7//Can not be given if it is necessary to output brackets because the default value is already given in print (a,10, ');        Another special can be given the input parameters of print (A, ten, ' * ', false);    8//No input the last parameter will default to the given default value Print2 (A, 10, ' ^ ');                When I enter the time does not enter the delimiter will automatically use the default value//Print2 (A, 10);    9.   Print2 (A, 10, "); Print2 (A, 10);//This will be an error, because the 8th function can only pass 2 parameters so here we don't know the 8th or 9th. In addition to changing the method name     Using dummy parameters cout << "= = Dummy parameter = = =" << Endl;        Print2 (A, 10);//Only 2 parameters that is the function that uses the 8th default value//If you want to print it all, pass a dummy parameter print2 (a,10,3.14f); return 0;}

The results of the operation are as follows:

12*15*16*18*23*89*73*29*88*97*[12/15/16/18/23/89/73/29/88/97/]===== Default Value ====[12 15 16 18 23 89 73 29 88 97]12*15*16*18*23 *89*73*29*88*97*[12^15^16^18^23^89^73^29^88^97^]==== Dummy Parameter ====[12--]12151618238973298897program Ended with exit code:0






Parameter default values and dummy for C + +

Related Article

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.