Templates - PowerPoint PPT Presentation

About This Presentation
Title:

Templates

Description:

Title: Training Author: Preferred Customer Last modified by: Hany Ammar Created Date: 6/2/1995 10:16:36 PM Document presentation format: Overhead Other titles – PowerPoint PPT presentation

Number of Views:20
Avg rating:3.0/5.0
Slides: 7
Provided by: Preferred99
Category:

less

Transcript and Presenter's Notes

Title: Templates


1
Templates
  • Class Templates
  • Used to specify generic class types where class
    members data types can
  • be specified as parameters, e.g. here is a
    generic List class template,
  • template ltclass tgt // t is a generic data type
    parameter
  • class List
  • int size
  • t list // a pointer to an object of the
    generic type t
  • public // example member functions
  • List(int sz 10)
  • t operator(int)
  • // member functions bodies must be preceded by
    the template keyword
  • template ltclass tgt ListlttgtList(int sz) list
    new t size sz
  • template ltclass tgt t Listlttgtoperator
    (int j)return listj
  • main() Listltchargt ch_arr(80) Listltdoublegt
    dbl_flt(15)
  • ListltComplexgt x // instantiate 3 classes and
    three objects

2
Templates
  • Class Templates (cont.)
  • Template class instantiation
  • Listlttypegt Object_name(parameters) //
    instantiates a Listltchargt
  • // class at compile time, the generic type t is
    replaced by type, then the
  • // object is instantiated as shown in the above
    main() function
  • Non-type parameters in class templates
  • template ltclass t, int sizegt List
  • t listsize // statically allocated list of
    type t elements
  • // the size of the list and the type of elements
    are determined at the class
  • // instantiation. In the above, the size of the
    list is specified at compile
  • // time no need for dynamic memory allocation,
    this avoids run time //errors due to new and
    eliminates execution overhead for memory
    //allocation
  • Listltfloat, 500gt b// instantiates the class
    List with float type
  • // members and a size of 500 and instantiates an
    object b of this type

3
Templates
  • Templates and inheritance
  • An explicit Base class instantiated from a class
    template, e.g.,
  • class Integer_Stack public Listltintgt //
    // the base class is // instantiated from a the
    template List ltclass tgt class
  • A template derived class with a non-template
    base class
  • template ltclass tgt class D public B // data
    members, member function // arguments, return
    types in D can take the generic type t, inherited
    members // of B all have explicit types
  • Both, a template derived class with a template
    base class
  • template ltclass tgt class Stack public
    Listlttgt
  • public Stack(int sz) Listlttgt(sz)
    //references with formal parameters
  • // the following statement instantiates class
    Stackltfloatgt and class Listltfloatgt
  • Stackltfloatgt a(100) // object a is instantiated
    from these classes

4
Templates
  • Templates and friend functions and classes
  • friend functions of a template class
  • template ltclass tgt class X
  • friend void f2(Xlttgt) // declares f2() as
    friend to Xlttgt type objects
  • Xltfloatgt a // assumes the existance of the
    function f2(Xltfloatgt), this
  • //function can have access to all members of
    Xltfloatgt type objects
  • Friend classes
  • template ltclass typegt class ListNode type Item
    ListNodelttypegt Next
  • template ltclass Tgt friend class List // all
    classes of ListltTgt are
  • // friends of class ListNodelttypegt, this is
    dangerous since type might be
  • // different from T, it is better to do the
    following instead,
  • friend class Listlttypegt // only the
    Listlttypegt class where T is replaced by type will
    be friend of all classes of the template class
    ListNodelttypegt

5
Templates
  • Templates, Container Classes, and Class Libraries
  • A container class is one that operates on a
    collection of one or more objects of a particular
    type, e.g., Lists, Stacks, Linked Lists, Queues,
    etc.,
  • Container classes are supported in the
    compilers class library as class templates, e.g.
    in Borland C the directory \classlib\Include
    have several header files which defines container
    class templates, these can be used as follows,
  • include ltclasslib\stacks.hgt
  • include ltiostream.hgt
  • void main()
  • TStackAsVectorltintgt x(100)
  • x.push(50)
  • x.push(30)
  • while(!x.IsEmpty()) cout ltlt x.pop() ltlt endl

6
Templates
  • Function templates
  • A function template is used to specify generic
    functions in the same way as class templates is
    used to specify generic classes, for example,
  • template ltclass Tgt
  • T abs(T n) // a generic function which returns
    the absolute value of object n
  • // of type T
  • return (n lt 0) ? -n n
  • void main()
  • int x -500 long y -100000, double z
    -1.87
  • cout ltlt abs(x) ltlt \t ltlt abs(y) ltlt \t ltlt
    abs(z)
  • // three different overloaded functions named abs
    are generated at compile
  • // time, these are abs(int), abs(long), and
    abs(double)
Write a Comment
User Comments (0)
About PowerShow.com