Download presentation
Presentation is loading. Please wait.
Published byKatrina Ramsey Modified over 8 years ago
1
Copyright © 1999-2014 Curt Hill Generic Classes Template Classes or Container Classes
2
Copyright © 1999-2014 Curt Hill Why? The idea of either generic functions or classes is to separate the logic from the data handled Recall that a sort does not care much what the data type is – the logic is the same There are two ways to do this: –Polymorphism –Generics
3
Copyright © 1999-2014 Curt Hill Polymorphism We can separate logic and data with polymorphism Make an inheritance tree that contains all the relevant data types Make sure that they all have the needed operations, usually assignment and comparison Let the run-time system determine and use the correct operations
4
Copyright © 1999-2014 Curt Hill Generics The generic function or class has a compile-time parameter of type to be handled As long as this type has the correct properties it may be used Conceptually it is similar to using a group replace on a piece of code –Each reference to TYPE is replaced with the needed type –Then the result is compiled
5
Copyright © 1999-2014 Curt Hill Tradeoffs Polymorphism requires run-time overhead –The type must be determined –This type is use to select the correct method Generics put the overhead at compile-time –Make the compiler do much more work –In a compile once, run many times environment this is the economical way
6
Copyright © 1999-2014 Curt Hill Templates The reserved word for these is template This is usually followed by a parameter list: template –Reserved word ‘class’ or ‘typename’ precedes the generic name of the type –TYPE is user defined name of the generic type –TYPE does not have to be an object type
7
Copyright © 1999-2014 Curt Hill Container Classes Most of the template classes implement well known data structures: list tree or map set stack or queue The operation of any of these is mostly independent of the data carried All that is required is that the contained data has certain operators such as assignment and comparison These are storage or organizing classes
8
Copyright © 1999-2014 Curt Hill Template Set object Suppose a set class exists The template version would be essentially the same if we changed the define from int to double We would just have to rename our class to double_set as well Rather than doing that C++, has the template feature which makes this easy and painless
9
Copyright © 1999-2014 Curt Hill First example Recall our set class header: class Set{ … }; // end class To make this a container class header: template class Set { … }; // end class Prefix the class with: template
10
Original Set Copyright © 1999-2014 Curt Hill #define TYPE int class set{ private: // dynamic array one member entry TYPE * s; unsigned short int used,size; … public: set(); // Default constructor set(TYPE a); ~set(); set operator + (const set &)const; set operator * (const set &)const; …
11
Template Set Copyright © 1999-2014 Curt Hill template class set{ private: // dynamic array one member entry TYPE * s; unsigned short int used,size; … public: set(); // Default constructor set(TYPE a); ~set(); set operator + (const set &)const; set operator * (const set &)const; …
12
Copyright © 1999-2014 Curt Hill Discussion The TYPE will be the type name of the type we will parameterize the template with (or the type we want the class to contain) From then on TYPE will be used as if it is a previously known type From that point on Set is a peculiar type: it must be parameterized with the type that we want it to contain This is a compile-time parameter
13
Copyright © 1999-2014 Curt Hill Using it in client code To declare sets in client code we do the following: set is; set rs; Each occurrence of the name Set must be accompanied by a type in angle brackets The thing in angle brackets needs to be a type suitable for assignment and comparison for set –May be natural or overloaded
14
Copyright © 1999-2014 Curt Hill Discussion Set is now templatized –Remember you saw it first here The type cannot be referenced without mention of the carried type This is always in angle brackets How is the regular set different than the templatized set implementation? –Each method is preceded by template –Any reference to the type is the generic type
15
Inside and Outside Did you notice the discrepancy? In the header we just use set –This refers to the templatized type –It is under the template In the client code we must always parameterize it –set The implementation code has some complications as well Copyright © 1999-2014 Curt Hill
16
Implementation code For regular Set + set set::operator + (const set & y)... For template set + template set set ::operator + (const set & b) const{
17
Copyright © 1999-2014 Curt Hill Set as a type Client code: –Always parameterized Header –Never parameterized except by template Implementation –Prefix to scope resolution operator must be parameterized –Return types as well
18
Example Usual template set set ::operator + (const set & b) const{ Acceptable template set set ::operator + (const set & b) const{ Copyright © 1999-2014 Curt Hill
19
Constructors The constructor name has two meanings When defining it functions like a method name so does not need the brackets: template Set ::Set(){… When called it functions like a type in a cast – does need type: s = Set ();
20
Copyright © 1999-2014 Curt Hill Effect The runtime effect is the same as if the different classes were manually generated The compiler reprocesses the source each time it finds a reference to a template class method that it has not previously processed The source of the template (ie. implementation) must be available –Usually in the header file
21
Copyright © 1999-2014 Curt Hill Example (1) Suppose the following: set is; set fs(4.5); is = set (8)+set (2); What does the compiler do? Before encountering this it must have seen the template definition of Set and all its method implementations
22
Copyright © 1999-2014 Curt Hill Example (2) When it sees this: set is; It now rescans the header Does the substitution with int to calculate the space to reserve It also processes the default constructor after doing the substitution for int
23
Copyright © 1999-2014 Curt Hill Example (3) When it sees this: set fs(4.5); It now rescans the header again Does the substitution with float to calculate the space to reserve It also processes the single type constructor after doing the substitution for float
24
Copyright © 1999-2014 Curt Hill Example (4) When it sees this: is = Set (8)+Set (2); It now rescans the header again It processes the single type constructor after doing the substitution for int It processes the + operator after doing the substitution for int It processes the = operator after doing the substitution for int
25
Copyright © 1999-2014 Curt Hill Example Discussion Very few compilation problems can be found in the initial pass of the template class –Usually only things having to do with the template line Compile errors are mostly found when the substitution is done and methods recompiled These occur when the code requires a property that the substituted type does not possess
26
Copyright © 1999-2014 Curt Hill Form of Header File Short classes may do everything in class header Longer classes usually have a header followed by implementations –Makes the header easier to read –These are still in the.h file –Each is prefaced by the template construction There is no.CPP file, all is in.H
27
Style How do we do the implementation? Two styles: –The Java Style –The C++ Style Which one do you think I prefer? Copyright © 1999-2014 Curt Hill
28
Java Style No function headers – everything is implemented immediately template class MyClass{ public: MyClass(){ …} Copyright © 1999-2014 Curt Hill
29
Development The typical method for a template class is: Develop a standard class of the right flavor and test Use a contained type such as float –A type that would not otherwise be used Place all the code in a header Prefix each item with a template construction Replace all the floats with the generic type
30
C++ Style Many template directives template class MyClass{ public: MyClass(); … }; // end of MyClass template MyClass ::MyClass{ …} Copyright © 1999-2014 Curt Hill
31
Template Construction Prefaces just one unit –Usually a class, function, or method –The class reserved word must be followed by the generic type Multiple parameters are allowed: template –T, U, V are user defined names –By convention uppercase –The scope of these is just the following unit –Instantiation must have three types
32
Copyright © 1999-2014 Curt Hill Nesting A template class may contain another template class: map > tree; Notice that the two > cannot be adjacent or the lexical analyzer determines that it is the >> operator map and list are both Standard Template Library container classes
33
Template Friends Previously we saw classes which are friends A linked list is a good example: –The node –The root –The iterator Root and iterator are friends of one another and both are friends of node How is this done? Copyright © 1999-2014 Curt Hill
34
Declaration The forward declaration needs a template, but the type is not used: template class LinkedList; When it is declared as a friend the type is present: template class ListNode{ friend class LinkedList ; A friend function works similarly Copyright © 1999-2014 Curt Hill
35
The end of this One of the positive outcomes of the introduction of templates is the development of the Standard Template Library (STL) There are several container classes that have been developed These have a standard interface which usually include several flavors of iterators This usually comes with compiler
36
DevC++ Error Shows an odd error, not seen in other compilers –If the method parameter names change errors ensue Example: template class CX{ void f(int a); …}; template void CX ::f(int b){ Copyright © 1999-2014 Curt Hill
37
Other Template Libraries Peer reviewed portable C++ libraries –http://www.boost.orghttp://www.boost.org Matrix Template Library –http://www.osl.iu.edu/research/mtl/http://www.osl.iu.edu/research/mtl/ Windows Template Library –http://sourceforge.net/projects/wtl/http://sourceforge.net/projects/wtl/ OO Template Library –http://www.ootl.orghttp://www.ootl.org Scientific Computing –http://www.oonumerics.org/http://www.oonumerics.org/ Iterative Method Library –http://Math.nist.gov/iml++/http://Math.nist.gov/iml++/
Similar presentations
© 2024 SlidePlayer.com Inc.
All rights reserved.