Difference between revisions of "CPP/DefaultArguments"

From ProgrammingExamples
< CPP
Jump to: navigation, search
(Created page with '==DefaultArguments.cpp== <source lang="cpp"> #include <iostream> #include <limits> void Test(int a = 5); int main(int argc, char *argv[]) { Test(); return 0; } void Test(i…')
 
(DefaultArguments.cpp)
Line 2: Line 2:
 
<source lang="cpp">
 
<source lang="cpp">
 
#include <iostream>
 
#include <iostream>
#include <limits>
 
  
void Test(int a = 5);
+
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[])
 
int main(int argc, char *argv[])
 
{
 
{
   Test();
+
   printNTimes("hello",5);
 +
  printNTimes("goodbye");
 
   return 0;
 
   return 0;
 
}
 
}
  
void Test(int a)
 
{
 
  std::cout << a << std::endl;
 
}
 
 
</source>
 
</source>

Revision as of 18:38, 8 July 2010

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);
  printNTimes("goodbye");
  return 0;
}