CPP/DefaultArguments

From ProgrammingExamples
< CPP
Jump to: navigation, search

DefaultArguments.cpp

#include <iostream>
 
using namespace std;
 
template<typename T>
void printNTimes(const T& arg, int n = 1){
 for(int i = 0; i != n; ++i){
   cout << arg << endl;
  }
}
 
int main(int argc, char *argv[])
{
  printNTimes("hello",5); //explicitly tell how many times to print
  printNTimes("goodbye"); //use default argument of 1
  return 0;
}