CMPUT 201: Lab 8 - PowerPoint PPT Presentation

1 / 14
About This Presentation
Title:

CMPUT 201: Lab 8

Description:

template class T ... All of a class template's member functions are function templates. You declare objects of a class template by providing a name and a ... – PowerPoint PPT presentation

Number of Views:25
Avg rating:3.0/5.0
Slides: 15
Provided by: nla5
Category:
Tags: cmput | class | lab

less

Transcript and Presenter's Notes

Title: CMPUT 201: Lab 8


1
CMPUT 201 Lab 8
Templates
November 14th, 2002
2
Function Templates
  • Often program methods implement some general
    algorithm that applies to any data type (e.g.
    swap values, sort a list, compare values)
  • We would like to define one generic function
    that handles all variable types (primitive types
    or user-defined classes)
  • Template functions allow us to do this in C
  • This saves us from declaring many functions
    that are identical in logic but differ only in
    their types

3
Function Template Syntax
  • Template functions must be preceded by a
    template prefix, which has the following form
  • templateltclass Tgt
  • This tells the compiler the function that
    follows is a template and T is a type parameter
  • T can be any word except a keyword
  • In the body of the function, T is used like
    any other type

4
Function Template Example
  • Consider the example below
  • template ltclass Tgt
  • int findCreature(T list, int size, char
    name) int location 0
  • while ( (location lt size) strcmp(
    listlocation.getName(), name) )
  • location
  • return location

5
Function Template Example
int main() int l Hobbit10 hobbits
Elf10 elves ... // calls the definition
of findCreature // that uses hobbits l
findCreature(hobbits, 10, Samwise) // calls
the definition of findCreature // that uses
elves l findCreature(elves, 10, Legolas)
6
Function Template Implementations
  • The preceding code suggests there is a
    separate version of findCreature for each
    variable type that is defined in that file
  • The compiler does not generate a separate
    version for every possible data type, it only
    generates versions for types that are used
  • When the compiler comes across a call to
    findCreature, it looks at the parameter type and
    generates a method definition for this type,
    assuming no such definition already exists

7
Template Function vs. Virtual Functions
  • Template function selection is done at compile
    time whereas virtual function selection is done
    at run time
  • The compiler uses the static type of the
    parameters in the template function invocation to
    determine which method definition to call
  • Template functions are a convenience mechanism
    offered by the compiler

8
Class Templates
  • Template classes allow us to define generic
    classes that specify a certain structure and
    purpose regardless of the data types they hold
  • Common examples are lists, arrays, stacks,
    queues, etc
  • Such classes are also called container classes
  • Template classes save us from declaring many
    classes that are identical in structure and
    purpose but differ only in their instance
    variable data types

9
Class Template Example (Part I)
  • Consider the example below
  • templateltclass Tgt
  • class creatureProfile
  • public
  • creatureProfile(T aCreature)
  • creatureProfile(T aCreature, int
    amountOfGold)
  • private
  • T creature int gold

10
Class Template Example (Part I)
templateltclass Tgt creatureProfileltTgtcreatureProf
ile( T aCreature) creature
aCreature templateltclass Tgt creatureProfileltTgt
creatureProfile( T aCreature, int
amountOfGold) creature aCreature gold
amountOfGold
11
Class Template Details
  • Syntax is the same as that of function
    templates (a template prefix must precede the
    class and all its function definitions)
  • All of a class templates member functions are
    function templates
  • You declare objects of a class template by
    providing a name and a parameter type (much like
    you declare an array)
  • The compiler will produce a class with that
    corresponding parameter type

12
Class Template Example (Part II)
int main() Hobbit h1(Frodo) Dwarf
d1(Gimli) // create an instance of the //
creatureProfile class that has a hobbit // data
type and give the hobbit 25 gold
creatureProfilelthobbitgt FrodoPfl(h1, 25) //
create an instance of the // creatureProfile
class that has an elf // data type
creatureProfileltelfgt GimliPfl(d1)
13
Templates and Inheritance
  • Inheritance of class templates works the same
    way as normal inheritance
  • template ltclass Tgt
  • class magicalCreatureProfile public
    creatureProfileltTgt public
  • magicalCreatureProfile(T aCreature,
    SpellBook spellsItKnows)
  • private
  • SpellBook spells

14
Templates and Inheritance
templateltclass Tgt magicCreatureProfileltTgtmagicCr
eatureProfile( T aCreature, SpellBook
spellsItKnows) creatureProfileltTgt(aCreature)
spells new SpellBook() spells
spellsItKnows.deepCopy()
Write a Comment
User Comments (0)
About PowerShow.com