32
Introduction to Introduction to Programming Programming Lecture 42 Lecture 42

CS201- Introduction to Programming- Lecture 42

Embed Size (px)

DESCRIPTION

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

Citation preview

Page 1: CS201- Introduction to Programming- Lecture 42

Introduction to Introduction to ProgrammingProgramming

Lecture 42Lecture 42

Page 2: CS201- Introduction to Programming- Lecture 42

template <class template <class T>T>

Page 3: CS201- Introduction to Programming- Lecture 42

TemplatTemplate e

ClassesClasses

Page 4: CS201- Introduction to Programming- Lecture 42

StacStackk

Page 5: CS201- Introduction to Programming- Lecture 42

Last In Last In First First OutOut

Page 6: CS201- Introduction to Programming- Lecture 42

template <class template <class T>T>class class ClassNameClassName{{

definitiondefinition}}

Page 7: CS201- Introduction to Programming- Lecture 42

template <class T>template <class T>

Class_Name <T> :: Function_Name (Arguments)Class_Name <T> :: Function_Name (Arguments)

{{

// body of function// body of function

}}

Member functionMember function

Page 8: CS201- Introduction to Programming- Lecture 42

Class_Name :: Function_Name ( Arguments )Class_Name :: Function_Name ( Arguments )

{{

// body of function// body of function

}}

Page 9: CS201- Introduction to Programming- Lecture 42

template <class T>template <class T>

class Numberclass Number

{{

private :private :

T MyNumber ;T MyNumber ;

public:public:

Number ( T n ) ;Number ( T n ) ;

display ( ) ;display ( ) ;

}}

ExampleExample

Page 10: CS201- Introduction to Programming- Lecture 42

Number <int> x ;Number <int> x ;

Number <double> y ;Number <double> y ;

ExampleExample

Page 11: CS201- Introduction to Programming- Lecture 42

Non Type Non Type ParameterParameter

ss

Page 12: CS201- Introduction to Programming- Lecture 42

template <class T , int template <class T , int elements>elements>

Page 13: CS201- Introduction to Programming- Lecture 42

int x int x [ 100 ] ;[ 100 ] ;

Page 14: CS201- Introduction to Programming- Lecture 42

Static Static Member Member VariablesVariables

Page 15: CS201- Introduction to Programming- Lecture 42

Number <int> x Number <int> x ;;

Page 16: CS201- Introduction to Programming- Lecture 42

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 - ( void ) ;PhoneCall PhoneCall :: operator - ( void ) ; void display ( void ) ;void display ( void ) ;} ;} ;

Page 17: CS201- Introduction to Programming- Lecture 42

PhoneCall PhoneCall :: operator -( void PhoneCall PhoneCall :: operator -( void ))

{{

PhoneCall temp ( * this ) ;PhoneCall temp ( * this ) ;

temp.billCode = 'C' ;temp.billCode = 'C' ;

return ( temp ) ;return ( temp ) ;

}}

ExampleExample

Page 18: CS201- Introduction to Programming- Lecture 42

Friend Friend FunctionFunction

Page 19: CS201- Introduction to Programming- Lecture 42

a + a + 2 ;2 ;

‘a’ is a object of a

class

2 is an integer value

Page 20: CS201- Introduction to Programming- Lecture 42

a + 2 ;a + 2 ;should be should be same assame as2 + a ;2 + a ;

Page 21: CS201- Introduction to Programming- Lecture 42

friend f friend f ( ) ;( ) ;

Page 22: CS201- Introduction to Programming- Lecture 42

friend f ( x <T> & ) ;friend f ( x <T> & ) ;

Page 23: CS201- Introduction to Programming- Lecture 42

friend f ( X <int> friend f ( X <int> & ) ;& ) ;

Page 24: CS201- Introduction to Programming- Lecture 42

friend class Y friend class Y ;;

Page 25: CS201- Introduction to Programming- Lecture 42

friend A :: f ( ) friend A :: f ( ) ;;

Page 26: CS201- Introduction to Programming- Lecture 42

friend A :: f ( X< T > & )friend A :: f ( X< T > & )

Page 27: CS201- Introduction to Programming- Lecture 42

template <class T>template <class T>class Stackclass Stack{{

private :private :int size ;int size ;T array [ ] ;T array [ ] ;

public :public :Stack ( ) ;Stack ( ) ; void push ( T ) ;void push ( T ) ;T pop ( ) ;T pop ( ) ;bool isEmpty ( ) ;bool isEmpty ( ) ;bool isFull ( ) ;bool isFull ( ) ;// Etc.// Etc.

} ;} ;

ExampleExample

Page 28: CS201- Introduction to Programming- Lecture 42

Stack <int> x ;Stack <int> x ;Stack <double> x ;Stack <double> x ;

ExampleExample

Page 29: CS201- Introduction to Programming- Lecture 42

Last In Last In First First OutOut

Page 30: CS201- Introduction to Programming- Lecture 42

QueueQueueLink Link ListList

Page 31: CS201- Introduction to Programming- Lecture 42

First In First In First First OutOut

Page 32: CS201- Introduction to Programming- Lecture 42

STLSTLStandard Standard Template Template LibraryLibrary