Download presentation
Presentation is loading. Please wait.
1
Introduction to Programming
Lecture 42
2
template <class T>
3
Template Classes
4
Stack
5
Last In First Out
6
template <class T> class ClassName { definition }
7
Member function template <class T>
Class_Name <T> :: Function_Name (Arguments) { // body of function }
8
Class_Name :: Function_Name ( Arguments )
{ // body of function }
9
Example template <class T> class Number { private : T MyNumber ;
public: Number ( T n ) ; display ( ) ; }
10
Example Number <int> x ; Number <double> y ;
11
Non Type Parameters
12
template <class T , int elements>
13
int x [ 100 ] ;
14
Static Member Variables
15
Number <int> x ;
16
Example class PhoneCall { private : int lengthOfCall ; char billCode ;
public : PhoneCall ( const int i , char b ) ; PhoneCall ( PoneCall & p ) ; PhoneCall PhoneCall :: operator - ( void ) ; void display ( void ) ; } ;
17
Example PhoneCall PhoneCall :: operator -( void ) {
PhoneCall temp ( * this ) ; temp.billCode = 'C' ; return ( temp ) ; }
18
Friend Function
19
‘a’ is a object of a class
2 is an integer value ‘a’ is a object of a class a + 2 ;
20
a + 2 ; should be same as 2 + a ;
21
friend f ( ) ;
22
friend f ( x <T> & ) ;
23
friend f ( X <int> & ) ;
24
friend class Y ;
25
friend A :: f ( ) ;
26
friend A :: f ( X< T > & )
27
Example template <class T> class Stack { private : int size ;
T array [ ] ; public : Stack ( ) ; void push ( T ) ; T pop ( ) ; bool isEmpty ( ) ; bool isFull ( ) ; // Etc. } ;
28
Example Stack <int> x ; Stack <double> x ;
29
Last In First Out
30
Queue Link List
31
First In First Out
32
STL Standard Template Library
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.