CS201- Introduction to Programming- Lecture 41

30
Introduction to Introduction to Programming Programming Lecture 41 Lecture 41

description

Virtual University Course CS201- Introduction to Programming Lecture No 41 Instructor's Name: Dr. Naveed A. Malik Course Email: [email protected]

Transcript of CS201- Introduction to Programming- Lecture 41

Page 1: CS201- Introduction to Programming- Lecture 41

Introduction to Introduction to ProgrammingProgramming

Lecture 41Lecture 41

Page 2: CS201- Introduction to Programming- Lecture 41

TemplateTemplatess

Page 3: CS201- Introduction to Programming- Lecture 41

Types of Types of TemplatesTemplates

Function TemplatesFunction Templates Class Templates Class Templates

Page 4: CS201- Introduction to Programming- Lecture 41

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

Page 5: CS201- Introduction to Programming- Lecture 41

Function Function OverloadinOverloadin

gg

Page 6: CS201- Introduction to Programming- Lecture 41

Function Function TemplatesTemplates

Page 7: CS201- Introduction to Programming- Lecture 41

template < class T template < class T >>

Page 8: CS201- Introduction to Programming- Lecture 41

return_type function_name ( argument_list return_type function_name ( argument_list ))

Page 9: CS201- Introduction to Programming- Lecture 41

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

Page 10: CS201- Introduction to Programming- Lecture 41

template < class T >template < class T >

T reverse ( T x )T reverse ( T x )

{{

return (- x ) ;return (- x ) ;

}}

ExampleExample

Page 11: CS201- Introduction to Programming- Lecture 41

main ( )main ( )

{{

int i ;int i ;

…………..

reverse ( i ) ;reverse ( i ) ;

}}

ExampleExample

Page 12: CS201- Introduction to Programming- Lecture 41

main ( )main ( ){{

int i ; int i ; … …reverse ( i ) ;reverse ( i ) ; … …double y ;double y ;

… …reverse ( y ) ;reverse ( y ) ;

}}

ExampleExample

Page 13: CS201- Introduction to Programming- Lecture 41

Code Code ReuseReuse

Page 14: CS201- Introduction to Programming- Lecture 41

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

Page 15: CS201- Introduction to Programming- Lecture 41

int a , b ; int a , b ;

char a , b ;char a , b ;

swap ( a , b ) ;swap ( a , b ) ;

Page 16: CS201- Introduction to Programming- Lecture 41

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

Page 17: CS201- Introduction to Programming- Lecture 41

template < class T , class template < class T , class U >U >

Page 18: CS201- Introduction to Programming- Lecture 41

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

Page 19: CS201- Introduction to Programming- Lecture 41

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

Page 20: CS201- Introduction to Programming- Lecture 41

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

Page 21: CS201- Introduction to Programming- Lecture 41

template <class T>template <class T>

T inverse ( T x )T inverse ( T x )

{{

return ( - x ) ;return ( - x ) ;

}}

ExampleExample

Page 22: CS201- Introduction to Programming- Lecture 41

main ( )main ( )

{{

int i = 4 , j = 8 ;int i = 4 , j = 8 ;

inverse ( i , j ) ;inverse ( i , j ) ;

inverse ( i ) ;inverse ( i ) ;

}}

ExampleExample

Page 23: CS201- Introduction to Programming- Lecture 41

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

Page 24: CS201- Introduction to Programming- Lecture 41

template <class T , class U>template <class T , class U>

T reverse ( U x )T reverse ( U x )

{{

return ( - x ) ;return ( - x ) ;

}}

ExampleExample

Page 25: CS201- Introduction to Programming- Lecture 41

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

Page 26: CS201- Introduction to Programming- Lecture 41

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 ) ;} ;} ;

Page 27: CS201- Introduction to Programming- Lecture 41

template <class T>template <class T>

T reverse ( T x )T reverse ( T x )

{{

return ( - x ) ;return ( - x ) ;

}}

ExampleExample

Page 28: CS201- Introduction to Programming- Lecture 41

PhoneCall reverse ( PhoneCall x )PhoneCall reverse ( PhoneCall x )

{{

return (- x ) ;return (- x ) ;

}}

ExampleExample

Page 29: CS201- Introduction to Programming- Lecture 41

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

Page 30: CS201- Introduction to Programming- Lecture 41

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 ( ) ;

}}