Templates CS212 & CS-240. Reuse Templates allow extending our classes Allows the user to supply...

13
Templates CS212 & CS-240

Transcript of Templates CS212 & CS-240. Reuse Templates allow extending our classes Allows the user to supply...

Page 1: Templates CS212 & CS-240. Reuse Templates allow extending our classes Allows the user to supply certain attributes at compile time. Attributes specified.

Templates

CS212 & CS-240

Page 2: Templates CS212 & CS-240. Reuse Templates allow extending our classes Allows the user to supply certain attributes at compile time. Attributes specified.

Reuse• Templates allow extending our classes • Allows the user to supply certain attributes

at compile time.• Attributes specified in template definition• Resolved by compiler via text substitutions• Applies template parameter to template

body.• Like text “replace” in a wordprocessor

Page 3: Templates CS212 & CS-240. Reuse Templates allow extending our classes Allows the user to supply certain attributes at compile time. Attributes specified.

Template Functions

• Not really a function, • It's the design (a pattern) of a function• starts off with template <class name>

– in function body, name will be replaced with actual type of submitted parameter

– typename may be substituted for class

• multiple substitutions can be specified by using <class obj1, class obj2, class obj3>

Page 4: Templates CS212 & CS-240. Reuse Templates allow extending our classes Allows the user to supply certain attributes at compile time. Attributes specified.

Template Function

template <class T>

T sum3 ( T a , T b , T c)

{ return (a+b+c); }

int main ( )

{ int l,m,n;

double r,s,t;

/* use sum3 to sum sets variables of different types */

cout << sum3 (l,m,n) << “ “ << sum3 (r,s,t);

}

Page 5: Templates CS212 & CS-240. Reuse Templates allow extending our classes Allows the user to supply certain attributes at compile time. Attributes specified.

Template class

• Like a function template is not a function a template class is not a class, but it is a pattern for the compiler on how to define the class

• template parameters are specified the same as for template functions

• text substitutions are done explicitly not implicitly like for template functions

Page 6: Templates CS212 & CS-240. Reuse Templates allow extending our classes Allows the user to supply certain attributes at compile time. Attributes specified.

General Form of a Template Class

template <typename T> // choices are: class or typenameclass templateClass{

public: // constructortemplateClass (const T& item);// member functionsT f( );void g (const T& item);

private:T dataValue;…

};

Required keywords, not related to typename

Name of your new class

Page 7: Templates CS212 & CS-240. Reuse Templates allow extending our classes Allows the user to supply certain attributes at compile time. Attributes specified.

Sample usage

template <class T> class mine; // declared, not defined yet

class mine <int> *mineptr; // declare pointer

class mine<int> newone; // error, unknown size

template <class L> class mine

{// template class defined here

};

//"newone wants to be a "class template"

// "mine" is the "template class"

Page 8: Templates CS212 & CS-240. Reuse Templates allow extending our classes Allows the user to supply certain attributes at compile time. Attributes specified.

MemoryCell Template class

template <class T>class MemoryCell{ public: /* constructor */ explicit MemoryCell( const T & initVal = T( ) ) :

storedValue(initValue){ } /* accessor functions */ const T & read ( ) const { return storedValue; } void write (const T & x) { storedValue = x; } private: T storedValue;}

Page 9: Templates CS212 & CS-240. Reuse Templates allow extending our classes Allows the user to supply certain attributes at compile time. Attributes specified.

MemoryCell class

• Notice, previous examples use “inline” code to define the class

• Notice also that the syntax is slightly different

• The next example separates into interface and implementation

Page 10: Templates CS212 & CS-240. Reuse Templates allow extending our classes Allows the user to supply certain attributes at compile time. Attributes specified.

MemoryCell interface

template <class T>class MemoryCell{ public: explicit MemoryCell (const T & initVal = T( )); const T & read( ) const; void write (const T & x); private: T storedValue;}

Page 11: Templates CS212 & CS-240. Reuse Templates allow extending our classes Allows the user to supply certain attributes at compile time. Attributes specified.

MemoryCell implementation

#include “MemoryCell.h”template <class T>MemoryCell<T>:: MemoryCell (const T & initVal) :

storedValue(initVal) {// code for default constructor (if any)}

template <class T>const T & MemoryCell<T> :: read ( ) const { return storedValue; }

template <class T> void MemoryCell<T> :: write (const T & x){ storedValue = x ;}

Page 12: Templates CS212 & CS-240. Reuse Templates allow extending our classes Allows the user to supply certain attributes at compile time. Attributes specified.

Using the template class

• Assuming that we had defined a template class named stack– to define a stack of integers called myIntStack you

would use:• stack<int> myIntStack;

– to define a stack of doubles called myDblStack you would use:• stack<double> myDblStack;

– to define a stack of people called myFolks you would use:• stack<people> myFolks;

Page 13: Templates CS212 & CS-240. Reuse Templates allow extending our classes Allows the user to supply certain attributes at compile time. Attributes specified.

Muddying the waters a bit

<typename T, class C> // uses items T in container C

class Myclass

{ public:

~Myclass();

void push( const T & );

private:

C item;

};

Myclass <int, List<int> > newone; // now create one