Difference between revisions of "CPP/DefaultArguments"
From ProgrammingExamples
< CPP
Daviddoria (Talk | contribs) (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…') |
FirstPerson (Talk | contribs) (→DefaultArguments.cpp) |
||
Line 2: | Line 2: | ||
<source lang="cpp"> | <source lang="cpp"> | ||
#include <iostream> | #include <iostream> | ||
− | |||
− | void | + | 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[]) | ||
{ | { | ||
− | + | printNTimes("hello",5); | |
+ | printNTimes("goodbye"); | ||
return 0; | return 0; | ||
} | } | ||
− | |||
− | |||
− | |||
− | |||
</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; }