Introduction to Introduction to Programming Programming Lecture 41 Lecture 41
Dec 18, 2014
Introduction to Introduction to ProgrammingProgramming
Lecture 41Lecture 41
TemplateTemplatess
Types of Types of TemplatesTemplates
Function TemplatesFunction Templates Class Templates Class Templates
void void swap ( int & i , int & j )swap ( int & i , int & j )
{{
int temp ;int temp ;
temp = i ;temp = i ;
i = j ;i = j ;
j = temp ;j = temp ;
}}
Swap FunctionSwap Function
Function Function OverloadinOverloadin
gg
Function Function TemplatesTemplates
template < class T template < class T >>
return_type function_name ( argument_list return_type function_name ( argument_list ))
int reverse ( int x )int reverse ( int x ){{ return ( - x ) ;return ( - x ) ;}}double reverse ( double x )double reverse ( double x ){{ return ( - x ) ;return ( - x ) ;}}
ExampleExample
template < class T >template < class T >
T reverse ( T x )T reverse ( T x )
{{
return (- x ) ;return (- x ) ;
}}
ExampleExample
main ( )main ( )
{{
int i ;int i ;
…………..
reverse ( i ) ;reverse ( i ) ;
}}
ExampleExample
main ( )main ( ){{
int i ; int i ; … …reverse ( i ) ;reverse ( i ) ; … …double y ;double y ;
… …reverse ( y ) ;reverse ( y ) ;
}}
ExampleExample
Code Code ReuseReuse
template < class T >template < class T >
void swap ( T & x , T & y )void swap ( T & x , T & y )
{{
T temp ;T temp ;
temp = x ;temp = x ;
x = y ;x = y ;
y = temp ;y = temp ;
}}
ExampleExample
int a , b ; int a , b ;
char a , b ;char a , b ;
swap ( a , b ) ;swap ( a , b ) ;
template < class T >template < class T >
void swap ( T & x , T & y )void swap ( T & x , T & y )
{{
T temp ;T temp ;
temp = x ;temp = x ;
x = y ;x = y ;
y = temp ;y = temp ;
}}
ExampleExample
template < class T , class template < class T , class U >U >
template <class T>template <class T>
T larger ( T x, T y )T larger ( T x, T y )
{{
T big ;T big ;
if ( x > y )if ( x > y )
big = x ;big = x ;
elseelse
big = y ;big = y ;
return ( big ) ;return ( big ) ;
}}
ExampleExample
main ( )main ( )
{{
int i = 5 , j = 7 ;int i = 5 , j = 7 ;
double x = 10.0 , y = 15.0 ;double x = 10.0 , y = 15.0 ;
cout << larger ( i , j ) << endl ;cout << larger ( i , j ) << endl ;
cout << larger ( x , y ) << endl ;cout << larger ( x , y ) << endl ;
// cout << larger ( i , y ) ; // cout << larger ( i , y ) ; ErrorError
}}
ExampleExample
template <class T>template <class T>
void inverse ( T & x , T & y )void inverse ( T & x , T & y )
{{
T temp ;T temp ;
temp = x ;temp = x ;
x = y ;x = y ;
y = temp ;y = temp ;
}}
ExampleExample
template <class T>template <class T>
T inverse ( T x )T inverse ( T x )
{{
return ( - x ) ;return ( - x ) ;
}}
ExampleExample
main ( )main ( )
{{
int i = 4 , j = 8 ;int i = 4 , j = 8 ;
inverse ( i , j ) ;inverse ( i , j ) ;
inverse ( i ) ;inverse ( i ) ;
}}
ExampleExample
template <class T>template <class T>T reverse ( T x )T reverse ( T x ){{ return ( - x ) ;return ( - x ) ;}}void main ( )void main ( ){{ double a = 10.75 ;double a = 10.75 ; reverse ( a ) ;reverse ( a ) ; reverse <int> ( a ) ;reverse <int> ( a ) ;}}
ExampleExample
template <class T , class U>template <class T , class U>
T reverse ( U x )T reverse ( U x )
{{
return ( - x ) ;return ( - x ) ;
}}
ExampleExample
main ( )main ( )
{{
double a = 8.8 ;double a = 8.8 ;
reverse ( a ) ;reverse ( a ) ;
reverse <int> ( a ) ;reverse <int> ( a ) ;
reverse <int , double> ( a ) ;reverse <int , double> ( a ) ;
reverse<double , double> ( a ) ;reverse<double , double> ( a ) ;
reverse<double , int> ( a ) ;reverse<double , int> ( a ) ;
}}
ExampleExample
ExampleExampleclass PhoneCallclass PhoneCall{{ private :private : int lengthOfCall ;int lengthOfCall ; char billCode ;char billCode ; public :public :
PhoneCall ( const int i , char b ) ;PhoneCall ( const int i , char b ) ;PhoneCall ( PoneCall & p ) ;PhoneCall ( PoneCall & p ) ;
PhoneCall PhoneCall :: operator - PhoneCall PhoneCall :: operator - ( void ) ;( void ) ;
void display ( void ) ;void display ( void ) ;} ;} ;
template <class T>template <class T>
T reverse ( T x )T reverse ( T x )
{{
return ( - x ) ;return ( - x ) ;
}}
ExampleExample
PhoneCall reverse ( PhoneCall x )PhoneCall reverse ( PhoneCall x )
{{
return (- x ) ;return (- x ) ;
}}
ExampleExample
PhoneCall PhoneCall :: operator - PhoneCall PhoneCall :: operator - ( void )( void )
{{
PhoneCall temp ( * this ) ;PhoneCall temp ( * this ) ;
temp.billCode = 'C' ;temp.billCode = 'C' ;
return ( temp ) ;return ( temp ) ;
}}
ExampleExample
Example Example main ( )main ( )
{{
PhoneCall a ( 10 , ‘S’ ) ;PhoneCall a ( 10 , ‘S’ ) ;
a.display ( ) ;a.display ( ) ;
a = reverse ( a ) ;a = reverse ( a ) ;
a.display ( ) ;a.display ( ) ;
}}