C++ Tutorial/Function/default arguments

Материал из C\C++ эксперт
Перейти к: навигация, поиск

Ambiguous function call with function with default parameter value

#include <iostream>
using namespace std;
int myfunc(int i);
int myfunc(int i, int j=1);
int main()
{
  cout << myfunc(4, 5) << " "; // unambiguous
  //cout << myfunc(10); // ambiguous
  return 0;
}
int myfunc(int i)
{
  return i;
}
int myfunc(int i, int j)
{
  return i*j;
}
20 "

Demonstrate default arguments

#include <iostream>  
using namespace std;  
 
void f(int x = 0, int y = 100); 
 
int main()  
{  
  f(1, 2); 
 
  f(10); 
 
  f(); 
 
  return 0; 
} 
 
void f(int x, int y) 
{ 
  cout << "x: " << x << ", y: " << y << "\n"; 
}
x: 1, y: 2
x: 10, y: 100
x: 0, y: 100

demonstrates missing and default arguments

#include <iostream>   
  using namespace std;   
    
  void repchar(char="*", int=45);   
  int main(){   
     repchar();                     
     repchar("=");                  
     repchar("+", 30);              
     return 0;   
  }   
  void repchar(char ch, int n)      
  {                              
     for(int j=0; j<n; j++)      
        cout << ch;              
  }

Function with two regular parameters and one parameter with default value

#include <iostream> 
#include <cstring> 
using namespace std; 
 
void f(char *s1, char *s2, int len = 0); 
 
int main() 
{ 
  char str1[80] = "This is a test"; 
  char str2[80] = "0123456789"; 
 
  f(str1, str2, 5); 
  f(str1, str2); 
  return 0; 
} 
 
void f(char *s1, char *s2, int len) 
{ 
  cout << s1;
  cout << " " << len << " "; 
  cout << s2;
}
This is a test 5 0123456789This is a test 0 0123456789

Use two default parameter values

#include <iostream>
 
 int f(int length, int width = 25, int height = 1);
 
 int main()
 {
     int length = 100;
     int width = 50;
     int height = 2;
     int area;
 
     area = f(length, width, height);
     std::cout << "First time area equals " << area << "\n";
 
     area = f(length, width);
     std::cout << "Second time area equals " << area << "\n";
 
     area = f(length);
     std::cout << "Third time area equals " << area << "\n";
     return 0;
 }
 
 int f(int length, int width, int height)
 {
     return (length * width * height);
 }
First time area equals 10000
Second time area equals 5000
Third time area equals 2500

Using multiple default parameter values: defaults for reference parameters

#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