Subscribe:

Ads 468x60px

Wednesday, August 29, 2012

C++ Template:Hello World

Hi guys

     C++ templates are very useful when we write a generic code, ie it will work with all data types. I am giving a sample code which shows the working of templates



 //============================================================================  
 // Name    : test_learn.cpp  
 // Author   : lentin  
 // Version   :  
 // Copyright  : Your copyright notice  
 // Description : Hello World in C, Ansi-style  
 //============================================================================  
   
 #include <iostream>  
   
 using namespace std;  
   
 //generic add function  
 template <class T>  
 void add(T a,T b)  
 {  
  cout<<(a+b)<<endl;  
 }  
   
 int main(int argc,char *argv[])  
 {  
  //integer addition  
  add(5,5);  
    
  //float addition  
  add(4.23425,5.5);  
   
 }  
   


Output
----------

10
9.73425

0 comments:

Post a Comment