Using multiple default parameter values: defaults for reference parameters : default arguments « Function « C++ Tutorial






#include <iostream>
#include <iomanip>
#include <string>
using std::cout;
using std::endl;
using std::string;

void show_data(const int data[], int count = 1,
            const string& title = "Data Values", int width = 10, int perLine =5);

int main() {
  int data[] = {1, 2, 3, 4};

  int dataItem = 99;
  show_data (&dataItem);

  dataItem = 13;
  show_data(&dataItem, 1, "Unlucky for some!");

  show_data(data, sizeof data/sizeof data[0]);
  show_data(data, sizeof data/sizeof data[0], "title 1");
  show_data(data, sizeof data/sizeof data[0], "title 2", 14);
  show_data(data, sizeof data/sizeof data[0], "title 3", 14, 4);

  return 0;
}

void show_data (const int data[], int count, const string& title,int width, int perLine) {
  cout << endl << title;                    
  cout << endl << perLine;
  cout << endl << width << data[2];    
  cout << endl;
}
Data Values
5
102

Unlucky for some!
5
102

Data Values
5
103

title 1
5
103

title 2
5
143

title 3
4
143








7.6.default arguments
7.6.1.Demonstrate default arguments
7.6.2.Function with two regular parameters and one parameter with default value
7.6.3.Use two default parameter values
7.6.4.Ambiguous function call with function with default parameter value
7.6.5.Using multiple default parameter values: defaults for reference parameters
7.6.6.demonstrates missing and default arguments