Computer Notes - Templates & Static Members - PowerPoint PPT Presentation

About This Presentation
Title:

Computer Notes - Templates & Static Members

Description:

- Computer Notes - Templates & Static Members in Object oriented Programming what is Templates & Static Members Explain about it in detail .explain it with example – PowerPoint PPT presentation

Number of Views:248
Slides: 23
Provided by: ecomputernotes
Category:

less

Transcript and Presenter's Notes

Title: Computer Notes - Templates & Static Members


1
Templates Static Members
http//ecomputernotes.com
2
Templates Static Members
  • Each instantiation of a class template has its
    own copy of static members
  • These are usually initialized at file scope

http//ecomputernotes.com
3
Templates Static Members
  • templatelt class T gt
  • class A
  • public
  • static int data
  • static void doSomething( T )

http//ecomputernotes.com
4
Templates Static Members
  • int main()
  • Alt int gt ia
  • Alt char gt ca
  • ia.data 5
  • ca.data 7
  • cout ltlt ia.data ltlt ia.data
  • ltlt endl
  • ltlt ca.data ltlt ca.data
  • return 0

http//ecomputernotes.com
5
Templates Static Members
  • Output
  • ia.data 5
  • ca.data 7

http//ecomputernotes.com
6
Templates Conclusion
  • Templates provide
  • Reusability
  • Writability
  • But can consume memory if used without care

7
Templates Conclusion
  • Templates affect reliability of a program
  • templatelt typename T gt
  • bool isEqual( T x, T y )
  • return ( x y )

http//ecomputernotes.com
8
Templates Conclusion
  • One may use it erroneously
  • int main()
  • char str1 Hello
  • char str2 World!
  • isEqual( str1, str2 )
  • // Compiler accepts!

http//ecomputernotes.com
9
Generic Algorithms Revisited
  • templatelt typename P, typename T gt
  • P find( P start, P beyond,
  • const T x )
  • while ( start ! beyond
  • start ! x )
  • start
  • return start

http//ecomputernotes.com
10
Generic Algorithms Revisited
  • int main()
  • int iArray5
  • iArray0 15
  • iArray1 7
  • iArray2 987
  • int found
  • found find(iArray, iArray 5, 7)
  • return 0

http//ecomputernotes.com
11
Generic Algorithms Revisited
  • We claimed that this algorithm is generic
  • Because it works for any aggregate object
    (container) that defines following three
    operations
  • Increment operator ()
  • Dereferencing operator ()
  • Inequality operator (!)

12
Generic Algorithms Revisited
  • Let us implement these operations in Vector to
    examine the generality of the algorithm
  • Besides these operations we need a kind of
    pointer to track the traversal

http//ecomputernotes.com
13
Example Vector
  • templatelt class T gt
  • class Vector
  • private
  • T ptr
  • int size
  • int index // initialized with zero
  • public
  • Vector( int 10 )

14
Example Vector
  • Vector( const Vectorlt T gt )
  • T operator (int)
  • int getIndex() const
  • void setIndex( int i )
  • T operator ()
  • bool operator !(
  • const Vectorlt T gt v )
  • Vectorlt T gt operator ()

http//ecomputernotes.com
15
Example Vector
  • templatelt class T gt
  • int Vectorlt T gtgetIndex() const
  • return index
  • templatelt class T gt
  • void Vectorlt T gtsetIndex( int i )
  • if ( index gt 0 index lt size )
  • index i

16
Example Vector
  • templatelt class T gt
  • VectorltTgt VectorltTgtoperator ()
  • if ( index lt size )
  • index
  • return this
  • templatelt class T gt
  • T Vectorlt T gtoperator ()
  • return ptrindex

http//ecomputernotes.com
17
Example Vector
  • templatelt class T gt
  • bool VectorltTgtoperator !( VectorltTgt v )
  • if ( size ! v.size
  • index ! v.index )
  • return true

18
Example Vector
  • for ( int i 0 i lt size i )
  • if ( ptri ! v.ptri )
  • return true
  • return false

http//ecomputernotes.com
19
Example Vector
  • int main()
  • Vectorltintgt iv( 3 )
  • iv0 10
  • iv1 20
  • iv2 30
  • Vectorltintgt beyond( iv ),found( 3 )
  • beyond.setIndex( iv.getSize() )
  • found find( iv, beyond, 20 )
  • coutltltIndex ltltfound.getIndex()
  • return 0

20
Generic Algorithm
  • templatelt typename P, typename T gt
  • P find( P start, P beyond,
  • const T x )
  • while ( start ! beyond
  • start ! x )
  • start
  • return start

http//ecomputernotes.com
21
Problems
  • Our generic algorithm now works fine with
    container Vector
  • However there are some problems with the
    iteration approach provided by class Vector

http//ecomputernotes.com
22
Problems
  • No support for multiple traversals
  • Inconsistent behavior
  • We use pointers to mark a position in a data
    structure of some primitive type
  • Here we use the whole container as marker e.g.
    found in the main program
  • Supports only a single traversal strategy

http//ecomputernotes.com
Write a Comment
User Comments (0)
About PowerShow.com