Skip to main contentCal State San Bernardino / [CNS] / [Comp Sci Dept] / [R J Botting] >> [CSci202] >> 17
[Index] [Schedule] [Syllabi] [Text] [Labs] [Projects] [Resources] [Search] [Contact] [Grading]
Notes: [01] [02] [03] [04] [05] [06] [07] [08] [09] [10] [11] [12] [13] [14] [15] [16] [17] [18] [19] [20]
Wed May 30 17:19:08 PDT 2007

Contents


    CSci202 Computer Science II, Session 17, Template Classes


      (previous): Do-It_Yourself Iterators: [ 16.html ]

      Preparation

      Study this page and Chapter 14 sections 14.1 only and write down the questions, doubts, and surprises that you have on a piece of paper.

      Assigned Work Due

      Hand in your questions, doubts and surprises. Include pages numbaers and name.

      Bonus - Project 5

      Input

      1. Key syntax:
         	template < parameters > class ....{ ..... };
         	template < parameters > function { ..... }
         	template_name < actual_parameters > .....;

      2. I have some notes you should review [ templates.html ]

        UML of page 314

      3. 14.1.1 Templates vs Instances
      4. 14.1.2 Static members
      5. 14.1.3 Friends and helpers
      6. 14.1.4 Parameters
      7. 14.1.5 Property classes (optional)
      8. 14.1.6 Standard Generic Classes

      Quick Exercise

      Here [ intStack.cpp ] is a simple class (and a test function) that defines a stack of ints. What do you change to make it into a generic stack that can work with any type of data?

      Questions

        What is a template and how is it used

        A template describes a rule for constructing classes. Typically they are used by providing an actual class to replace the tempalte parameter. The compiler does the hard lifting.... a kind of reliable find, cut, and paste with the template.

        What is a static member?

        A part of a class that is not part of an object.... but part of the class itself. In a templated class then each instance of the template has it's own special members...

        Not on final.

        Not in Labs.

        You may need them in your future career.... perhaps.

        Are non-member template functions declared the same as member function?

        Yes.... but no need for a
         		ClassName::
        before the generic function:
         		template<typename T> Type_returned name(arguments){body}

        For example

         		template<class T> void swap(T&a, T&b){ T t=a; a=b; b=t; }
        See [ tswap.cpp ] (note -- there is a version of swap in the std library so I don't use namespace std).

        Do you have any simple template examples?

        Yes! See above and [ templates.html ]

        How do templates work?

        The compiler generates the class and function definitions that you need, automatically, and inserts them in your program in the right places.

        How are friends and helper classes related?

        A helper class helps you implement another class. Normally it is simpler for the helper class to let the class it helps have access to its variables and functions. So it declares it to be a friend.

        These days put helper classes and functions in the private part of the helped class.

        How does a class template differ from a class?

        A class template is a rubber stamp that produces classes (instances) as we need them. It describes an infinite set of possible classes. A class describes only one class.

        If a template is declared template<class T> can T be a simple data type?

        Yes. I think so. I find it less confusing to write "template <typename T>" instead.

        What do we do with a definition and a header file if the header defines a template?

        Keep the bodies inside the header.

        Why does anybody ever put the bodies in a separate file?

        (1) To hide the details of your clever coding and data from other programmer's eyes. This means they can't take advantage of any tricks inside the functions. All they can see (and abuse) are the headers in the '.h' file.

        (2) You can precompile the bodies into a ".o" object file once. Then you don't have to wait for the compiler to recompile it every time you use it.

        Note: if you do split body from header.... use a tool like "make" to handle compilations.

        Are there any predefined template classes in the standard library?

        Yes. Hundreds. Including: vector, dequeue, list, ...... Even the <algorithm>s are template functions!

        We made a template in CS201.... and it was quite useful!

        Cool!

        How do you have different types in a single typename parameter?

        Set up a object-oriented hierarchy and put pointers in the instance:
         		vector< Shape *> shapes;
        See [ 18.html ] next class.

      Exercises

      Write a template function called min that returned the smallest of two values.... of any type.

    . . . . . . . . . ( end of section CSci202 Computer Science II, Session 17, Template Classes) <<Contents | End>>

    Laboratory on templates

    [ lab09.html ]

    Next -- Project 5 and Chapter 14.2

    Project 5 (Ch 13.1..11) is due! Resubmit 4. Topic (chapter 14, section 14.2) Introduction to template functions [ 18.html ]

    Abbreviations

  1. TBA::="To Be Announced", something I have to do.
  2. TBD::="To Be Done", something you have to do.
  3. Dia::="A free Open Source Diagramming tool for Linux, Windoze, etc. ".
  4. YAGNI::="You Ain't Gonna Need It".
  5. DRY::="Don't Repeat Yourself".

End