Function Templates

Function Templates


 Function Templates

 Generic function for different types.
E.g. get the min value of three variables (int, float, char)
Define function templates   46.cpp
Template<class S, class T>
   func_name()
Func_name<type name>()  
something like macro
More powerful than macro
 
 
Function Templates
Function Templates

 
Using template functions
  Func_name<type_name list>()
47.cpp
Generic quick sort 47-1.cpp



Class templates
  Generic classes
 
Define class templates
 
Template<class  T>

Class {}

Template<class T>

ret_type  class_name<type_name> :: 

 func_name ( ){

}
48.cpp
 
 
 Class template examples
  Class_name<type_name..> obj
Array class  49.cpp
Linked list 50.cpp



overload template functions 
 It is possible to overload a template function.
51.cpp
 


Template specialization
 An example from www.cplusplus.com
52.cpp
template <> class class_name <type>
 
 
Default value for templates paramters
template <class T = char> // With a default value.
template <int Tfunc (int)> // A function as parameter.
the implementation (definition) of a template class or function must be in the same file as the declaration.
 
 
Execptions
try
{ // code to be tried
   throw exception; }
catch (type  exception)
{ // code to be executed in case of exception }
 
  
Uncaught exception
  Uncaught exceptions will be thrown into outer scope.
If no catch, usually program will terminate.
Void terminate();
 
 
I/O system
  iostream.h
cin,cout,cerr,clog
iostream,fstream,strstream
Flags 57.cpp 58.cpp  
Overload <<  59.cpp
60.cpp 61.cpp 62.cpp 63.cpp 64.cpp


   

Post a Comment

0 Comments