Upload
bilal-ahmed
View
27
Download
1
Tags:
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
Introduction to Introduction to ProgrammingProgramming
Lecture 42Lecture 42
template <class template <class T>T>
TemplatTemplate e
ClassesClasses
StacStackk
Last In Last In First First OutOut
template <class template <class T>T>class class ClassNameClassName{{
definitiondefinition}}
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
Class_Name :: Function_Name ( Arguments )Class_Name :: Function_Name ( Arguments )
{{
// body of function// body of function
}}
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
Number <int> x ;Number <int> x ;
Number <double> y ;Number <double> y ;
ExampleExample
Non Type Non Type ParameterParameter
ss
template <class T , int template <class T , int elements>elements>
int x int x [ 100 ] ;[ 100 ] ;
Static Static Member Member VariablesVariables
Number <int> x Number <int> x ;;
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 ) ;} ;} ;
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
Friend Friend FunctionFunction
a + a + 2 ;2 ;
‘a’ is a object of a
class
2 is an integer value
a + 2 ;a + 2 ;should be should be same assame as2 + a ;2 + a ;
friend f friend f ( ) ;( ) ;
friend f ( x <T> & ) ;friend f ( x <T> & ) ;
friend f ( X <int> friend f ( X <int> & ) ;& ) ;
friend class Y friend class Y ;;
friend A :: f ( ) friend A :: f ( ) ;;
friend A :: f ( X< T > & )friend A :: f ( X< T > & )
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
Stack <int> x ;Stack <int> x ;Stack <double> x ;Stack <double> x ;
ExampleExample
Last In Last In First First OutOut
QueueQueueLink Link ListList
First In First In First First OutOut
STLSTLStandard Standard Template Template LibraryLibrary