CS304: Lecture 12 - PowerPoint PPT Presentation

1 / 18
About This Presentation
Title:

CS304: Lecture 12

Description:

Templates are C 's method of allowing generic ... Imagine writing just one sort that will accept an arbitrary ... ( Practical application: Summing an array ... – PowerPoint PPT presentation

Number of Views:42
Avg rating:3.0/5.0
Slides: 19
Provided by: matthe49
Category:
Tags: cs304 | lecture

less

Transcript and Presenter's Notes

Title: CS304: Lecture 12


1
CS304 Lecture 12
  • Templates
  • Deitel, Ch. 14
  • http//www.cis.uab.edu/cs304

2
Generic Programming
  • Templates are Cs method of allowing generic
    programming.
  • This is the method of allowing a single code
    segment an entire range of overloaded functions,
    or an entire range of related classes.

3
Function vs. Class Templates Examples
  • Function A sort function! Imagine writing just
    one sort that will accept an arbitrary container
    type, as well as an arbitrary data type.
  • Class A stack class. Such a stack could be
    instantiated for any type, so it could hold any
    data type with one implementation. (How would
    this be different from a similar stack in Java?)

4
Function Templates
  • When overloading functions, its often
    repetitive, resulting in identical (or similar
    functions)
  • Identical implementations can be abstracted to a
    template, where the types can be inferred from
    calls to the function.

5
Example Add
  • Initially, unrelated implementations
  • int add(int x, int y) return x y
  • unsigned int add (uint x, uint y) return x y
  • char add(char x, char y) return x y
  • float add(float x, float y) return x y
  • double add(double x, double y) return x y
  • mytype add(mytype x, mytype y) return x y

6
Problems with Add
  • This takes SIX function definitions to do the
    same exact things with the six types given
  • Also, notice If we want to add more user
    defined types, we have to add even more!

7
Add An Improvement Through Templates
  • Template lt typename Type gt
  • Type add(Type x, Type y)
  • return x y
  • //Note The type template parameter Type is
    usually abbreviated as T

8
How Does the Compiler Handle This?
  • In this case, there is very little intelligence
    behind the actual implemenation of templates.

9
Templates with User Defined Types
  • How can we automagically use a user defined type
    in the add function?
  • Well, automagic isnt the way computers work. We
    have to overload the operator
  • This is the second reason that operator
    overloading is so important in C!
  • No overloading results in a program that wont
    compile

10
Overloading Function Templates
  • What if almost all of the functions we want to
    overload are identical, but one or two are quite
    different? (Practical application Summing an
    array of numbers)
  • Specifying a non-templatized function will
    override the corresponding function created by
    the template

11
Class Template
  • Lets expand on the idea of a stack template that
    can take any type. Here is the usual
    declaration

12
  • class stack int d int index int
    sizestack() d new int10 size 10 index
    -1 stack() delete d void push(int
    val)
  • int peek()
  • int pop()

13
  • template lttypename Tgt class stack T d int
    index int sizestack() d new T10 size
    10 index -1 stack() delete d void
    push(T val)
  • T peek()
  • T pop()

14
Using a Template Class in a Program
  • Using a function template is trivial Calling the
    function as usual does the trick
  • Instantiating a templated class is not the same,
    because the compiler wont know which type to use
    to instantiate. This must be given at
    declaration.
  • int main() Stack ltdoublegt doubleStack

15
Nontype Parameters
  • Nontypes can be used with templates in order to
    make compile-time information useful in a classs
    definition.
  • Example A stack class that has a statically
    allocated block of memory of a specific size, in
    order to reduce the cost of dynamic allocation
  • template lttypename T, int elementsgt
  • Stackltdouble, 100gt HundredSlotStack
  • Instantiate a 100-element stack

16
Notes on Templates and
  • Inheritance
  • A class template can be derived from
  • a class-template specialization
  • a nontemplate class
  • A class-template specialization can be derived
    from
  • a class-template specialization ???
  • A nontemplate class can be derived from
  • a class-template specialization

17
Notes on Templates and
  • Friends
  • It is possible to use template classes,
    class-template specializations, and template
    functions as friends
  • Please see 14.7 in the book for further
    information, but it works as you would expect.

18
Notes on Templates and
  • Static Members
  • Recall that class-template specializations make
    copies of the class, and basically substitutes
    the typename in for the typename variable used in
    the class
  • This implies that static variables are shared
    between all objects of a class-template
    specialization, but not all class-template
    specializations, i.e., each specialization has
    its own copy
Write a Comment
User Comments (0)
About PowerShow.com